@@ -152,10 +152,67 @@ typedef ExitCallback = FutureOr<bool> Function(
152
152
@immutable
153
153
abstract class RouteBase with Diagnosticable {
154
154
const RouteBase ._({
155
+ this .redirect,
155
156
required this .routes,
156
157
required this .parentNavigatorKey,
157
158
});
158
159
160
+ /// An optional redirect function for this route.
161
+ ///
162
+ /// In the case that you like to make a redirection decision for a specific
163
+ /// route (or sub-route), consider doing so by passing a redirect function to
164
+ /// the GoRoute constructor.
165
+ ///
166
+ /// For example:
167
+ /// ```
168
+ /// final GoRouter _router = GoRouter(
169
+ /// routes: <GoRoute>[
170
+ /// GoRoute(
171
+ /// path: '/',
172
+ /// redirect: (_) => '/family/${Families.data[0].id}',
173
+ /// ),
174
+ /// GoRoute(
175
+ /// path: '/family/:fid',
176
+ /// pageBuilder: (BuildContext context, GoRouterState state) => ...,
177
+ /// ),
178
+ /// ],
179
+ /// );
180
+ /// ```
181
+ ///
182
+ /// If there are multiple redirects in the matched routes, the parent route's
183
+ /// redirect takes priority over sub-route's.
184
+ ///
185
+ /// For example:
186
+ /// ```
187
+ /// final GoRouter _router = GoRouter(
188
+ /// routes: <GoRoute>[
189
+ /// GoRoute(
190
+ /// path: '/',
191
+ /// redirect: (_) => '/page1', // this takes priority over the sub-route.
192
+ /// routes: <GoRoute>[
193
+ /// GoRoute(
194
+ /// path: 'child',
195
+ /// redirect: (_) => '/page2',
196
+ /// ),
197
+ /// ],
198
+ /// ),
199
+ /// ],
200
+ /// );
201
+ /// ```
202
+ ///
203
+ /// The `context.go('/child')` will be redirected to `/page1` instead of
204
+ /// `/page2` .
205
+ ///
206
+ /// Redirect can also be used for conditionally preventing users from visiting
207
+ /// routes, also known as route guards. One canonical example is user
208
+ /// authentication. See [Redirection] (https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/redirection.dart)
209
+ /// for a complete runnable example.
210
+ ///
211
+ /// If [BuildContext.dependOnInheritedWidgetOfExactType] is used during the
212
+ /// redirection (which is how `of` method is usually implemented), a
213
+ /// re-evaluation will be triggered if the [InheritedWidget] changes.
214
+ final GoRouterRedirect ? redirect;
215
+
159
216
/// The list of child routes associated with this route.
160
217
final List <RouteBase > routes;
161
218
@@ -209,7 +266,7 @@ class GoRoute extends RouteBase {
209
266
this .builder,
210
267
this .pageBuilder,
211
268
super .parentNavigatorKey,
212
- this .redirect,
269
+ super .redirect,
213
270
this .onExit,
214
271
super .routes = const < RouteBase > [],
215
272
}) : assert (path.isNotEmpty, 'GoRoute path cannot be empty' ),
@@ -325,62 +382,6 @@ class GoRoute extends RouteBase {
325
382
///
326
383
final GoRouterWidgetBuilder ? builder;
327
384
328
- /// An optional redirect function for this route.
329
- ///
330
- /// In the case that you like to make a redirection decision for a specific
331
- /// route (or sub-route), consider doing so by passing a redirect function to
332
- /// the GoRoute constructor.
333
- ///
334
- /// For example:
335
- /// ```
336
- /// final GoRouter _router = GoRouter(
337
- /// routes: <GoRoute>[
338
- /// GoRoute(
339
- /// path: '/',
340
- /// redirect: (_) => '/family/${Families.data[0].id}',
341
- /// ),
342
- /// GoRoute(
343
- /// path: '/family/:fid',
344
- /// pageBuilder: (BuildContext context, GoRouterState state) => ...,
345
- /// ),
346
- /// ],
347
- /// );
348
- /// ```
349
- ///
350
- /// If there are multiple redirects in the matched routes, the parent route's
351
- /// redirect takes priority over sub-route's.
352
- ///
353
- /// For example:
354
- /// ```
355
- /// final GoRouter _router = GoRouter(
356
- /// routes: <GoRoute>[
357
- /// GoRoute(
358
- /// path: '/',
359
- /// redirect: (_) => '/page1', // this takes priority over the sub-route.
360
- /// routes: <GoRoute>[
361
- /// GoRoute(
362
- /// path: 'child',
363
- /// redirect: (_) => '/page2',
364
- /// ),
365
- /// ],
366
- /// ),
367
- /// ],
368
- /// );
369
- /// ```
370
- ///
371
- /// The `context.go('/child')` will be redirected to `/page1` instead of
372
- /// `/page2` .
373
- ///
374
- /// Redirect can also be used for conditionally preventing users from visiting
375
- /// routes, also known as route guards. One canonical example is user
376
- /// authentication. See [Redirection] (https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/redirection.dart)
377
- /// for a complete runnable example.
378
- ///
379
- /// If [BuildContext.dependOnInheritedWidgetOfExactType] is used during the
380
- /// redirection (which is how `of` method is usually implemented), a
381
- /// re-evaluation will be triggered if the [InheritedWidget] changes.
382
- final GoRouterRedirect ? redirect;
383
-
384
385
/// Called when this route is removed from GoRouter's route history.
385
386
///
386
387
/// Some example this callback may be called:
@@ -458,9 +459,11 @@ class GoRoute extends RouteBase {
458
459
/// as [ShellRoute] and [StatefulShellRoute] .
459
460
abstract class ShellRouteBase extends RouteBase {
460
461
/// Constructs a [ShellRouteBase] .
461
- const ShellRouteBase ._(
462
- {required super .routes, required super .parentNavigatorKey})
463
- : super ._();
462
+ const ShellRouteBase ._({
463
+ super .redirect,
464
+ required super .routes,
465
+ required super .parentNavigatorKey,
466
+ }) : super ._();
464
467
465
468
static void _debugCheckSubRouteParentNavigatorKeys (
466
469
List <RouteBase > subRoutes, GlobalKey <NavigatorState > navigatorKey) {
@@ -623,6 +626,7 @@ class ShellRouteContext {
623
626
class ShellRoute extends ShellRouteBase {
624
627
/// Constructs a [ShellRoute] .
625
628
ShellRoute ({
629
+ super .redirect,
626
630
this .builder,
627
631
this .pageBuilder,
628
632
this .observers,
@@ -783,6 +787,7 @@ class StatefulShellRoute extends ShellRouteBase {
783
787
/// [navigatorContainerBuilder] .
784
788
StatefulShellRoute ({
785
789
required this .branches,
790
+ super .redirect,
786
791
this .builder,
787
792
this .pageBuilder,
788
793
required this .navigatorContainerBuilder,
@@ -809,12 +814,14 @@ class StatefulShellRoute extends ShellRouteBase {
809
814
/// for a complete runnable example using StatefulShellRoute.indexedStack.
810
815
StatefulShellRoute .indexedStack ({
811
816
required List <StatefulShellBranch > branches,
817
+ GoRouterRedirect ? redirect,
812
818
StatefulShellRouteBuilder ? builder,
813
819
GlobalKey <NavigatorState >? parentNavigatorKey,
814
820
StatefulShellRoutePageBuilder ? pageBuilder,
815
821
String ? restorationScopeId,
816
822
}) : this (
817
823
branches: branches,
824
+ redirect: redirect,
818
825
builder: builder,
819
826
pageBuilder: pageBuilder,
820
827
parentNavigatorKey: parentNavigatorKey,
0 commit comments