Sub topic 4
Why ShellRoute Is Not Enough for Tabs
Assume you know Dart and Flutter widgets, have used Navigator, and understand basic go_router configuration. This chapter focuses on the difference between keeping a shared layout and keeping several independent tab histories.
What ShellRoute provides
ShellRoute places a widget around its child routes. The shell often contains shared UI such as a Scaffold, an app bar, or a bottom navigation bar. The child route appears inside the shell's body.
A ShellRoute also creates a nested Navigator for the routes below it. This is useful when pages should open inside a shared layout. For example, /home/details can appear while the shell remains visible.
The important limitation is that one ShellRoute has one nested navigator for its descendants. It does not create one navigator for each possible tab.
Where tab behavior breaks
Suppose /home and /settings are sibling routes inside a ShellRoute. The bottom navigation bar can call context.go('/settings') to change locations. The shell remains visible, but the router changes the child route displayed by the same nested navigator.
This creates problems when each tab should remember its own history:
- The Home tab opens
/home/details. - The user switches to Settings.
- The user switches back to Home.
- The router matches
/home, not/home/details.
The Home detail page is no longer represented by the current location. Its route stack and local widget state are not preserved as an independent tab stack. A shell layout remains persistent, but the tab contents do not behave like separate navigation areas.
This distinction matters for pages with scroll positions, form input, nested detail routes, or their own back history. A route can be reachable again through its URL, but that is different from keeping its previous navigator state alive.
A minimal ShellRoute tab layout
The following configuration creates a shared shell around two locations:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
initialLocation: '/home',
routes: [
ShellRoute(
builder: (context, state, child) {
return AppShell(
location: state.uri.path,
child: child,
);
},
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const Placeholder(),
),
GoRoute(
path: '/settings',
builder: (context, state) => const Placeholder(),
),
],
),
],
);
class AppShell extends StatelessWidget {
const AppShell({
super.key,
required this.location,
required this.child,
});
final String location;
final Widget child;
@override
Widget build(BuildContext context) {
final selectedIndex = location.startsWith('/settings') ? 1 : 0;
return Scaffold(
body: child,
bottomNavigationBar: NavigationBar(
selectedIndex: selectedIndex,
destinations: const [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
onDestinationSelected: (index) {
context.go(index == 0 ? '/home' : '/settings');
},
),
);
}
}This is a good design when the shell is shared and the routes do not need independent histories. It is also simpler than a stateful shell.
Choosing the right shell
| Requirement | ShellRoute | StatefulShellRoute |
|---|---|---|
| Keep a shared scaffold visible | Yes | Yes |
| Display different top-level locations | Yes | Yes |
| Give every tab its own navigator | No | Yes |
| Preserve each tab's route stack | Not by itself | Yes |
| Simpler setup for non-tab layouts | Yes | Usually no |
Use ShellRoute for a common layout around one active navigation flow. Use StatefulShellRoute when tabs must retain separate stacks and widget state while the user switches between them.
Exercise
After visiting /home/details, why can a ShellRoute-based tab layout return to /home without the detail page?
Show answer
ShellRoute provides one nested navigator for its child routes. Switching to another sibling location and back matches /home again, so the Home tab does not have a separate navigator stack that remembers /home/details.