Sub topic 14
Persistent bottom navigation with ShellRoute
This chapter assumes you know Dart async and await, basic Flutter widgets, BuildContext, and how go_router defines routes. A shell route is a route that keeps shared UI, such as a bottom navigation bar, around a changing child page.
Why use a shell route?
A normal route replaces the current page. A shell route builds a common layout and places the matched child route inside it. This avoids copying the bottom navigation bar into every screen.
ShellRoute keeps the shell visible while its child changes. For truly persistent tabs, use StatefulShellRoute. It gives each tab its own navigation branch, so switching tabs can preserve the selected page and its navigation stack.
The navigation structure looks like this:
Build persistent tabs
The StatefulNavigationShell passed to the shell builder displays the active branch. Its goBranch method switches branches. initialLocation: true also returns to the branch's initial page when the user selects the already active tab.
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 Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: (index) {
navigationShell.goBranch(
index,
initialLocation: index == navigationShell.currentIndex,
);
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.search),
label: 'Search',
),
NavigationDestination(
icon: Icon(Icons.settings_outlined),
selectedIcon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const Text('Home'),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/search',
builder: (context, state) => const Text('Search'),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const Text('Settings'),
),
],
),
],
),
],
);
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: router);
}
}Each StatefulShellBranch can contain child routes. For example, the home branch could contain /home/details. The shell remains visible, while the branch's navigator handles its internal pages.
Use context.go('/search') when a URL should select a tab. Inside the shell, use navigationShell.goBranch(index) for tab selection because it changes the active branch directly.
Common errors
- Do not create a separate
Scaffoldand bottom bar inside every tab page. - Do not assume
ShellRoutepreserves every tab's navigation stack. UseStatefulShellRoutefor that behavior. - Keep each branch's paths distinct, such as
/homeand/search. - Give each branch a navigator key only when you need explicit control over its navigator.
Exercise
Which route type should you choose when switching tabs must preserve each tab's nested navigation history?
Show answer
Choose StatefulShellRoute, usually with StatefulShellRoute.indexedStack.