Sub topic 7
Building the shell UI with StatefulNavigationShell
This chapter assumes that you know Dart and Flutter widgets, Scaffold, basic Navigator concepts, and go_router APIs such as GoRouter, GoRoute, and context.go.
A StatefulShellRoute owns several branch navigators. The StatefulNavigationShell is the widget that connects those navigators to your shell UI. Your shell usually contains persistent controls, such as a bottom navigation bar, while the navigation shell displays the active branch.
The shell's responsibilities
The shell widget normally does three things:
- Places
StatefulNavigationShellin theScaffoldbody. - Reads
currentIndexto highlight the active tab. - Calls
goBranchwhen the user selects a destination.
The shell does not create a separate screen for every tab. The StatefulShellRoute has already created a navigator for each StatefulShellBranch. The shell chooses which branch is visible.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class AppShell extends StatelessWidget {
const AppShell({
super.key,
required this.navigationShell,
});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) {
return Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.settings_outlined),
selectedIcon: Icon(Icons.settings),
label: 'Settings',
),
],
onDestinationSelected: (index) {
navigationShell.goBranch(
index,
initialLocation: index == navigationShell.currentIndex,
);
},
),
);
}
}The route configuration passes the shell into this widget:
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return AppShell(navigationShell: navigationShell);
},
branches: [
// StatefulShellBranch objects go here.
],
)currentIndex is the zero-based index of the active branch. It keeps the NavigationBar selection synchronized with the URL and the router. Avoid storing a second tab index in local widget state. Two sources of truth can become inconsistent after deep links, redirects, or browser back navigation.
How tab selection works
goBranch(index) activates the selected branch. The branch keeps its own route stack, so switching away does not automatically remove the pages inside it.
The initialLocation argument controls what happens when the user taps a tab:
| Value | Behavior |
|---|---|
| `false` | Show the branch at its current location and preserve its stack. |
| `true` | Navigate that branch to its initial route. |
The expression in the example resets the active tab when it is tapped again. This is common in mobile navigation. A user on a deeply nested Home page can tap Home to return to /home.
Visual model
Common mistakes
If the selected tab does not update, check that selectedIndex uses navigationShell.currentIndex. If a tab loses its nested page, check that you call goBranch rather than rebuilding the entire router or using unrelated local state. If the tab bar disappears on a detail page, confirm that the detail route is nested inside the branch rather than placed above the shell.
Exercise
What does initialLocation: index == navigationShell.currentIndex do when the active tab is tapped?
Show answer
It passes true for the active tab, so goBranch returns that branch to its initial route. Tapping a different tab preserves that tab's existing location.