Sub topic 1
Quick Refresher: GoRouter and GoRoute
go_router is a declarative routing package for Flutter. It maps URL-like locations to widgets. Instead of manually pushing every page with Navigator, you describe the route tree once and change the current location.
This chapter assumes you already know basic Flutter widgets, Scaffold, and the idea of a navigation stack.
Prerequisites
You should be comfortable with:
- Dart classes and functions.
- Flutter widgets and
BuildContext. - Basic
Navigatorconcepts. - URL-like paths such as
/homeand/settings. - The
go_routerpackage andcontext.go.
The Main Routing Pieces
A GoRouter is the application’s routing configuration. It contains one or more GoRoute objects. A GoRoute connects a path to a widget builder.
The path is the location in the app. The builder creates the page for that location. context.go('/settings') changes the current location and lets go_router rebuild the matching route.
| Piece | Purpose | Example |
|---|---|---|
| `GoRouter` | Holds the route configuration | `GoRouter(routes: [...])` |
| `GoRoute` | Maps a path to a screen | `GoRoute(path: '/home', ...)` |
| `context.go` | Replaces the current location | `context.go('/settings')` |
| `context.push` | Adds a location to the stack | `context.push('/details')` |
Here is a small application with two routes:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final GoRouter router = GoRouter(
initialLocation: '/home',
routes: [
GoRoute(
path: '/home',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
),
],
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: router,
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/settings'),
child: const Text('Open settings'),
),
),
);
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/home'),
child: const Text('Back to home'),
),
),
);
}
}MaterialApp.router connects Flutter’s app widget to the GoRouter instance. The initialLocation determines the first location. In this example, the app starts at /home.
Understanding Locations and Navigation
A location is the complete path that identifies the current screen. go_router compares the current location with the configured routes and builds the matching widgets.
context.go moves directly to a new location. It is useful for tab selection or other navigation where the destination represents the current application state. context.push adds a new page to the current navigation stack. That distinction becomes important when you build nested navigation.
flowchart TD
A["Current location: /home"] --> B["context.go('/settings')"]
B --> C["New location: /settings"]
C --> D["SettingsScreen is built"]When a route contains a path parameter, the parameter is part of the location:
GoRoute(
path: '/profile/:userId',
builder: (context, state) {
final userId = state.pathParameters['userId']!;
return ProfileScreen(userId: userId);
},
)For /profile/42, userId has the value 42. The exclamation mark tells Dart that this required parameter is not null.
Why This Matters for StatefulShellRoute
A simple GoRouter can describe a flat set of pages. Nested navigation needs a route tree, where a parent route owns child routes. A shell route adds a shared layout around several route branches.
StatefulShellRoute builds on these ideas. It uses several independent route trees, usually for tabs. Each tree can have its own pages and back stack. The next chapters use the basic GoRouter, GoRoute, and location model introduced here.
Exercise
Which call should you usually use when selecting a tab whose URL represents the app's current state: context.go or context.push?
Show answer
Use context.go, because it changes the current location rather than adding another tab-selection page to the navigation stack.