Sub topic 3
Nesting detail pages inside a tab
This chapter assumes you already have a StatefulShellRoute.indexedStack with at least two branches, a rootNavigatorKey passed to GoRouter, and basic context.go usage.
Nesting detail pages inside a tab
Add a detail route to the routes list of the branch's GoRoute. The detail page then lives in that branch's navigator, so the bottom bar stays visible and the tab keeps its own back stack.
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
routes: [
GoRoute(
path: 'details/:id',
builder: (context, state) =>
DetailsScreen(id: state.pathParameters['id']!),
),
],
),
],
)Child paths are relative, so this route matches /home/details/42.
Opening full-screen routes above the tab bar
Set parentNavigatorKey to the root navigator key. The route then opens on the root navigator, above the whole shell.
GoRoute(
path: 'photo',
parentNavigatorKey: rootNavigatorKey,
builder: (context, state) => const PhotoViewerScreen(),
),> callout note: parentNavigatorKey has no effect unless you pass the same key to GoRouter(navigatorKey: rootNavigatorKey).
Redirects and guards around the shell
A redirect set on the top-level GoRouter runs for every location, including routes inside branches. Define one guard function and pass it to redirect.
bool isLoggedIn = false;
String? guard(BuildContext context, GoRouterState state) {
final onLogin = state.matchedLocation == '/login';
if (!isLoggedIn && !onLogin) return '/login';
if (isLoggedIn && onLogin) return '/home';
return null;
}Customizing branch display with navigatorContainerBuilder
By default, go_router places the branch navigators in an indexed stack. Use navigatorContainerBuilder to change how they are laid out. The following version keeps every branch mounted and hides the inactive ones with Offstage.
navigatorContainerBuilder: (context, navigationShell, children) {
return Stack(
children: [
for (var i = 0; i < children.length; i++)
Offstage(
offstage: i != navigationShell.currentIndex,
child: children[i],
),
],
);
},Pitfalls and debugging tips
| Symptom | Likely cause | Fix |
|---|---|---|
| Full-screen page appears under the tab bar | `parentNavigatorKey` missing | Set it to the root key |
| Tab loses its scroll position | Plain `ShellRoute` used | Use `StatefulShellRoute` |
| Same navigator key error at startup | One `GlobalKey` reused | Create a separate key for each navigator |
| Detail page never matches | Parameter name mismatch | Match `:id` with `pathParameters['id']` |
Start with GoRouter(debugLogDiagnostics: true) to log every match and redirect.
Exercise
Exercise
You add a settings/confirm route inside the Settings branch and set parentNavigatorKey: rootNavigatorKey. Where does the confirm page open, and what happens to the bottom bar?
Show answer
It opens on the root navigator, above the whole shell. The bottom bar is hidden because the page covers the shell.