Go_Router包中,如何隐藏StatefulShellRoute.indexedStack?我尝试使用钥匙,但我就是无法让它工作

问题描述 投票:0回答:2

我有一个名为“clubview”的页面,位于 StatefulShellRoute 内。当用户位于此页面时,他可以导航到另一个名为“createClub”的页面。

我需要一种方法来随时显示 BottomNavigationBar,在这种情况下,我希望它在用户位于“clubview”中时显示,而不是在他们按下“createClub”时显示。如果我使用 GoNamed 我可以让它工作,但是我不能通过执行 pop() 返回。那么,有没有办法在使用pushNamed时仅在某些路线中显示BottomNavigationBar?

我的代码看起来部分像这样:

注意:我什至可以使“createClub”成为“clubview”的子路线,但即使如此我也无法使其工作。尝试在网络上搜索,但有关 StatefulShellRoute.indexStack 的资源非常有限

      GoRouter(
        navigatorKey: _rootNavigatorKey,
        initialLocation: initialLocation,
        refreshListenable: GoRouterRefreshStream(authBloc.stream),
        redirect: (context, state) {
          if (authBloc.state is AuthUnauthenticatedState) {
            if (state.location == Routes.login.path ||
                state.location == Routes.register.path ||
                state.location.startsWith(Routes.passwordreset.path)) {
              return null;
            } else if (state.location == Routes.verifyemail.path ||
                state.location == Routes.chooseName.path) {
              return Routes.register.path;
            } else {
              return Routes.login.path;
            }
          } else if (authBloc.state is AuthChooseNameState &&
              state.location != Routes.chooseName.path) {
            return Routes.chooseName.path;
          } else if (authBloc.state is AuthVerifyEmaildState &&
              state.location != Routes.verifyemail.path) {
            return Routes.verifyemail.path;
          } else if (authBloc.state is AuthAuthenticatedState &&
              (state.location == Routes.login.path ||
                  state.location == Routes.register.path ||
                  state.location == Routes.chooseName.path ||
                  state.location == Routes.verifyemail.path)) {
            return Routes.clubview.path;
          } else if (authBloc.state is ColdStartLogin &&
              (state.location == Routes.login.path ||
                  state.location == Routes.register.path ||
                  state.location == Routes.chooseName.path ||
                  state.location == Routes.verifyemail.path)) {
            return Routes.clubview.path;
          } else {
            //if (state.location == Routes.createClub.path ||
            //    state.location == Routes.mediaPicker.path) {
            //  BlocProvider.of<InstantFadecubit>(context).hideBottomBar();
            //} else {
            //  BlocProvider.of<InstantFadecubit>(context).showBottomBar();
            //}
            return null;
          }
        },
        routes: [
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.home.name,
            path: Routes.home.path,
            builder: (context, state) => const HomePage(),
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.login.name,
            path: Routes.login.path,
            builder: (context, state) => const LoginPage(),
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.register.name,
            path: Routes.register.path,
            builder: (context, state) => const RegisterPage(),
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.chooseName.name,
            path: Routes.chooseName.path,
            builder: (context, state) => const ChooseName(),
          ),
          StatefulShellRoute.indexedStack(
            //parentNavigatorKey: _rootNavigatorKey,
            builder: (context, state, navigationShell) {
              return DashboardScreen(key: state.pageKey, child: navigationShell);
            },
            branches: <StatefulShellBranch>[
              StatefulShellBranch(
                //navigatorKey: _shellNavigatorKey,
                routes: <RouteBase>[
                  GoRoute(
                      name: Routes.clubview.name,
                      path: Routes.clubview.path,
                      builder: (context, state) => const ClubView()),
                ],
              ),
              StatefulShellBranch(
                //navigatorKey: _shellNavigatorKey,
                routes: <RouteBase>[
                  GoRoute(
                    name: Routes.searchEngine.name,
                    path: Routes.searchEngine.path,
                    builder: (context, state) => const SearchEngine(),
                  ),
                ],
              ),
              StatefulShellBranch(
                //navigatorKey: _shellNavigatorKey,
                routes: <RouteBase>[
                  GoRoute(
                    name: Routes.mapa.name,
                    path: Routes.mapa.path,
                    builder: (context, state) => const Map1(),
                  ),
                ],
              ),
              StatefulShellBranch(
                //navigatorKey: _shellNavigatorKey,
                routes: <RouteBase>[
                  GoRoute(
                    name: Routes.profile.name,
                    path: Routes.profile.path,
                    builder: (context, state) => const Profile(),
                  ),
                ],
              ),
            ],
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.verifyemail.name,
            path: Routes.verifyemail.path,
            builder: (context, state) => const VerifyEmail(),
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            path: '${Routes.passwordreset.path}/:email',
            name: Routes.passwordreset.name,
            builder: (context, state) => PasswordReset(
              email: state.pathParameters['email']!,
            ),
          ),
          GoRoute(
              //parentNavigatorKey: _rootNavigatorKey,
              path: Routes.passwordreset.path,
              name: '${Routes.passwordreset.name}null',
              builder: (context, state) => const PasswordReset()),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            path: Routes.clubpage.path,
            name: Routes.clubpage.name,
            builder: (context, state) {
              if (state.extra is ClubOverview) {
                final club = state.extra as ClubOverview;
                return ClubPage(club: club);
              } else {
                final club =
                    ClubOverview.fromMap(state.extra as Map<String, dynamic>);
                return ClubPage(club: club);
              }
            },
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.createClub.name,
            path: Routes.createClub.path,
            pageBuilder: (context, state) {
              return CustomTransitionPage(
                key: state.pageKey,
                transitionDuration: const Duration(milliseconds: 300),
                reverseTransitionDuration: const Duration(milliseconds: 300),
                child: const ClubForm(),
                transitionsBuilder:
                    (context, animation, secondaryAnimation, child) {
                  return Transform.scale(
                    scale: Tween<double>(begin: 0, end: 1).evaluate(animation),
                    child: child,
                  );
                },
              );
            },
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.createPost.name,
            path: Routes.createPost.path,
            builder: (context, state) {
              final club = state.extra as ClubProfile;
              return CreatePost(clubProfile: club);
            },
          ),
          GoRoute(
            //parentNavigatorKey: _rootNavigatorKey,
            name: Routes.mediaPicker.name,
            path: Routes.mediaPicker.path,
            builder: (context, state) {
              final Map<String, dynamic> extraData =
                  state.extra as Map<String, dynamic>;
              // Extract any necessary parameters from the state.extra if needed
              final maxCount = extraData['maxCount'] as int;
              final requestType = extraData['requestType'] as RequestType;
              return ImageMediaPicker(
                  maxCount: maxCount, requestType: requestType);
            },
          ),
        ],
      );

使用 go_router StatefulShellRoute.indexStack 根据路线显示和隐藏 BottomNavigationBar,同时还使用 Pushnamed 导航路线

flutter flutter-go-router
2个回答
0
投票

为createClub路线添加_rootNavigatorKey


0
投票

在仪表板屏幕中检查最上面的路线名称,

router.routerDelegate.currentConfiguration.last.matchedLocation
.

将上面的名称与您要排除的路线名称相匹配,并有条件地显示/隐藏底部导航栏。

© www.soinside.com 2019 - 2024. All rights reserved.