Flutter项目启动时黑屏解决办法

我的flutter项目是带一个动画启动页的,在真机上开启app时,遇到以下现象:
白屏几秒->黑屏几秒->动画启动页。

研究了很多文献,一直想解决这个问题,目前已经得到了安卓上的解决办法。我先贴一下参考资料:
flutter启动时的背景颜色(不要白屏或黑屏)
Flutter - 给App增加启动屏幕(Splash Screen)并且设置背景颜色
谷歌官方文档,全面解析flutter项目接入android的启动流程

在android端,flutter项目启动时,经历三个过程:显示android启动项activity,再到flutter启动项activity,最后进入flutter项目的第一个页面(我这里是一个lottie动画页面)。黑屏实际上对应的是未设置的flutter启动项activity,知道了原理,解决办法就呼之欲出了。

1.首先,设置android项目里的AndroidManifest.xml文件,主要配置两个注释下面的内容,即meta-data:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hardcore.swift_rabbit">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="swift_rabbit"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

            <!--Flutter 闪屏页-->
            <meta-data
                android:name="io.flutter.embedding.android.SplashScreenDrawable"
                android:resource="@drawable/launch_background" />
            <!--闪屏页结束后的Theme-->
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />
        </activity>

        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

2.去 <android_project>/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>
    </style>
    <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/normal_background</item>
    </style>
</resources>

这里normal_background是一份需要在android\app\src\main\res\drawable路径下新建的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" />

    <!-- You can insert your own image assets here -->
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/naruto" />
    </item>
</layer-list>

这里你可以直接置白屏 item android:drawable="@android:color/white",
也可以添加一张png图片,我这里添加的是路径是…/res/mipmap-xxhdpi/naruto.png

到此,黑屏问题就解决了。如果本文对你有帮助的话,我会非常开心。如果以后有什么心得,也请分享给别人。赠人玫瑰,手有余香,加油ヾ(◍°∇°◍)ノ゙

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值