Sub topic 10
Nesting detail pages inside a tab
This chapter assumes you know Dart and Flutter widgets, the basic Navigator model, and go_router concepts such as GoRouter, GoRoute, and context.go. You should also understand that a route can contain child routes.
A tab often needs its own detail pages. For example, the Home tab might show a list of products, and selecting a product should open a detail page. The detail page belongs to the Home tab, so its navigation history should stay separate from the Settings tab.
Add child routes to a branch
A GoRoute can contain a routes list. These child routes use paths relative to their parent. In this example, the child path is details/:id, so the complete URL is /home/details/:id.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
initialLocation: '/home',
routes: [
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return ScaffoldWithNavBar(navigationShell: navigationShell);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
routes: [
GoRoute(
path: 'details/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return DetailsScreen(id: id);
},
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
),
],
),
],
);The :id part is a path parameter. It matches a value in the URL, such as 42. The detail builder reads that value from state.pathParameters.
Navigate to a detail page with either an absolute location or a relative location:
// From anywhere that can reach this route:
context.go('/home/details/42');
// From a Home page when you want to push onto its stack:
context.push('/home/details/42');context.push adds the detail page above the current Home page. The Back button then returns to /home. context.go changes the location directly and is useful when the previous page should not remain on the stack.
Understand the branch stack
Each StatefulShellBranch owns a navigator. The Home branch therefore has a stack like this:
If the user opens a detail page, switches to Settings, and then returns to Home, the Home branch can still display the detail page. This is the state-preservation behavior provided by StatefulShellRoute.
A nested route does not automatically cover the entire application. It is displayed by the navigator belonging to its branch. As a result, the shell UI, such as a bottom NavigationBar, normally remains visible while the detail page is open.
Common errors
- Defining
details/:idas a sibling of/homewhen it should belong to the Home branch. - Calling
context.go('/details/42')when the actual path is/home/details/42. - Reading the parameter with the wrong name, such as
state.pathParameters['productId']when the route declares:id. - Expecting
context.goto preserve the same back-stack behavior ascontext.push. - Replacing the shell or branch navigator key and accidentally losing the stored stack.
Exercise
What complete location matches the child route details/:id under the parent route /home?
Show answer
/home/details/:id, such as /home/details/42 when the parameter value is 42.