[英]How to await splash screen while loading a home screen in flutter
my homepage is taking sometime to load all asset, so I want to add a splash screen there, but it should wait for 3 second (the time that take load home screen), so I wanna cover the loading section with splash screen我的主页需要一些时间来加载所有资产,所以我想在那里添加一个启动画面,但它应该等待 3 秒(加载主屏幕的时间),所以我想用启动画面覆盖加载部分
Use this packages.使用这个包。 https://pub.dev/packages/flutter_native_splash This will help you.
https://pub.dev/packages/flutter_native_splash这将对您有所帮助。 This splash screen will wait until your homescreen or whatever you set is loading
此初始屏幕将等到您的主屏幕或您设置的任何内容正在加载
There is package that does exactly that, I use it for real world projet, the setup is easy: https://pub.dev/packages/flutter_native_splash package 正是这样做的,我将它用于现实世界的项目,设置很简单: https://pub.dev/packages/flutter_native_splash
Do a hard restart and test out, It will wait before your app is loaded and then the splash screen will disappear硬重启并测试一下,它会在你的应用程序加载之前等待,然后启动屏幕会消失
You can add a image as a splashscreen using this method,您可以使用此方法将图像添加为启动画面,
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
Timer(
Duration(seconds: 5),
() => Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Home())));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Color.fromRGBO(20, 172, 168, 1),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 2,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.white,
radius: 60.0,
child: new Image.asset(
'assets/images/tree.jpg',
width: 70,
height: 90,
)),
Padding(
padding: EdgeInsets.only(top: 10.0),
),
Text(
'Your Text here!!',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24.0),
)
],
),
),
),
Expanded(
flex: 1,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
Padding(
padding: EdgeInsets.only(top: 20.0),
),
Text(
'Your Text here!!',
softWrap: true,
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0,
color: Colors.white),
)
],
),
)
],
)
],
),
);
}
}
And if you want to increase or decrease the time span then you can change this如果你想增加或减少时间跨度,那么你可以改变这个
Timer(const Duration(seconds: 3).
Use you own custom image of splashscreen or design new one here.increase or decrease time but less time is better使用您自己的启动画面自定义图像或在此处设计新图像。增加或减少时间,但时间越短越好
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
startTime();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
Size size = MediaQuery.of(context).size;
return SafeArea(
child: Scaffold(
body: new Stack(
alignment: Alignment.center,
children: <Widget>[
Center(
child: new Image.asset(
'assets/images/splash.png',
width: size.width,
height: size.height,
fit: BoxFit.fill,
),
),
],
),
),
);
}
startTime() async {
var _duration = new Duration(seconds: 5);
return new Timer(_duration, navigationPage);
}
void navigationPage() {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => DashBoardScreen()),
ModalRoute.withName("/login"));
}
}
You can also have a minimal Flutter app running while you await the more expensive initialization.您还可以在等待更昂贵的初始化时运行最小的 Flutter 应用程序。 Just call runApp twice!
只需调用 runApp 两次!
void main () async {
runApp(SomethingAnimated());
await longTaskOne();
await longTaskTwo();
await longTaskThree();
runApp(MyRealTopLevel();
}
Yes this is perfectly legal and documented.是的,这是完全合法且有文件记录的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.