Flutter Android项目启动白屏问题

白屏现象

首先,我们使用Android Studio创建一个Flutter项目。运行项目,我们先会看到先闪过一个白屏,然后再进入计数器页面。

那么问题来了,为什么是白屏,而不是黑屏或者其他颜色呢?

我们打开android项目,打开AndroidManifest.xml可以看到配置启动的主题@style/LaunchTheme,点击打开styles.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off -->
    <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             the Flutter engine draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
</resources>

点击@drawable/launch_background打开launch_background.xml,可以看到设置的背景为white。

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/launch_image" />
    </item> -->
</layer-list>

解决方式

我们以京东APP为例,通过解压后,找到启动图uh.9.png,该图不能直接使用,将图片拖入绘图后保存使用。

可以看到图片尺寸为718*1278,我们在res目录下创建drawable-xhdpi文件夹

使用layer-list

使用截图软件,截取启动图中间图片和底部的图片放入drawable-xhdpi,吸取背景颜色#FA2B18

修改launch_background.xml

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>
        <shape>
            <solid android:color="#FA2B18" />
        </shape>
    </item>
​
    <item android:bottom="100dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/center" />
    </item>
​
    <item android:bottom="30dp">
        <bitmap
            android:gravity="bottom"
            android:src="@drawable/bottom" />
    </item>
​
</layer-list>

效果

使用.9图

将完整图launch_image.png片放入drawable-xhdpi,点击右键,弹窗选择Create 9-Path file,创建launch_image.9.png,然后删除旧的图片,只保留.9图。

绘制原则

  1. 上边左右各绘制1px,距离边缘至少1px
  2. 左边上下绘制200px,距离边缘至少1px(取200px的原因:当屏幕显示导航栏时,图片显示的高度变小,可以上下减小图片高度,防止变形)

直接修改styles.xml主题中的背景使用.9图

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off -->
    <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             the Flutter engine draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_image</item>
    </style>
</resources>

效果

相关推荐
你听得到112 小时前
从需求到封装:手把手带你打造一个高复用、可定制的Flutter日期选择器
前端·flutter
哲科软件12 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin
天涯海风12 小时前
Kuikly 与 Flutter 的全面对比分析,结合技术架构、性能、开发体验等核心维度
flutter·kuikly
aiprtem12 小时前
基于Flutter的web登录设计
前端·flutter
coder_pig15 小时前
跟🤡杰哥一起学Flutter (三十四、玩转Flutter手势✋)
前端·flutter·harmonyos
程序员老刘18 小时前
Android 16开发者全解读
android·flutter·客户端
Jalor18 小时前
Flutter + 鸿蒙 | Flutter 跳转鸿蒙原生界面
flutter·harmonyos
吴Wu涛涛涛涛涛Tao21 小时前
一步到位:用 Very Good CLI × Bloc × go_router 打好 Flutter 工程地基
flutter·ios
九丝城主21 小时前
2025使用VM虚拟机安装配置Macos苹果系统下Flutter开发环境保姆级教程--中篇
服务器·flutter·macos·vmware
ITfeib21 小时前
Flutter
开发语言·javascript·flutter