[英]How can i show some loading screen or splash screen while flutter application loads up
I have been working on an app recently.我最近一直在开发一个应用程序。 I want to check if the user is logged in and is verified when my app loads up.
我想检查用户是否已登录并在我的应用程序加载时进行验证。 So I created a Wrapper class to check if the user is logged in and is verified.
所以我创建了一个 Wrapper 类来检查用户是否已登录并经过验证。 Then accordingly I would show them either login screen or home screen.
然后我会相应地向他们展示登录屏幕或主屏幕。
I have assigned home : Wrapper(), in Main.dart .我在 Main.dart 中分配了 home : Wrapper()。
After that I have wrapper class as之后我有包装类
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
// checking if there is user and the user is verified
bool _isAuth() {
if (user != null && user.isVerified) {
return true;
}
return false;
}
return _isAuth() ? MainScreen() : Authenticate();
}
}
This works fine but the problem is it first flashes the login page and then takes me to the homepage if the user is logged in and is verified but it just works fine if the user is not logged in see gif image here这工作正常,但问题是它首先闪烁登录页面,然后在用户登录并通过验证时将我带到主页,但如果用户未登录它就可以正常工作,请在此处查看 gif 图像
It probably shows the login page because of the way your logic is being handled.由于处理逻辑的方式,它可能会显示登录页面。 you should do this in
initState
instead of the build method.您应该在
initState
而不是 build 方法中执行此操作。 There are two ways to do this you can either use your wrapper as redirection class or use the build method like you're already doing to toggle the view.有两种方法可以做到这一点,你可以使用你的包装器作为重定向类,或者使用你已经在做的构建方法来切换视图。
First Method (uses redirection)第一种方法(使用重定向)
class Wrapper extends StatefulWidget {
@override
_WrapperState createState() => _WrapperState();
}
class _WrapperState extends State<Wrapper> {
@override
void initState() {
super.initState();
final user = Provider.of<User>(context, listen: false);
var _isAuth = user != null && user.isVerified;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => _isAuth ? MainScreen() : Authenticate()),
);
}
@override
Widget build(BuildContext context) {
return CircularProgressIndicator();
}
}
Second Method (uses build method):第二种方法(使用构建方法):
class Wrapper extends StatefulWidget {
@override
_WrapperState createState() => _WrapperState();
}
class _WrapperState extends State<Wrapper> {
bool _isAuth = false;
bool _isLoading = true;
@override
void initState() {
super.initState();
final user = Provider.of<User>(context, listen: false);
setState(() {
_isAuth = user != null && user.isVerified;
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return _isLoading
? CircularProgressIndicator()
: _isAuth
? MainScreen()
: Authenticate();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.