应用检查登录状态时如何设置加载屏幕?

我使用SharedPreferences保持登录状态。效果很好。

但是在我打开应用程序后关闭它。它显示了我所有的屏幕,例如flash屏幕,然后停留在其右侧屏幕上。

但是我不要这个,这也不好。

我希望当应用检查登录状态时,它显示一个加载屏幕或完成显示应用右屏幕后的所有内容。

我该怎么做?

这是我的登录状态检查码

 checkLoginStatus() async {
    sharedPreferences = await SharedPreferences.getInstance();

if (sharedPreferences.getString("empid") == null) {
  Navigator.of(context).pushAndRemoveUntil(
      MaterialPageRoute(builder: (BuildContext context) => Home()),(Route<dynamic> route) => false);
   }
}
gaoyu5945 回答:应用检查登录状态时如何设置加载屏幕?

我个人喜欢使用变量 _isLoading ,该变量在登录方法开始时使用 setState 设置为 true 。然后使用此布尔值,只需更改脚手架中显示的内容即可。

bool _isLoading = false;

checkLoginStatus() async {
    setState() => _isLoading = true;
    sharedPreferences = await SharedPreferences.getInstance();

    if (sharedPreferences.getString("empid") == null) {
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(builder: (BuildContext context) => Home()),(Route<dynamic> route) => false);
    } else {
        setState() => _isLoading = false;
    }
}

编辑:您的情况可能更相关:

@override
  Widget build(BuildContext context) {
    switch (currentUser.status) {
      case AuthStatus.notSignedIn:
        return LoginPage();
      case AuthStatus.signedIn:
        return HomePage();
      default:
        return LoadingPage();
    }
  }

我的 currentUser 只是我创建的一个类,其中包含一个枚举 AuthStatus ,其值可以为 notSignedIn signedIn 。如果在加载时将状态设置为null,则可以显示加载屏幕。

,

代码段

void main async {

bool isUserLogin = await User.isUserLogin();
  if (isUserLogin) { // wait untill user details load
    await User.currentUser.loadPastUserDetails();
  }

  runApp(MyApp(isUserLogin));

}

class MyApp extends StatelessWidget {

  bool isUserLogin;
  MyApp(this.isUserLogin);

  @override
  Widget build(BuildContext context) {
    var defaultRoot = isUserLogin ? HomePage() : LoginScreen();

    final material =  MaterialApp(
      debugShowCheckedModeBanner: false,home: defaultRoot,);

    return material;
  }
}

希望这会有所帮助!

本文链接:https://www.f2er.com/3160579.html

大家都在问