You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter APK安装后首次启动仅显示启动屏背景问题求助

解决方案

1. 移除透明窗口属性

你的styles.xml中LaunchTheme设置的android:windowIsTranslucent="true"会导致Android系统首次启动时无法正确渲染windowBackground中的图片资源,后续启动因资源已缓存才显示正常。

修改styles.xml的LaunchTheme,删除该属性并调整相关配置:

<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@drawable/launch_background</item>
    <item name="android:forceDarkAllowed">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

2. 禁止APK打包压缩启动屏图片

APK打包时的资源压缩可能导致首次启动无法快速加载图片,在android/app/build.gradle中添加不压缩规则:

android {
    // ...其他配置
    aaptOptions {
        // 精准排除启动屏相关图片
        noCompress 'logo-main.png', 'logo-branding.png'
    }
}

3. 彻底清理并重新生成原生启动屏

确保配置生效,执行完整的清理与生成流程:

flutter clean
rm -rf android/app/src/main/res/drawable/launch_background.xml
rm -rf android/app/src/main/res/drawable-v21/launch_background.xml
rm -rf android/app/src/main/res/drawable-v31/launch_background.xml
flutter pub get
dart run flutter_native_splash:create --path=native_splash.yaml
flutter build apk

4. 优化启动屏保留逻辑

替换固定延迟,改为等待必要初始化完成后再移除启动屏,提升可靠性:

void main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  
  // 等待应用初始化操作完成(如SharedPreferences、网络配置等)
  await Future.wait([
    // SharedPreferences.getInstance(),
  ]);
  
  FlutterNativeSplash.remove();
  runApp(const MyApp());
}

验证流程

  1. 卸载设备上的现有应用
  2. 执行flutter build apk生成新安装包
  3. 通过APK安装到设备,查看首次启动的启动屏显示效果

内容的提问来源于stack exchange,提问作者I. Antonov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:53:23