简体   繁体   English

如何在 flutter 中加载主屏幕时等待启动画面

[英]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.

相关问题 如何在颤动的初始屏幕中与主屏幕进行比较 - How to compare to home screen in splash screen on flutter Flutter 如何仅在加载 flutter 数据时显示启动画面 - Flutter How to show splash screen only while loading flutter data 启动画面到主屏幕导航 - Flutter - Splash screen to Home screen navigation - Flutter 如何在 Flutter 应用程序加载时显示一些加载屏幕或启动画面 - How can i show some loading screen or splash screen while flutter application loads up 如何在 flutter 中第二次从初始屏幕导航到主页? - How to navigate to home from splash screen in second time in flutter? flutter 中,如何去掉原生启动画面和主画面之间的默认淡入淡出过渡? (仅使用本机启动画面) - How to remove the default fade transition between the native splash screen and the home screen in flutter? (Using native splash screen only) 身份验证加载时如何显示颤动的原生闪屏? - How to show flutter native splash screen when authentication loading? 在 flutter 中登录应用程序期间加载主屏幕时的 CircularprogressIndicator - CircularprogressIndicator while loading the home screen during logging in app in flutter 如何在 flutter 中添加启动画面 - how to add Splash Screen in flutter 飞溅/加载屏幕的最佳方式 - Flutter best way for splash / loading screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM