-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.dart
252 lines (217 loc) · 5.81 KB
/
routes.dart
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import 'package:flutter/material.dart';
import 'package:utilities/utilities.dart';
import 'gen/l10n/l10n.dart';
import 'screens/opening.dart';
import 'screens/product.dart';
import 'screens/product_list.dart';
import 'screens/settings.dart';
part 'routes.g.dart';
extension AppRouteProvider on BuildContext {
AppRoute get currentRoute => AppRoute.fromLocation(
this,
GoRouterState.of(this).uri.toString(),
);
}
enum RoutingMethod {
go,
push,
pushReplacement,
replace,
}
// Would be nice if this was made part of the library but sealed types cannot
// be extended outside of file/library
sealed class AppRoute extends ScreenRoute {
const AppRoute() : super();
abstract final int? index;
bool matchesLocation(final String location);
Future<T?> navigate<T>(
final BuildContext context,
final RoutingMethod method,
);
static AppRoute? fromIndex(final int index) {
if (index == const OpeningRoute().index) {
return const OpeningRoute();
} else if (index == const SettingsRoute().index) {
return const SettingsRoute();
} else if (index == const ProductListRoute().index) {
return const ProductListRoute();
} else {
return null;
}
}
static AppRoute fromLocation(
final BuildContext context,
final String location,
) {
if (const OpeningRoute().matchesLocation(location)) {
return const OpeningRoute();
} else if (const SettingsRoute().matchesLocation(location)) {
return const SettingsRoute();
} else if (const ProductListRoute().matchesLocation(location)) {
return const ProductListRoute();
} else {
final int? id = int.tryParse(
GoRouterState.of(context).uri.pathSegments.last,
);
if (id == null) {
return const ProductListRoute();
}
return ProductRoute(id: id);
}
}
}
@TypedGoRoute<OpeningRoute>(
path: '/',
routes: <TypedGoRoute<AppRoute>>[
TypedGoRoute<ProductListRoute>(
path: 'products',
routes: <TypedGoRoute<AppRoute>>[
TypedGoRoute<ProductRoute>(path: 'product/:id'),
],
),
TypedGoRoute<SettingsRoute>(path: 'settings'),
],
)
class OpeningRoute extends AppRoute {
const OpeningRoute() : super();
@override
String title(final BuildContext context) => context.l10n.routeOpeningTitle;
@override
RouteScreenBuilder get builder => (
final BuildContext context,
final GoRouterState state,
) =>
OpeningScreen(state: state);
@override
int? get index => 0;
@override
bool matchesLocation(final String location) => this.location == location;
@override
Future<T?> navigate<T>(
final BuildContext context,
final RoutingMethod method,
) async {
switch (method) {
case RoutingMethod.go:
go(context);
break;
case RoutingMethod.push:
return push<T>(context);
case RoutingMethod.pushReplacement:
pushReplacement(context);
break;
case RoutingMethod.replace:
replace(context);
break;
}
return null;
}
}
class SettingsRoute extends AppRoute {
const SettingsRoute() : super();
@override
String title(final BuildContext context) => context.l10n.routeSettingsTitle;
@override
RouteScreenBuilder get builder => (
final BuildContext context,
final GoRouterState state,
) =>
SettingsScreen(state: state);
@override
int? get index => 2;
@override
bool matchesLocation(final String location) => this.location == location;
@override
Future<T?> navigate<T>(
final BuildContext context,
final RoutingMethod method,
) async {
switch (method) {
case RoutingMethod.go:
go(context);
break;
case RoutingMethod.push:
return push<T>(context);
case RoutingMethod.pushReplacement:
pushReplacement(context);
break;
case RoutingMethod.replace:
replace(context);
break;
}
return null;
}
}
class ProductListRoute extends AppRoute {
const ProductListRoute() : super();
@override
String title(final BuildContext context) =>
context.l10n.routeProductsListTitle;
@override
RouteScreenBuilder get builder => (
final BuildContext context,
final GoRouterState state,
) =>
ProductListScreen(state: state);
@override
int? get index => 1;
@override
bool matchesLocation(final String location) => this.location == location;
@override
Future<T?> navigate<T>(
final BuildContext context,
final RoutingMethod method,
) async {
switch (method) {
case RoutingMethod.go:
go(context);
break;
case RoutingMethod.push:
return push<T>(context);
case RoutingMethod.pushReplacement:
pushReplacement(context);
break;
case RoutingMethod.replace:
replace(context);
break;
}
return null;
}
}
class ProductRoute extends AppRoute {
const ProductRoute({required this.id}) : super();
final int id;
@override
String title(final BuildContext context) =>
context.l10n.routeProductTitle(id);
@override
RouteScreenBuilder get builder => (
final BuildContext context,
final GoRouterState state,
) =>
ProductScreen(state: state, id: id);
@override
int? get index => null;
@override
bool matchesLocation(final String location) => this.location == location;
@override
Future<T?> navigate<T>(
final BuildContext context,
final RoutingMethod method,
) async {
switch (method) {
case RoutingMethod.go:
go(context);
break;
case RoutingMethod.push:
return push<T>(context);
case RoutingMethod.pushReplacement:
pushReplacement(context);
break;
case RoutingMethod.replace:
replace(context);
break;
}
return null;
}
}