小编sul*_*110的帖子

Flutter:如何将 GoRouter 与 Bloc 结合使用从启动屏幕路由到登录屏幕

所以我最近在我的应用程序中切换到 Go router,因为它非常容易实现。但我在从启动屏幕转移到登录屏幕时遇到问题。我的启动屏幕中有逻辑,我可以检查用户是否登录。根据用户的身份验证,屏幕将转到登录屏幕或主页。

这是启动画面。

    class SplashScreen extends StatefulWidget {
  static const routeName = "/SplashScreen";

  const SplashScreen({Key? key}) : super(key: key);

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<AuthenticationBloc, AuthenticationState>(
      listener: (context, state) {
        if (kDebugMode) {
          print('Listener: $state');
        }
        Future.delayed(const Duration(seconds: 3), () {
          if (state.authStatus == AuthStatus.unAuthenticated) {
            GoRouter.of(context).go('/login');

            Navigator.pushNamed(context, SignUpScreen.routeName);
          } else if (state.authStatus == AuthStatus.authenticated) {
            //Navigator.popUntil(context, (route) => route.isFirst);
            Navigator.pushReplacementNamed(context, HomePage.routeName);
          }
        });
      }, …
Run Code Online (Sandbox Code Playgroud)

splash-screen flutter bloc flutter-go-router

5
推荐指数
1
解决办法
6763
查看次数

标签 统计

bloc ×1

flutter ×1

flutter-go-router ×1

splash-screen ×1