-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathshell_route_with_keys_example.dart
More file actions
166 lines (139 loc) · 4.3 KB
/
shell_route_with_keys_example.dart
File metadata and controls
166 lines (139 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: public_member_api_docs, unreachable_from_main
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
part 'shell_route_with_keys_example.g.dart';
void main() => runApp(App());
final GlobalKey<NavigatorState> rootNavigatorKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> shellNavigatorKey = GlobalKey<NavigatorState>();
class App extends StatelessWidget {
App({super.key});
@override
Widget build(BuildContext context) =>
MaterialApp.router(routerConfig: _router);
final GoRouter _router = GoRouter(
routes: $appRoutes,
initialLocation: '/home',
navigatorKey: rootNavigatorKey,
);
}
@TypedShellRoute<MyShellRouteData>(
routes: <TypedRoute<RouteData>>[
TypedGoRoute<HomeRouteData>(path: '/home'),
TypedGoRoute<UsersRouteData>(
path: '/users',
routes: <TypedGoRoute<UserRouteData>>[
TypedGoRoute<UserRouteData>(path: ':id'),
],
),
],
)
class MyShellRouteData extends ShellRouteData {
const MyShellRouteData();
static final GlobalKey<NavigatorState> $navigatorKey = shellNavigatorKey;
@override
Widget builder(BuildContext context, GoRouterState state, Widget navigator) {
return MyShellRouteScreen(child: navigator);
}
}
class MyShellRouteScreen extends StatelessWidget {
const MyShellRouteScreen({required this.child, super.key});
final Widget child;
int getCurrentIndex(BuildContext context) {
final String location = GoRouterState.of(context).uri.path;
if (location.startsWith('/users')) {
return 1;
}
return 0;
}
@override
Widget build(BuildContext context) {
final int selectedIndex = getCurrentIndex(context);
return Scaffold(
body: Row(
children: <Widget>[
NavigationRail(
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.home),
label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.group),
label: Text('Users'),
),
],
selectedIndex: selectedIndex,
onDestinationSelected: (int index) {
switch (index) {
case 0:
const HomeRouteData().go(context);
case 1:
const UsersRouteData().go(context);
}
},
),
const VerticalDivider(thickness: 1, width: 1),
Expanded(child: child),
],
),
);
}
}
class HomeRouteData extends GoRouteData with $HomeRouteData {
const HomeRouteData();
@override
Widget build(BuildContext context, GoRouterState state) {
return const Center(child: Text('The home page'));
}
}
class UsersRouteData extends GoRouteData with $UsersRouteData {
const UsersRouteData();
@override
Widget build(BuildContext context, GoRouterState state) {
return ListView(
children: <Widget>[
for (int userID = 1; userID <= 3; userID++)
ListTile(
title: Text('User $userID'),
onTap: () => UserRouteData(id: userID).go(context),
),
],
);
}
}
class DialogPage extends Page<void> {
/// A page to display a dialog.
const DialogPage({required this.child, super.key});
/// The widget to be displayed which is usually a [Dialog] widget.
final Widget child;
@override
Route<void> createRoute(BuildContext context) {
return DialogRoute<void>(
context: context,
settings: this,
builder: (BuildContext context) => child,
);
}
}
class UserRouteData extends GoRouteData with $UserRouteData {
const UserRouteData({required this.id});
// Without this static key, the dialog will not cover the navigation rail.
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
final int id;
@override
Page<void> buildPage(BuildContext context, GoRouterState state) {
return DialogPage(
key: state.pageKey,
child: Center(
child: SizedBox(
width: 300,
height: 300,
child: Card(child: Center(child: Text('User $id'))),
),
),
);
}
}