在 go_router flutter 中获取构建器内当前的 shell 路由

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

我试图不在某些路线上显示底部导航,但我无法在 StatefulShellRoute 构建器上获取真实的当前路线。

这是我的路由器:

final appRouter = GoRouter(
  initialLocation: '/',
  navigatorKey: navigatorKey,
  routes: [
    GoRoute(
      path: '/',
      name: 'splash',
      builder: (context, state) => const SplashScreen(),
    ),
    ...
    StatefulShellRoute.indexedStack(
        builder: (BuildContext context, GoRouterState state,
            StatefulNavigationShell navigationShell) {
          return ScaffoldBottomNavigationBar(navigationShell: navigationShell);
        },
        branches: <StatefulShellBranch>[
          StatefulShellBranch(
            routes: <RouteBase>[
              GoRoute(
                path: '/home',
                name: 'home',
                builder: (context, state) => const HomeScreen(),
              ),
              GoRoute(
                path: '/product/:productId',
                name: 'product',
                builder: (context, state) {
                  final id = state.pathParameters['productId']!;
                  return ProductScreen(
                    productId: id,
                  );
                },
              ),
            ],
          ),
          StatefulShellBranch(
            routes: <RouteBase>[
              GoRoute(
                path: '/categories',
                name: 'categories',
                builder: (context, state) => const CategoryScreen(),
              ),
            ],
          ),
         ...
        ]),
  ],
);

我正在尝试获取我的

builder
StatefulShellRoute.indexedStack
上的当前路线。但它总是返回初始路由路径。如果我进入产品页面,它总是说匹配的位置是 /home,而不是 /product/:productId

我尝试使用以下方法获取构建器内的当前路线:

navigationShell.shellRouteContext.routerState.matchedLocation;
state.matchedLocation;

但它始终是“/home”,即使我刚刚进入“/product”页面。我需要将一个参数传递给我的脚手架,以仅在“产品”路线上隐藏我的导航器。那我怎样才能实现呢?

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

在您的 ScaffoldBottomNavigationBar 小部件中检查最上面的路线名称,

appRouter.routerDelegate.currentConfiguration.last.matchedLocation.

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

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