颤动立即导航到屏幕 [英] Flutter navigate to screen without delay

查看:153
本文介绍了颤动立即导航到屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录屏幕,在我的表单按钮中,我进行了检查,然后调用Shared Preferences来设置登录标志,而initState我调用了一种方法来检查该值并重定向到homeScreen.现在一切顺利,但是我能够看到登录表单大约一秒钟,然后我被重定向到首页.我希望完全看不到登录表单.我怎样才能做到这一点? 这是initState方法的内容:

I have a login screen, in my form button, i do the check and then call Shared Preferences to set the login flag, and initState i call a method to check the value and redirect to homeScreen. Now all went well, but i am able to see the login form for a second or so, then i get redirected to home. I wish to not see the login form at all. How can i do that? Here is the content of initState method:

_loadLoginStatus() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      _loginStatus =prefs.getBool('loginStatus') ?? false;
      if(_loginStatus == true){
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => HomeScreen()));
      }
    });
  }

推荐答案

请勿将登录屏幕设置为AppWidget上的主屏幕.

Don't setup your login screen as a home screen on your AppWidget.

代替此操作,创建一个LoadingScreen或SplashScreen并在此处检查登录状态.

Instead of this create a LoadingScreen or SplashScreen and check login status here.

class LoadingScreen extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<LoadingScreen> {

  @override
  Widget build(BuildContext context) {
    _checkLogin(context);
    return Scaffold(body: Center(child: CircularProgressIndicator()));
  }

  _checkLogin(BuildContext context) async {
    // do what you want with await functions
    if (condition) {
      Navigator.push(
            context, MaterialPageRoute(builder: (context) => HomeScreen()));
    } else {
      Navigator.push(
            context, MaterialPageRoute(builder: (context) => LoginScreen()));
    }
  }
}

这篇关于颤动立即导航到屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆