Splash Screen (or Launch Screen on iOS) is the very first page of your app seen by users. So it would be great to make a good first impression. I would like to present you a few ways to create a splash screen in your Flutter application using native solutions or Flutter package. Our goal is to create splash screens as shown below (from left: iOS 15, Android 12, Android 8):
We are able to set a splash screen for iOS in two ways. The first one is by using Storyboard.
open ios/Runner.xcworkspace
command.TIP 1: If you can’t see changes to your splash screen, try to delete the app from the phone/simulator, restart the phone/simulator, go to Xcode→Product→Clean Build Folder. That should solve the issue.
The second approach uses Info.plist file (Information Property List).
For Android, we also need our icon image in different resolutions for different devices. In this case, place these images in directories called drawable with different suffixes. If you are not familiar with drawables, I highly recommend reading that article explaining the topic in detail.
To generate all required resolutions of images, I have used the website https://www.img-bak.in/ (but this one is also cool http://romannurik.github.io/AndroidAssetStudio/index.html). Remember to provide high-resolution image as an input, e.g. if you want to use an image of 200x200 pixels in your app, you should provide an image of 800x800 pixels because that is the highest resolution used by some Android devices (drawable-xxxhdpi).
Android/app/src/main/res
. Android/app/src/main/res/drawable/launch_background.xml
file. This file will represent the look of our splash screen. If you also have a drawable-v21
directory present, which in my case, was generated together with the whole project, you can remove the mentioned directory or place there the same launch_background.xml
file as in the drawable
directory. launch_background.xml
as a splash screen. To do that, open Android/app/src/main/res/values/styles.xml
and under LaunchTheme
resource set android:windowBackground
as in the snippet below.
If you want to have a different splash screen in dark mode/theme, you have to provide a different file e.g.launch_background_dark.xml
in values-night/styles.xml
.
TIP 2: If you can’t see an icon on your splash screen after building the app, just close the app and open it directly from the device.
Now, if we launch the app on Android 11 or lower, we will see our splash screen, however, on Android 12 and higher, it won’t work in the same way. Android 12 brings some big changes to splash screens. The two most important changes are:
More information on that here.
Ok, so let’s modify our splash screen for Android 12.
values-v31
. v31
means that this directory will be used instead of the sheer values
directory for all Android devices with SDK 31 and higher. Then, place here styles.xml
file presented below. Most of the code looks identical to the previous one.android12splash
instead of logo
(which was used earlier in launch_background.xml
file as our icon.)As documentation says:
To generate all required icons, we can use one of the online tools presented earlier.
For example, first - an icon with wrong dimensions:
and second - with proper ones:
flutternativesplash is a fast way to create a splash screen for both iOS and Android apps. It has some limitations, but in most cases, it is sufficient.
pubspec.yaml
file under dev_dependencies
(or under dependencies
sometimes 😉`). I am using version 2.1.0.Most important lines:
flutter_native_splash.yaml
file. flutter pub run flutter_native_splash:create --path=flutter_native_splash.yaml
. And that is all! One more thing to explain, what is the reason to place the flutter_native_splash
package under dependencies
? With the version 2.0.3+1
the package allows us to select the time when the splash screen should disappear. So now we are able to initialize everything we need to start the app and then show our first screen. Previously, when the app was ready to render the first frame of the app, the native splash screen (so that one we were creating) disappeared and there was a need to create a special page being our splash screen until the whole app was initialized.
So to keep displaying the native splash screen, we need to write two simple lines of code 7-8:
After that, the splash screen will be visible until we call FlutterNativeSplash.remove();
.
Let’s check what changes the flutter_native_splash
package makes to our project after running the generate command.
Info.plist - setting a flag to hide status bar during splash screen.
ios/Runner/Assets.xcassets/BrandingImage.imageset/* - directory for setting branding image at the bottom of the splash screen. We didn’t set that in our config file flutter_native_splash.yaml
, but it was generated anyway. So we can remove that whole directory BrandingImage.imageset
.
ios/Runner/Assets.xcassets/BackgroundImage.imageset/background.image - this package uses a storyboard to set a splash screen for iOS. In storyboard, it sets background as an image. So firstly, the package generates an asset image (1x1 pixel) which represents a background color and then sets it as a background image.
ios/Runner/Assets.xcassets/LaunchImage.imageset/* - directory for icon (“logo.png”) automatically generated in different resolutions.
**ios/Runner/Base.lproj/LaunchScreen.storyboard **- as I have mentioned earlier, the package uses storyboard in which icon and background color is being set.
All files named android12splash.png are generated images for different devices with Android 12 and higher placed in appropriate drawable directories.
All files named logo.png are generated images for different devices with Android 11 and lower placed in appropriate **drawable **directories.
background.png - image 1x1 pixel which represents a background color. This file is later used in the launch_background.xml
file to set the background.
**launch_background.xml **- the same file we have been modifying earlier, when setting the splash screen without a package, but with some changes.
In drawable and drawable-v21 directories, we have exactly the same files, so we can remove the latter one if we want.
android/app/src/main/res/values/styles.xml - again, the same file we have been modifying earlier with some differences i.e. added two flags forceDarkAllowed
and windowFullscreen
.
So as you can see, there is no magic. flutter_native_splash
Package does everything that we can do ourselves, but faster 😊.