Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build cross-platform Flutter 3 apps with Riverpod/Bloc state management, GoRouter navigation, and performance optimization.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/gorouter-navigation.md
1# GoRouter Navigation23## Basic Setup45```dart6import 'package:go_router/go_router.dart';78final goRouter = GoRouter(9initialLocation: '/',10redirect: (context, state) {11final isLoggedIn = /* check auth */;12if (!isLoggedIn && !state.matchedLocation.startsWith('/auth')) {13return '/auth/login';14}15return null;16},17routes: [18GoRoute(19path: '/',20builder: (context, state) => const HomeScreen(),21routes: [22GoRoute(23path: 'details/:id',24builder: (context, state) {25final id = state.pathParameters['id']!;26return DetailsScreen(id: id);27},28),29],30),31GoRoute(32path: '/auth/login',33builder: (context, state) => const LoginScreen(),34),35],36);3738// In app.dart39class MyApp extends StatelessWidget {40const MyApp({super.key});4142@override43Widget build(BuildContext context) {44return MaterialApp.router(45routerConfig: goRouter,46theme: AppTheme.light,47darkTheme: AppTheme.dark,48);49}50}51```5253## Navigation Methods5455```dart56// Navigate and replace history57context.go('/details/123');5859// Navigate and add to stack60context.push('/details/123');6162// Go back63context.pop();6465// Replace current route66context.pushReplacement('/home');6768// Navigate with extra data69context.push('/details/123', extra: {'title': 'Item'});7071// Access extra in destination72final extra = GoRouterState.of(context).extra as Map<String, dynamic>?;73```7475## Shell Routes (Persistent UI)7677```dart78final goRouter = GoRouter(79routes: [80ShellRoute(81builder: (context, state, child) {82return ScaffoldWithNavBar(child: child);83},84routes: [85GoRoute(path: '/home', builder: (_, __) => const HomeScreen()),86GoRoute(path: '/profile', builder: (_, __) => const ProfileScreen()),87GoRoute(path: '/settings', builder: (_, __) => const SettingsScreen()),88],89),90],91);92```9394## Query Parameters9596```dart97GoRoute(98path: '/search',99builder: (context, state) {100final query = state.uri.queryParameters['q'] ?? '';101final page = int.tryParse(state.uri.queryParameters['page'] ?? '1') ?? 1;102return SearchScreen(query: query, page: page);103},104),105106// Navigate with query params107context.go('/search?q=flutter&page=2');108```109110## Quick Reference111112| Method | Behavior |113|--------|----------|114| `context.go()` | Navigate, replace stack |115| `context.push()` | Navigate, add to stack |116| `context.pop()` | Go back |117| `context.pushReplacement()` | Replace current |118| `:param` | Path parameter |119| `?key=value` | Query parameter |120