Sub topic 12
Bottom Navigation That Keeps State with StatefulShellRoute
When to use a stateful shell
This chapter assumes that you know Dart null safety, can build Flutter widgets, and have used Navigator.push and Navigator.pop. It also assumes that you understand basic go_router routes.
A bottom navigation bar often switches between areas such as Feed, Search, and Settings. A simple implementation changes the current route, but it may discard each tab's navigation stack. For example, a user opens a detail page in Feed, switches to Settings, and returns to Feed. A stateless switch may show the Feed home page again instead of the detail page.
StatefulShellRoute solves this problem. It gives each branch its own navigator. The indexedStack constructor keeps those navigators alive while the user switches tabs.
Configure the shell
Each StatefulShellBranch contains one or more routes. The shell builder receives a StatefulNavigationShell. Display that object as the body of a Scaffold, then call goBranch when the selected navigation destination changes.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/feed',
routes: <RouteBase>[
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return AppScaffold(navigationShell: navigationShell);
},
branches: <StatefulShellBranch>[
StatefulShellBranch(
routes: <RouteBase>[
GoRoute(
path: '/feed',
builder: (context, state) => const FeedScreen(),
routes: <RouteBase>[
GoRoute(
path: 'details',
builder: (context, state) => const FeedDetailsScreen(),
),
],
),
],
),
StatefulShellBranch(
routes: <RouteBase>[
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
),
],
),
],
);
class AppScaffold extends StatelessWidget {
const AppScaffold({super.key, required this.navigationShell});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) {
return Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: (index) {
navigationShell.goBranch(
index,
initialLocation: index == navigationShell.currentIndex,
);
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.list),
label: 'Feed',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
}
}
class FeedScreen extends StatelessWidget {
const FeedScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Feed')),
body: Center(
child: ElevatedButton(
onPressed: () => context.push('/feed/details'),
child: const Text('Open details'),
),
),
);
}
}
class FeedDetailsScreen extends StatelessWidget {
const FeedDetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Feed details')),
);
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Settings')),
);
}
}
void main() {
runApp(MaterialApp.router(routerConfig: router));
}The shell's body displays the active branch. currentIndex identifies that branch. Calling goBranch changes tabs without rebuilding the other branch's navigator.
The initialLocation argument controls what happens when the user taps the already-selected tab. When it is true, that branch returns to its first route. When it is false, the current branch location remains visible. The example uses true only for the active tab, which gives the common “tap the current tab to return home” behavior.
Common mistakes
- Using
context.go('/settings')works for changing locations, but tab buttons should normally callnavigationShell.goBranch. - Placing a detail route outside its tab branch prevents that detail page from belonging to the tab's navigator.
- Replacing
StatefulShellRoutewith an ordinaryShellRouteloses the separate persistent branch stacks.
Exercise
A user opens /feed/details, switches to Settings, and switches back. Which design keeps /feed/details available: a plain route switch or StatefulShellRoute.indexedStack?
Show answer
StatefulShellRoute.indexedStack keeps the Feed branch navigator and its detail stack alive while Settings is active.