Get current shell route inside builder in go_router flutter

475 views Asked by At

I'm trying to not show a bottom navigation on certain routes, but I can't get the real current route on StatefulShellRoute builder.

This is my router:

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(),
              ),
            ],
          ),
         ...
        ]),
  ],
);

I'm trying to get the current route on the builder of my StatefulShellRoute.indexedStack. But it always returns the initial route path. If I go to a product page, it always says the matchedLocation is /home, instead of /product/:productId

I tried getting the current route inside the builder by using:

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

But it always is '/home', even when I just went to '/product' page. I need to pass a parameter to my scaffold to hide my navigator only on 'product' route. How could I achieve that then?

0

There are 0 answers