安卓里面設(shè)置啟動頁
這里我們需要借助第三方的框架flutter_splash_screen來實(shí)現(xiàn)璃弄。具體步驟如下:
1要销、需要在pubspec.yaml文件里面添加依賴:
dependencies:
flutter_splash_screen: ^xxx
2、通過flutter packages get來更新獲取依賴包谢揪。
3蕉陋、在安卓入口文件MainActivity.java里面添加如下代碼:
package com.example.aethersharedcommunication;
import android.os.Bundle;
//導(dǎo)入我們引入的第三方框架
import org.devio.flutter.splashscreen.SplashScreen;
import androidx.annotation.NonNull;
//import android.support.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//在此調(diào)用,實(shí)現(xiàn)引導(dǎo)頁屏幕的顯示
SplashScreen.show(this, true);
super.onCreate(savedInstanceState);
}
}
4拨扶、在GeneratedPluginRegistrant文件里面需要注冊我們使用到的中間件
package io.flutter.plugins;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
/**
* Generated file. Do not edit.
* This file is generated by the Flutter tool based on the
* plugins that support the Android platform.
*/
@Keep
public final class GeneratedPluginRegistrant {
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
//注冊啟動頁需要用到的中間件
org.devio.flutter.splashscreen.FlutterSplashScreenPlugin.registerWith(shimPluginRegistry.registrarFor("org.devio.flutter.splashscreen.FlutterSplashScreenPlugin"));
io.github.ponnamkarthik.toast.fluttertoast.FluttertoastPlugin.registerWith(shimPluginRegistry.registrarFor("io.github.ponnamkarthik.toast.fluttertoast.FluttertoastPlugin"));
flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
flutterEngine.getPlugins().add(new com.tekartik.sqflite.SqflitePlugin());
flutterEngine.getPlugins().add(new io.flutter.plugins.webviewflutter.WebViewFlutterPlugin());
}
}
5凳鬓、然后創(chuàng)建一個(gè)launch_screen.xml文件放在/android/app/src/main/res/layout目錄下,里面的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--launch_icon是我們啟動頁的名字-->
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
android:src="@drawable/launch_icon" android:scaleType="centerCrop" />
</RelativeLayout>
6患民、然后需要在app/src/main/res/values文件夾下缩举,新加一個(gè)colors.xml文件,這個(gè)文件主要是對狀態(tài)欄的顏色進(jìn)行設(shè)置匹颤,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--狀態(tài)欄的顏色-->
<color name="primary_dark">#000000</color>
</resources>
7仅孩、然后需要在app/src/main/res/values文件夾下,新加一個(gè)styles.xml文件印蓖,這個(gè)文件主要是對背景的顏色進(jìn)行設(shè)置辽慕,內(nèi)容如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
<!--設(shè)置透明背景-->
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
8、然后回到主頁面的flutter代碼赦肃,對啟動頁進(jìn)行關(guān)閉溅蛉,代碼如下:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_splash_screen/flutter_splash_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
hideScreen();
}
///hide your splash screen
Future<void> hideScreen() async {
Future.delayed(Duration(milliseconds: 3600), () {
FlutterSplashScreen.hide();
});
}
iOS里面設(shè)置啟動頁
1、打開我們的flutter項(xiàng)目的iOS工程他宛,如圖所示:
1587349068985.jpg
2船侧、將準(zhǔn)備好的圖片資源,按照如圖所示的約束添加好厅各,放入到LaunchScreen.storyboard里面
1587349132077.jpg
3镜撩、將圖片的模式設(shè)置為Scale to Fill,如圖所示:
1587349163728.jpg
到此队塘,我們就已經(jīng)將iOS和安卓的啟動頁設(shè)置OK袁梗。