I am a beginner in Flutter, currently developing an application. I would like to know how to remove the default Flutter splash screen that appears when the application launches. Instead, I want to directly display my custom splash screen using the Timer class in Flutter to create a delay before moving to the next page.
Here’s my current main.dart code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:splash_screen_flutter/secondPage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.indigo,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
//final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void initState() {
// TODO: implement initState
super.initState();
startSplashScreenTimer(); // calling startSplashScreenTimer method,to start the timer
}
startSplashScreenTimer() async { // Because we using Timer and it is a Future Object, we used async keyword
var _duration = new Duration(seconds: 5);
return new Timer(_duration, navigationToNextPage);
/*
1-Create a variable type Duration, and set Duration to 5 seconds,
2-Create a Timer here, which takes two arguments, first duration,
and callback which is in our case is navigationToNextPage which will be called after the duration {5 seconds here },
Note: we have to import 'dart:async' so we can use Timer class
*/
}
void navigationToNextPage() async {
Navigator.pushAndRemoveUntil(
context, new MaterialPageRoute(builder: (context) =>
new secondPage()),
(Route<dynamic> route) => false,//
);
/*
1- use Navigator to switch between routes or activities in Flutter app,
2- Navigator.push : to push to new route or activity
3- we called Navigator.pushAndRemoveUntil to pus to the next Route,which is in this case 'secondPage'
Note: we used pushAndRemoveUntil to push the given route onto navigator and remove all the previous routes from stack or
until the predicate returns true.
*/
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.indigo[300],
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
//crossAxisAlignment: CrossAxisAlignment.stretch,
children:[
SizedBox(height: 100,),
CircleAvatar(
radius: 70,
backgroundColor: Colors.white,
child: Text('Splash 😍', style:TextStyle(color: Colors.black, fontSize: 20)),
),
SizedBox(height: 50,),
CircularProgressIndicator(
backgroundColor: Colors.pink[300],
),
Container(
padding: EdgeInsets.only(top: 20, bottom: 50),
child: Text('Splash Screen Example', style: TextStyle(color: Colors.amber, fontSize: 25),))
]
),
),
);
}
}
I have tried making some modifications using the Timer class, but the default splash screen continues to show. I was wondering if there is a specific solution for Flutter beginners to remove this splash screen and directly display my custom splash screen using the Timer class to create a delay before moving to the next page.
Thank you for your help and advice!
3
Answers
Thank you for your response. I tried using the "animated_splash_screen: ^1.3.0" dependency as you suggested, but unfortunately, I'm still facing the same issue with the appearance of the default Flutter splash screen. I'm seeking assistance to find a solution that allows me to display my own custom splash screen.
If you have any additional ideas or recommendations on how to properly configure the "animated_splash_screen" package to work with my application, I would greatly appreciate it if you could share them with me.
Thank you in advance for your help! this is my new code :
The simple answer is you can’t; Flutter needs a period ( when the app starts for the first time ) to run its engine so that you will face a white screen. You can replace it for an Android build with this or a web build by changing the web/index.html file.
The solution you are trying to implement cannot work since the first splash screen is native and not controlled by Flutter (since it is shown before Flutter renders its first screen/while Flutter is loading).
Check out this package: https://pub.dev/packages/flutter_native_splash . It should solve your issue. You can also use it to keep up the splash screen if your app needs to do some additional loading before you want to show anything.