In this tutorial, we will learn how to add a splash screen to Flutter apps? Flutter actually gives a simpler way to add Splash Screen to our application. We first need to design a basic page as we design other app screens. You need to make it a StatefulWidget since the state of this will change in a few seconds.
import 'dart:async'; import 'package:flutter/material.dart'; import 'home.dart'; class SplashScreen extends StatefulWidget { @override _SplashScreenState createState() => _SplashScreenState(); } class _SplashScreenState extends State{ @override void initState() { super.initState(); Timer( Duration(seconds: 3), () => Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (BuildContext context) => HomeScreen()))); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Center( child: Image.asset('assets/splash.png'), ), ); } }
Logic Inside the initState(), call a Timer() with the duration, as you wish, I made it 3 seconds, once done push the navigator to Home Screen of our application.
Note: The application should show the splash screen only once, the user should not go back to it again on back button press. For this, we use Navigator.pushReplacement(), It will move to a new screen and remove the previous screen from the navigation history stack.
Reference:
https://stackoverflow.com/questions/43879103/adding-a-splash-screen-to-flutter-apps
The post How to add a splash screen to Flutter apps? appeared first on FreeWebMentor.
This post first appeared on Programming Blog Focused On Web Technologies, please read the originial post: here