Sub topic 11
Shared layouts with ShellRoute
Assume you know Dart null safety, Flutter widgets, Navigator.push and Navigator.pop, and the basic GoRouter setup from earlier chapters. This chapter focuses on sharing one layout across several routes.
A ShellRoute is a route that wraps a group of child routes. Its builder receives the currently matched child page. You can place shared UI, such as an app bar, drawer, or bottom navigation bar, around that child.
The URL still identifies the active page. The shell only provides the common visual structure.
Build a shared application shell
Add go_router to pubspec.yaml, then create a router with a shell around the application pages:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
initialLocation: '/',
routes: <RouteBase>[
ShellRoute(
builder: (context, state, child) {
return AppShell(child: child);
},
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
routes: <RouteBase>[
GoRoute(
path: 'details',
builder: (context, state) => const DetailsPage(),
),
],
),
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsPage(),
),
],
),
],
);
void main() {
runApp(
MaterialApp.router(
routerConfig: router,
),
);
}
class AppShell extends StatelessWidget {
const AppShell({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Library')),
body: child,
bottomNavigationBar: NavigationBar(
destinations: const [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
onDestinationSelected: (index) {
context.go(index == 0 ? '/' : '/settings');
},
),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () => context.go('/details'),
child: const Text('Open details'),
),
);
}
}
class DetailsPage extends StatelessWidget {
const DetailsPage({super.key});
@override
Widget build(BuildContext context) {
return const Center(child: Text('Details'));
}
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return const Center(child: Text('Settings'));
}
}The shell remains in the widget tree while the child changes. Therefore, the app bar and navigation bar do not need to be repeated in every page.
The details route is nested under /. Its complete location is /details, because the parent path is / and the child path is details.
ShellRoute and StatefulShellRoute
ShellRoute is useful when several pages share one layout. It does not automatically preserve an independent navigation stack for each bottom navigation branch. Switching from one branch to another can rebuild the branch's location.
Use StatefulShellRoute.indexedStack when each tab must keep its own stack and state. For example, a feed tab can remain scrolled while you open settings and return later. That more advanced pattern belongs to tabbed navigation.
A shell also has its own navigator. If a page must cover the shell, such as a full-screen login page or dialog, give the route a root navigator key and use parentNavigatorKey.
Exercise
What should you use when several routes share an app bar, but each bottom-navigation tab must preserve its own navigation stack?
Show answer
Use ShellRoute for the shared layout only when one common stack is enough. Use StatefulShellRoute.indexedStack when each tab needs its own persistent stack and state.