In Flutter, you can create a native splash screen by creating a splash screen Android theme and set the windowBackground
to your splash screen image. Here is an example of how you can do this:
Create a drawable resource for your splash screen image and place it in the
res/drawable
folder of your Android project.In your
res/values/styles.xml
file, create a new style for your splash screen theme:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
- In your
AndroidManifest.xml
file, set theandroid:theme
attribute of the<activity>
element to@style/SplashTheme
:
<activity
android:name=".MainActivity"
android:theme="@style/SplashTheme"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This will display the splash screen image as the background for the MainActivity
when the app is launched. You can then use a FlutterAppDelegate
subclass to switch to the Flutter app's main screen after a short delay.
import io.flutter.app.FlutterActivity
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set up a timer to switch to the Flutter app's main screen after a delay
Handler().postDelayed({
startActivity(Intent(this, FlutterMainActivity::class.java))
finish()
}, SPLASH_SCREEN_DELAY)
}
}