我个人喜欢使用变量 _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