如何在Flutter中展示启动画面?有一个选项用于启动图标。我在它们各自的文件夹中添加了iOS和Android的图片。我的问题是它太快了。所以它直接打开MyApp()。我希望我的应用程序不显示任何内容,让启动画面控制,直到我想好要把用户带到哪个屏幕(在MyApp中,我想做初始化)。
如何在Flutter中展示启动画面?有一个选项用于启动图标。我在它们各自的文件夹中添加了iOS和Android的图片。我的问题是它太快了。所以它直接打开MyApp()。我希望我的应用程序不显示任何内容,让启动画面控制,直到我想好要把用户带到哪个屏幕(在MyApp中,我想做初始化)。
您可以在initState
中使用Future.delayed
构造函数。这将使您的启动屏幕在导航发生之前保持指定的持续时间。
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState (){
super.initState();
// TODO initial state stuff
new Future.delayed(const Duration(seconds: 4));
}
@override
Widget build(BuildContext context) {
//build
}
}
Future.wait
结合使用,以等待必要资源的加载。 - Rémi RousseletrunApp
被调用时,启动画面就会结束,而且据我所知,Future.delayed
等同于Timer
。 - arielnmz对Aziza的回答进行小更新
import 'dart:async';
import 'package:flutter/material.dart';
import 'src/login_screen.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
new Future.delayed(
const Duration(seconds: 3),
() => Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white,
body: Container(
child: new Column(children: <Widget>[
Divider(
height: 240.0,
color: Colors.white,
),
new Image.asset(
'assets/logo.png',
fit: BoxFit.cover,
repeat: ImageRepeat.noRepeat,
width: 170.0,
),
Divider(
height: 105.2,
color: Colors.white,
),
]),
),
);
}
}