flutter Splash启动页(闪屏)页面解决方案实战

  • Post author:
  • Post category:其他




1.下载插件

pubspec.yaml 文件中加入该flutter_splash_screen: ^0.1.0



2.修改MainActivity.java

路径:android\app\src\main\java\com\example\ganxin\MainActivity.java
package com.example.XXX;
import android.os.Bundle;
import io.flutter.embedding.android.FlutterActivity;
//导入我们引入的第三方框架
import org.devio.flutter.splashscreen.SplashScreen;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.engine.FlutterEngine;
import androidx.annotation.NonNull;
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);
    }
}



3.新增drawable目录 并且在里面新建文件launch_background.xml

路径:android\app\src\main\res\drawable

launch_background.xml:

代码块:

<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />
</layer-list>



4.styles.xml文件修改

路径:

android\app\src\main\res\values\styles.xml

代码块:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/launch_background</item>
        <item name="android:windowIsTranslucent">true</item>//加这一行
    </style>
    <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@android:color/white</item>
    </style>
</resources>



5.新增 colors.xml文件

路径:

android\app\src\main\res\values\colors.xml

代码块:

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



6.新增 layout文件夹 并新增文件

路径:

android\app\src\main\res\layout\launch_screen.xml

代码块:

<?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/splash_bg" android:scaleType="centerCrop" />//splash_bg就是你需要展示的启动页图片
</RelativeLayout>



7.main.dart 修改

引入插件:

import ‘package:flutter_splash_screen/flutter_splash_screen.dart’;

  @override
  void initState() {
    super.initState();
    hideScreen();
  }

  ///hide your splash screen
  Future<void> hideScreen() async {
    Future.delayed(Duration(milliseconds: 3600), () {
      FlutterSplashScreen.hide();
    });
  }

完毕,此时可以测试查看。。。。。。



版权声明:本文为weixin_43273113原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。