美文网首页Flutter
Flutter里面设置iOS和安卓启动页

Flutter里面设置iOS和安卓启动页

作者: 从0到1的过程 | 来源:发表于2020-04-20 10:24 被阅读0次

    安卓里面设置启动页

    这里我们需要借助第三方的框架flutter_splash_screen来实现。具体步骤如下:
    1、需要在pubspec.yaml文件里面添加依赖:

    dependencies:
      flutter_splash_screen: ^xxx
    

    2、通过flutter packages get来更新获取依赖包。
    3、在安卓入口文件MainActivity.java里面添加如下代码:

    package com.example.aethersharedcommunication;
    
    import android.os.Bundle;
    //导入我们引入的第三方框架
    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) {
        //在此调用,实现引导页屏幕的显示
        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、然后创建一个launch_screen.xml文件放在/android/app/src/main/res/layout目录下,里面的内容如下:

    <?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文件夹下,新加一个colors.xml文件,这个文件主要是对状态栏的颜色进行设置,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!--状态栏的颜色-->
        <color name="primary_dark">#000000</color>
    </resources>
    

    7、然后需要在app/src/main/res/values文件夹下,新加一个styles.xml文件,这个文件主要是对背景的颜色进行设置,内容如下

    <?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>
            <!--设置透明背景-->
            <item name="android:windowIsTranslucent">true</item>
        </style>
    </resources>
    

    8、然后回到主页面的flutter代码,对启动页进行关闭,代码如下:

    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里面设置启动页

    1、打开我们的flutter项目的iOS工程,如图所示:


    1587349068985.jpg

    2、将准备好的图片资源,按照如图所示的约束添加好,放入到LaunchScreen.storyboard里面


    1587349132077.jpg
    3、将图片的模式设置为Scale to Fill,如图所示:
    1587349163728.jpg

    到此,我们就已经将iOS和安卓的启动页设置OK。

    相关文章

      网友评论

        本文标题:Flutter里面设置iOS和安卓启动页

        本文链接:https://www.haomeiwen.com/subject/ivrlihtx.html