Sub topic 13
Customizing branch display with `navigatorContainerBuilder`
This chapter assumes that you know Flutter widgets, Navigator, declarative routes, and basic go_router usage. You should also understand that a StatefulShellRoute gives each branch its own navigator.
Why customize the branch container?
StatefulShellRoute.indexedStack uses a built-in container for branch navigators. It displays the active branch and keeps the other branches mounted. This is a good default for most tab bars.
Use the regular StatefulShellRoute constructor with navigatorContainerBuilder when you need control over how branches are displayed. For example, you might want to:
- Add a transition when the active tab changes.
- Animate branches with
AnimatedSwitcher. - Apply different layout rules on tablets.
- Keep inactive branches mounted with a custom
Stack. - Add keys, clipping, or visual effects around branch navigators.
The builder receives the StatefulNavigationShell and a list of navigator widgets. Each list item represents one StatefulShellBranch.
Build a custom container
The following example keeps every branch mounted but displays only the active branch. Offstage hides an inactive branch without removing it from the widget tree.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final rootNavigatorKey = GlobalKey<NavigatorState>();
final router = GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: '/home',
routes: [
StatefulShellRoute(
builder: (context, state, navigationShell) {
return Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
destinations: const [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
onDestinationSelected: (index) {
navigationShell.goBranch(index);
},
),
);
},
navigatorContainerBuilder: (context, navigationShell, children) {
return BranchContainer(
currentIndex: navigationShell.currentIndex,
children: children,
);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const Text('Home'),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const Text('Settings'),
),
],
),
],
),
],
);
class BranchContainer extends StatelessWidget {
const BranchContainer({
super.key,
required this.currentIndex,
required this.children,
});
final int currentIndex;
final List<Widget> children;
@override
Widget build(BuildContext context) {
return Stack(
children: [
for (var index = 0; index < children.length; index++)
Offstage(
offstage: index != currentIndex,
child: children[index],
),
],
);
}
}The Scaffold places the StatefulNavigationShell in its body. The shell then asks BranchContainer to lay out the branch navigators. The container does not create the navigators. It only decides how to display the widgets it receives.
Preserve state while changing the layout
Keeping inactive branches mounted is important. If a custom container removes an inactive child from the tree, Flutter may dispose of widgets in that branch. The user could lose scroll positions, form values, or nested navigation history.
| Container strategy | Inactive branches stay mounted | Typical use |
|---|---|---|
| `Offstage` in a `Stack` | Yes | Simple persistent tabs |
| `IndexedStack` | Yes | Built-in indexed display |
| Conditional `children[currentIndex]` | No | Stateless or intentionally reset views |
StatefulShellRoute.indexedStack is effectively the convenient choice when an indexed stack is enough. Choose navigatorContainerBuilder when the container itself needs custom behavior.
Exercise
Exercise
Why should a custom branch container usually keep inactive navigator widgets mounted?
Show answer
Mounted navigators preserve each tab's route stack and widget state, such as scroll positions and form values.