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

Flutter Android端自定义完整原生启动页实现疑问

Flutter Android端自定义完整原生启动页实现疑问

嘿,我猜你是在折腾Flutter Android端的自定义原生启动页吧?看你贴的代码没写完,应该是卡在Android 12及以上版本的Splash Screen API使用上了,我来给你捋捋完整的实现步骤和注意点~

一、先满足基础配置要求

首先得确保你的Android项目适配Android 12+,也就是在android/app/build.gradle里把这两个版本号设为31及以上:

android {
    compileSdkVersion 33
    defaultConfig {
        targetSdkVersion 33
        // 其他配置...
    }
}

这是使用新Splash Screen API的前提,低版本的话就得用旧的兼容方案啦

二、完善启动页主题样式

你贴的LaunchTheme没写完,完整的配置应该包含背景色、启动图标这些核心项,把代码补全成这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <!-- 启动页的背景色,对应你项目里的颜色资源 -->
        <item name="android:windowSplashScreenBackground">@color/ic_launcher_background</item>
        <!-- 启动页显示的图标,建议放在mipmap文件夹适配不同分辨率 -->
        <item name="android:windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
        <!-- 图标的动画时长,可选,默认1000毫秒 -->
        <item name="android:windowSplashScreenAnimationDuration">1000</item>
        <!-- 按需设置是否全屏,隐藏状态栏和导航栏 -->
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>

三、给主Activity绑定启动主题

打开AndroidManifest.xml,找到你的主Activity(一般是.MainActivity),把android:theme指定为刚才的LaunchTheme:

<activity
    android:name=".MainActivity"
    android:exported="true"
    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>
</activity>

四、Flutter端配合避免闪屏

为了防止启动后出现白屏或者主题切换突兀,在main.dart里要等Flutter初始化完成后再切换到App的主题:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() async {
  // 确保Flutter绑定初始化完成
  WidgetsFlutterBinding.ensureInitialized();
  // 锁定竖屏(按需设置)
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    // 这里切换到你的App主题
    return MaterialApp(
      title: '你的App名称',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('首页')),
      body: const Center(child: Text('Hello World')),
    );
  }
}

五、额外注意点

  • 如果需要自定义更复杂的启动页(比如带文字、特殊布局),Android 12+支持用android:windowSplashScreenContent指定自定义布局,但这个布局会覆盖默认的图标,要做好屏幕适配。
  • 针对Android 11及以下的旧版本,可以在LaunchTheme里添加android:windowBackground来设置静态背景图,做向下兼容。
  • 如果启动后还是有闪屏,大概率是Flutter初始化耗时较长,可以考虑优化App的初始化逻辑,或者在启动页里加个过渡动画。

备注:内容来源于stack exchange,提问作者Apri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 17:44:31