Flutter Splash Screen

 

Flutter Splash Screen

A splash screen is a launch screen, start screen, or boot screen, which is a graphical control element containing the image, logo, and current version of the software. It is the first screen of the app that displays whenever the application is loading. It can also be the app's welcome screen that provides a simple initial experience when a mobile game or program is launching. The splash screen is just a display screen that allows users to look something while the hardware is loading to present software to the user.

The common elements of a splash screen contain a company name and logo or a title. The most common example of a splash screen is the Flutter logo on starting the Flutter application or Microsoft logo while starting the Microsoft operating system. In this tutorial, we are going to see how a splash screen is created in Flutter application.

Splash Screen Characteristics

The following are the essential characteristics of the splash screen:

  • It is mainly used for branding or identity recognition of the application and puts the branding impression to users.
  • It can also be used to show some loading progress indicator while the hardware is loading to present software to the user.
  • When the loading of the splash screen completes, the user gets another functional screen that would be home screen or dashboard, then is forgotten. Once the loading completes, we cannot press the back button to return the splash screen.

Here we are going to explain two methods to add a splash screen in our application.

Method 1: In the first method, we will implement the Timer() function to create a splash screen in our app.

First, create a new project in the IDE you are using. Open the project, navigate to the lib folder, and replace the below code with the main.dart file.

  1. import 'dart:async';  
  2. import 'package:flutter/material.dart';  
  3.   
  4. void main() { runApp(MyApp());}  
  5.   
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       home: MyHomePage(),  
  11.       debugShowCheckedModeBanner: false,  
  12.     );  
  13.   }  
  14. }  
  15.   
  16. class MyHomePage extends StatefulWidget {  
  17.   @override  
  18.   SplashScreenState createState() => SplashScreenState();  
  19. }  
  20. class SplashScreenState extends State<MyHomePage> {  
  21.   @override  
  22.   void initState() {  
  23.     super.initState();  
  24.     Timer(Duration(seconds: 5),  
  25.             ()=>Navigator.pushReplacement(context,  
  26.             MaterialPageRoute(builder:  
  27.                 (context) => HomeScreen()  
  28.             )  
  29.          )  
  30.     );  
  31.   }  
  32.   @override  
  33.   Widget build(BuildContext context) {  
  34.     return Container(  
  35.         color: Colors.yellow,  
  36.         child:FlutterLogo(size:MediaQuery.of(context).size.height)  
  37.     );  
  38.   }  
  39. }  
  40. class HomeScreen extends StatelessWidget {  
  41.   @override  
  42.   Widget build(BuildContext context) {  
  43.     return Scaffold(  
  44.       appBar: AppBar(title:Text("Splash Screen Example")),  
  45.       body: Center(  
  46.           child:Text("Welcome to Home Page",  
  47.               style: TextStyle( color: Colors.black, fontSize: 30)  
  48.           )  
  49.       ),  
  50.     );  
  51.   }  
  52. }  

In the above code, we have a method initState() called once when the stateful widget is inserted in the widget tree. This method first called the super.initState() and then the Timer function. The timer function contains two arguments where the first is duration, and the second is action to be performed. We have specified duration time five seconds, and after completing the time, the current screen will be replaced with the main screen of the app, i.e., HomeScreen().

Output:

When we open the app, we will see the flutter logo first for 5 seconds, shown in the below screenshot.

Flutter Splash Screen

After completing the 5 seconds, it will display our application's home screen shown in the below screenshot.

Flutter Splash Screen

Method 2: In the second method, we will use a splashscreen package to create a splash screen in our app. This package provides many attributes that are given below:

  1. SplashScreen ({ Color loaderColor,  
  2. int seconds,   
  3. double photoSize,   
  4. Image image,   
  5. Text loadingText,   
  6. Color backgroundColor,  
  7. Text title,   
  8. TextStyle styleTextUnderTheLoader,   
  9. dynamic onClick,   
  10. dynamic navigateAfterSeconds,   
  11. ImageProvider<dynamic> imageBackground,   
  12. Gradient gradientBackground})  

First, create a new project in the IDE you are using. Open the project, navigate to the lib folder, and open the pubspec.yaml file where we need to add the splashscreen dependency as below:

Flutter Splash Screen

Now, open the main.dart file and replace it with the below code.

  1. import 'dart:async';  
  2. import 'package:flutter/material.dart';  
  3. import 'package:splashscreen/splashscreen.dart';  
  4.   
  5. void main() { runApp(MyApp()); }  
  6.   
  7. class MyApp extends StatelessWidget {  
  8.   @override  
  9.   Widget build(BuildContext context) {  
  10.     return MaterialApp(  
  11.       theme: ThemeData(  
  12.         primarySwatch: Colors.green,  
  13.       ),  
  14.       home: SplashScreenPage(),  
  15.       debugShowCheckedModeBanner: false,  
  16.     );  
  17.   }  
  18. }  
  19. class SplashScreenPage extends StatelessWidget {  
  20.   @override  
  21.   Widget build(BuildContext context) {  
  22.     return SplashScreen(  
  23.       seconds: 5,  
  24.       navigateAfterSeconds: new HomeScreen(),  
  25.       backgroundColor: Colors.yellow,  
  26.       title: new Text('Flutter Javatpoint',textScaleFactor: 2,),  
  27.       image: new Image.network(  
  28.           'https://static.javatpoint.com/tutorial/flutter/images/flutter-creating-android-platform-specific-code3.png'  
  29.       ),  
  30.       loadingText: Text("Loading"),  
  31.       photoSize: 150.0,  
  32.       loaderColor: Colors.red,  
  33.     );  
  34.   }  
  35. }  
  36. class HomeScreen extends StatelessWidget {  
  37.   @override  
  38.   Widget build(BuildContext context) {  
  39.     return Scaffold(  
  40.       appBar: AppBar(title:Text("Splash Screen Example")),  
  41.       body: Center(  
  42.           child:Text("Welcome to Home Page",  
  43.               style: TextStyle( color: Colors.black, fontSize: 30)  
  44.           )  
  45.       ),  
  46.     );  
  47.   }  
  48. }  

In the above code, we have a home page as SplashScreenPage() that will return the SplashScreen class. This class has several properties for displaying the splash screen, such as title, image, backgroundcolor, gradientBackground, seconds, loadingText, etc. The second property is used for how much time the splash screen appears to the user, and after completion, it will navigate to a new screen, i.e., HomeScreen() in our app.

Output:

When we open the app, we will see the image and loading icon first for 5 seconds, shown in the below screenshot. When the specified time completes, we will navigate to the main page of the application.

Flutter Splash Screen


Post a Comment

Previous Post Next Post