Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
159 commits
Select commit Hold shift + click to select a range
fa4c542
Added NestedNavigationShellRoute, to support building nested persiste…
tolo Sep 26, 2022
76b17ae
Added example for NestedNavigationShellRoute.
tolo Sep 26, 2022
7612a55
Formatting.
tolo Sep 27, 2022
62574bd
Formatting.
tolo Sep 27, 2022
850f41a
Removed NestedNavigationShellRoute and replaced it with new field `ne…
tolo Sep 27, 2022
1c04588
Rebased onto upstream/main.
tolo Sep 27, 2022
1db6bba
Fixed typos.
tolo Sep 27, 2022
6028c90
Updated documentation of property navigatorKey on ShellRoute.
tolo Sep 28, 2022
5f2e995
Added another unit test for ShellRoute with nestedNavigationBuilder.
tolo Sep 28, 2022
461efd9
Updated example to use nested ShellRoutes instead of creating nested …
tolo Sep 28, 2022
0315e1a
Refactored support for nested stateful navigation - introduced new ro…
tolo Sep 30, 2022
510ec34
Some renaming.
tolo Sep 30, 2022
e82647a
Changed the way currentLocation is calculated since it currently does…
tolo Sep 30, 2022
e38fa32
Minor cleanup and refactoring.
tolo Sep 30, 2022
f38b9bf
Renamed StackedNavigationScaffold to StackedNavigationShell.
tolo Sep 30, 2022
89f1dd7
Added more tests for PartitionedShellRoute.
tolo Sep 30, 2022
3a00a54
Added more detail
tolo Sep 30, 2022
b0a6264
Fixed analyzer issue and code style issue.
tolo Sep 30, 2022
b729d64
Merge branch 'main' of github.com:flutter/packages into nested-persis…
johnpryan Sep 30, 2022
3b3909f
Fix test
johnpryan Sep 30, 2022
f0b2a1b
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 2, 2022
eb6d4d3
Introduced the method `navigatorKeyForChildRoute` on ShellRouteBase t…
tolo Oct 2, 2022
016be76
Added a canPop test for PartitionedShellRoute (by replacing a duplica…
tolo Oct 2, 2022
4ae26d7
Updated implementation of popRoute be in sync with canPop.
tolo Oct 2, 2022
4a9889a
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 3, 2022
1f3f01a
Updated documentation of PartitionedShellRoute with examples.
tolo Oct 3, 2022
3b38641
Updated documentation for builder and pageBuilder fields of ShellRout…
tolo Oct 3, 2022
d701ab5
Fixed documentation typos and minor refactoring (renaming).
tolo Oct 3, 2022
bb6240d
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 3, 2022
245d47b
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 4, 2022
bda571b
Refactored PartitionedShellRoute and renamed to StatefulShellRoute.
tolo Oct 7, 2022
da1ea0b
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 7, 2022
59c19e7
Some refactoring (mostly naming and code readability).
tolo Oct 10, 2022
723de76
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 10, 2022
4c0a91d
Fixed CI analyzer issue.
tolo Oct 10, 2022
7556965
Removed animation support from StatefulNavigationShell and refactored…
tolo Oct 11, 2022
ee9bde9
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 11, 2022
e9a7029
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 13, 2022
59a6b05
Updated changelog (replaced PartitionedShellRoute with StatefulShellR…
tolo Oct 13, 2022
06ab1e9
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 14, 2022
1b877c8
Implemented equality in StatefulShellRouteState and ShellRouteBranchS…
tolo Oct 22, 2022
e542a05
Documentation updates.
tolo Oct 23, 2022
2215a51
Removed field navigatorKeys from StatefulShellRoute and fixed issue w…
tolo Oct 25, 2022
b180653
Removed the use of top GoRouterState as a way of getting the current …
tolo Oct 25, 2022
8fdfb82
Updated example to use the default constructors of StatefulShellRoute…
tolo Oct 25, 2022
9240ea4
Added check when pushing a new route to ensure you cannot push a rout…
tolo Oct 25, 2022
93bce8e
Minor renaming.
tolo Oct 25, 2022
20dc0c6
Added restorationScopeId to ShellRouteBranch and ShellRoute.
tolo Oct 25, 2022
376e80f
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 25, 2022
2b2ff91
Added support for maintaining any extra navigation object passed to t…
tolo Oct 27, 2022
5c9fe04
Moved NEXT info 5.2.0 in changelog
tolo Oct 28, 2022
81e1296
Added assertion in RouteConfiguration for field defaultLocation of Sh…
tolo Oct 28, 2022
59e3b66
Added field preloadBranches to StatefulShellRoute, to enable support …
tolo Oct 28, 2022
8636bf8
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 28, 2022
1c509f1
Various updates from PR feedback:
tolo Oct 28, 2022
42c7b7d
Changed the way switching between route branches works - switching is…
tolo Oct 29, 2022
62e7fc1
Reverted restriction around pushing sub-routes of a StatefulShellRoute.
tolo Oct 30, 2022
9a7069a
Doc fixes/updates for StatefulShellRoute.
tolo Oct 30, 2022
ae48ede
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Oct 30, 2022
141fdc1
Added handling of redirection when switching and preloading route bra…
tolo Nov 4, 2022
b6b289f
Reintroduced validation of defaultLocation of StatefulShellRoute (in …
tolo Nov 4, 2022
4d05d99
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Nov 4, 2022
d4edd47
Removed replaceMatchList in GoRouterDelegate (using setNewRoutePath i…
tolo Nov 4, 2022
7b9de47
Added additional test for redirection with StatefulShellRoute.
tolo Nov 4, 2022
703815c
Updated ShellRouteBranch to accept list of routes instead of only a s…
tolo Nov 9, 2022
9f88928
Added support for resetting StatefulShellRouteState.
tolo Nov 9, 2022
5ca533d
Renamed and changed signature of the navigator getters of StatefulShe…
tolo Nov 9, 2022
f5f0ecb
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Nov 9, 2022
4a2eac3
Added temporary workaround due to duplication of encodedParams of Rou…
tolo Nov 18, 2022
fc7bd54
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Nov 18, 2022
a65f9df
Refactoring of StatefulShellRoute to support dynamic branches, as wel…
tolo Nov 29, 2022
816acb2
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Nov 29, 2022
ee2a845
Introduced an internal branch Navigator proxy widget to make the API …
tolo Nov 30, 2022
89b82c5
Renamed StatefulShellRoute example file to stateful_shell_route.dart
tolo Nov 30, 2022
90b9d62
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Nov 30, 2022
7392264
Reduced unnecessary rebuilds of child Widgets of StatefulShellRoute, …
tolo Dec 1, 2022
c0253f6
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 1, 2022
2c4afa1
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 2, 2022
845c052
Minor refactoring - moved StatefulShellBranchResolver to route.dart.
tolo Dec 2, 2022
193a267
Fixed issue in StatefulShellBranch.isBranchFor (accidental use of inc…
tolo Dec 8, 2022
37c4969
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 8, 2022
c562482
Reverted workaround regarding pop.
tolo Dec 8, 2022
38b5772
Equality fix in UnmodifiableRouteMatchList.
tolo Dec 8, 2022
db22bac
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 8, 2022
d8d1641
Corrected invalid sample file names.
tolo Dec 13, 2022
4c4b7b0
Additional rebuild improvement.
tolo Dec 13, 2022
f08f548
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 13, 2022
6f1b047
Minor documentation fix.
tolo Dec 14, 2022
07ee030
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 14, 2022
9ffff5c
Removed AppRouterProvider from sample code.
tolo Dec 15, 2022
e9d40ee
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Dec 15, 2022
0ea48cb
Update packages/go_router/CHANGELOG.md
tolo Dec 21, 2022
4abcaa9
Update packages/go_router/CHANGELOG.md
tolo Dec 21, 2022
ce23558
Update packages/go_router/CHANGELOG.md
tolo Dec 21, 2022
351ceb2
Update packages/go_router/lib/src/route.dart
tolo Dec 21, 2022
4cb0f1e
Partially reverted the StatefulShellRoute API back to the previous so…
tolo Jan 5, 2023
e6a4f71
Documentation updates and some renaming.
tolo Jan 5, 2023
873bf34
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Jan 5, 2023
5b668f8
Fix for error in _preloadShellBranchNavigator due to recent changes i…
tolo Jan 5, 2023
565c3cd
Updated _routeMatchLookUp to handle reused/cached Navigators.
tolo Jan 19, 2023
5ee1a8f
Added support for resetting state for single branch (resetBranch).
tolo Jan 19, 2023
358551f
Added examples of pushing modal routes above the stateful shell.
tolo Jan 19, 2023
f9a2608
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Jan 19, 2023
87211aa
Alternative StatefulShellRoute implementation, were either a builder …
tolo Feb 7, 2023
257a272
Extracted construction of StatefulNavigationShell and Navigators out …
tolo Feb 16, 2023
08148e5
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Feb 16, 2023
c18941f
Added NavigatorObserver support to StatefulShellRoute.
tolo Feb 16, 2023
b1ce762
Refactored the builder methods of the shell route classes to improve …
tolo Feb 16, 2023
75e43d0
Various refactoring.
tolo Feb 17, 2023
9f54b4e
Minor renaming.
tolo Feb 17, 2023
1226f44
Updated equals method in GoRouterState to properly compare Map fields.
tolo Feb 19, 2023
881be86
Updated handling of RouteMatch lookup for pages/routes (replaced Expa…
tolo Feb 19, 2023
6bbbd29
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Feb 19, 2023
fd5412f
Add type annotation to listEquals
tolo Feb 23, 2023
a5234a6
Moved RouteNavigatorBuilder into builder.dart and made private.
tolo Feb 26, 2023
b9428cb
Moved StatefulNavigationShell and support classes into route.dart and…
tolo Feb 26, 2023
ee047c8
Refactoring of StatefulShellRoute and related classes to simplify bui…
tolo Mar 8, 2023
ddc71f0
Re-introduced proper validation of parent Navigator keys for routes i…
tolo Mar 8, 2023
5041bea
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Mar 8, 2023
01cce04
Updated constructors in sample code to use super parameters.
tolo Mar 8, 2023
9315373
Removed preload support.
tolo Mar 10, 2023
89341ef
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Mar 10, 2023
2c298cd
Refactored state management for StatefulShellRoute.
tolo Mar 13, 2023
4c22b53
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Mar 13, 2023
a7d419f
Moved branch state management back into StatefulShellRoute and simpli…
tolo Mar 31, 2023
7285788
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Mar 31, 2023
9983887
Renamed StatefulShellRoute to StackedShellRoute
tolo Apr 6, 2023
ad43837
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Apr 6, 2023
3a83007
Merge remote-tracking branch 'upstream/main' into nested-persistent-n…
tolo Apr 6, 2023
ec6722c
Update packages/go_router/lib/src/route.dart
tolo Apr 15, 2023
9bb0da9
Update packages/go_router/lib/src/route.dart
tolo Apr 15, 2023
a1c7a8f
Update packages/go_router/lib/src/route.dart
tolo Apr 15, 2023
477b47c
Removed StackedShellRouteState and moved functionality into StackedNa…
tolo Apr 20, 2023
7f25f0c
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Apr 20, 2023
6f35636
Doc updates - removed/replaced references to StackedShellRouteState.
tolo Apr 20, 2023
bb5c08f
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo Apr 28, 2023
16c095c
Refactoring due to review feedback.
tolo Apr 29, 2023
a18a5a5
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo May 1, 2023
abbd01b
Removed matchListEquals from RouteMatchList and replaced with the now…
tolo May 1, 2023
0f36623
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo May 1, 2023
316cf8c
Removed obsolete copy method from RouteMatchList.
tolo May 4, 2023
79a0c63
Updated StatefulNavigationShell to only save the part of the RouteMat…
tolo May 4, 2023
3696448
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo May 4, 2023
763b136
Minor sample code refactoring (documentation and renaming).
tolo May 8, 2023
2f48831
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo May 8, 2023
709ee7a
Apply suggestions from code review
tolo May 10, 2023
bb7fd9e
Implemented review feedback
tolo May 10, 2023
9eaf1af
Some additional cleanup
tolo May 10, 2023
527a806
Merge branch 'main' of github.com:flutter/packages into nested-persis…
tolo May 10, 2023
86a72e1
Merge branch 'main' into nested-persistent-navigation
johnpryan May 17, 2023
57b761f
Apply suggestions from code review
tolo May 18, 2023
85fa0b6
Apply suggestions from code review.
tolo May 18, 2023
d75c5e2
Reintroduced resetLocation parameter to goBranch.
tolo May 18, 2023
bd1c018
Merge remote-tracking branch 'upstream/main' into nested-persistent-n…
tolo May 18, 2023
c13cd71
Renamed parameter resetLocation in goBranch to initialLocation.
tolo May 19, 2023
c7b1295
Fixed future deprecation warning workaround
tolo May 19, 2023
0f842ee
Merge remote-tracking branch 'upstream/main' into nested-persistent-n…
tolo May 19, 2023
80c396f
Merge branch 'main' into nested-persistent-navigation
chunhtai May 19, 2023
04b549a
Merge branch 'main' into nested-persistent-navigation
johnpryan May 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored state management for StatefulShellRoute.
Removed some obsolete code.
  • Loading branch information
tolo committed Mar 13, 2023
commit 2c298cd6d690273ea46ac043206cbf96140e334f
8 changes: 0 additions & 8 deletions packages/go_router/example/lib/stateful_shell_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class NestedTabNavigationExampleApp extends StatelessWidget {
branches: <StatefulShellBranch>[
/// The route branch for the first tab of the bottom navigation bar.
StatefulShellBranch(
/// To enable preloading of the initial locations of branches, pass
/// true for the parameter preload.
// preload: true,
navigatorKey: _tabANavigatorKey,
routes: <RouteBase>[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this only take a single RouteBase? I can see the behavior difference with a single RouteBase vs multiple RouteBases when you switch to a different uri. but multiple RouteBases may make route parsing a bit complex. Also it is hard to keep track if you may have duplicated GoRoute in different branch.

Is that a use case that require multiple RouteBases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think the main reason was to better support redirection and the possibility to have different root routes for a branch, depending on the application state.

Copy link
Contributor

@chunhtai chunhtai Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the possibility to have different root routes

I think that can be a valid use case. Does branch has priority? for example, if a string location would match two different branch, does one take priority over the other. If so, how the priorty is design. We should definitely document the behavior.

If the branch should not have priority, we should add an assert that the route can't overlap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure any priority is needed, but I guess an overlap check could be good. But perhaps such a check should be added for all routes, not just for StatefulShellRoute branches, or is there something preventing such a check (apart from parametrised routes making such check a bit harder)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok for subroutes to overlaps for example this is valid
routes: [
GoRoute(path: '/page/myspecialpage'),
GoRoute(path: '/page/*'),
]

in this case you can do special handling for a specific match in a wild card match, but I am not sure about branches if there are multiple root route though... maybe that is ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late response to this (possible answered elsewhere), but this should be fine - overlapping routes should work the same as in other places. The only difference is that StatefulShellRoute needs to determine which Navigator a sub-route should be placed on (handled in navigatorKeyForSubRoute), but overlapping routes shouldn't affect this.

A related thing that I realised though, is that there probably needs to be an assertion to check that the defaultRoute of a branch doesn't use parameters. In such case, the initialLocation must be used instead.

GoRoute(
Expand Down Expand Up @@ -510,11 +507,6 @@ class TabScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
/// If preloading is enabled on the top StatefulShellRoute, this will be
/// printed directly after the app has been started, but only for the route
/// that is the initial location ('/c1')
debugPrint('Building TabScreen - $label');

return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
Expand Down
62 changes: 25 additions & 37 deletions packages/go_router/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class RouteBuilder {
final Map<GlobalKey<NavigatorState>, HeroController> _goHeroCache =
<GlobalKey<NavigatorState>, HeroController>{};

/// State for any active stateful shell routes
final Map<ShellRouteBase, Object?> _shellRouteState =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary need to be done in this pr. I think we should unify all the state update in one central place at some point. Right now we have GoRouterStateRegistry, _shellRouteState, and _goHeroCache and they all update separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if we want to avoid "weird" ways of wiring up StatefulShellRouteState with StatefulNavigationShell, we kind of need to have this here as I see it. Either that or return to the previous way of using an InheritedWidget and a somewhat more complex builder/pageBuilder setup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, what I meant is to have one state update system that have a general API to take necessary input during the build step. the system hold all these states and update accoudingly. I don't think anything would change here, the problem with the current structure is that if we update the build logic, we need to make sure all three state also need to be update correclty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, misunderstood you. Yes, I was kind of thinking about this as well, but this seemed like a pretty large change, so I was thinking this was something that could be done in stages. But I think some kind of centralized state management would good.

<ShellRouteBase, Object?>{};

/// Builds the top-level Navigator for the given [RouteMatchList].
Widget build(
BuildContext context,
Expand Down Expand Up @@ -154,43 +158,19 @@ class RouteBuilder {
/// Clean up previous cache to prevent memory leak.
_goHeroCache.removeWhere(
(GlobalKey<NavigatorState> key, _) => !keyToPage.keys.contains(key));
}
}

/// Builds a preloaded nested [Navigator], containing a sub-tree (beginning
/// at startIndex) of the provided route match list.
Widget buildPreloadedNestedNavigator(
BuildContext context,
RouteMatchList matchList,
int startIndex,
bool routerNeglect,
GlobalKey<NavigatorState> navigatorKey,
{List<NavigatorObserver>? observers,
String? restorationScopeId}) {
final Map<GlobalKey<NavigatorState>, List<Page<Object?>>> keyToPage =
<GlobalKey<NavigatorState>, List<Page<Object?>>>{};
try {
final _PagePopContext pagePopContext = _PagePopContext._(onPopPage);
_buildRecursive(
context,
matchList,
startIndex,
pagePopContext,
routerNeglect,
keyToPage,
navigatorKey, <Page<Object?>, GoRouterState>{});

return _buildNavigator(
pagePopContext.onPopPage,
keyToPage[navigatorKey]!,
navigatorKey,
observers: observers,
restorationScopeId: restorationScopeId,
heroController: _getHeroController(context),
);
} on _RouteBuilderError catch (e) {
return _buildErrorNavigator(
context, e, matchList, onPopPage, configuration.navigatorKey);
/// Clean up cache of shell route states, but keep states for shell routes
/// in the current match list, as well as in any nested stateful shell
/// routes
if (_shellRouteState.isNotEmpty) {
Iterable<ShellRouteBase> shellRoutes = matchList.matches
.map((RouteMatch e) => e.route)
.whereType<ShellRouteBase>();
shellRoutes = RouteConfiguration.routesRecursively(shellRoutes)
.whereType<ShellRouteBase>();
_shellRouteState
.removeWhere((ShellRouteBase key, _) => !shellRoutes.contains(key));
}
}
}

Expand Down Expand Up @@ -273,12 +253,20 @@ class RouteBuilder {
);
}

final ShellRouteContext shellRouteContext = ShellRouteContext(
ShellRouteContext shellRouteContext = ShellRouteContext(
subRoute: subRoute,
routeMatchList: matchList,
shellRouteState: _shellRouteState[route],
navigatorBuilder: buildShellNavigator,
);

// Call the ShellRouteBase to create/update the shell route state
final Object? shellRouteState =
route.updateShellState(context, state, shellRouteContext);
_shellRouteState[route] = shellRouteState;
shellRouteContext =
shellRouteContext.copyWith(shellRouteState: shellRouteState);

// Build the Page for this route
final Page<Object?> page = _buildPageForRoute(
context, state, match, pagePopContext,
Expand Down
13 changes: 7 additions & 6 deletions packages/go_router/lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,23 @@ class RouteConfiguration {
return true;
}

static Iterable<RouteBase> _subRoutesRecursively(
List<RouteBase> routes) sync* {
/// Returns an Iterable that traverses the provided routes and their
/// sub-routes recursively.
static Iterable<RouteBase> routesRecursively(
Iterable<RouteBase> routes) sync* {
for (final RouteBase route in routes) {
yield route;
yield* _subRoutesRecursively(route.routes);
yield* routesRecursively(route.routes);
}
}

static GoRoute? _findFirstGoRoute(List<RouteBase> routes) =>
_subRoutesRecursively(routes).whereType<GoRoute>().firstOrNull;
routesRecursively(routes).whereType<GoRoute>().firstOrNull;

/// Tests if a route is a descendant of, or same as, an ancestor route.
bool _debugIsDescendantOrSame(
{required RouteBase ancestor, required RouteBase route}) =>
ancestor == route ||
_subRoutesRecursively(ancestor.routes).contains(route);
ancestor == route || routesRecursively(ancestor.routes).contains(route);

/// Recursively traverses the routes of the provided StatefulShellBranch to
/// find the first GoRoute, from which a full path will be derived.
Expand Down
31 changes: 18 additions & 13 deletions packages/go_router/lib/src/matching.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,25 @@ class RouteMatchList {
class UnmodifiableRouteMatchList {
/// UnmodifiableRouteMatchList constructor.
UnmodifiableRouteMatchList.from(RouteMatchList routeMatchList)
: _matches = List<RouteMatch>.unmodifiable(routeMatchList.matches),
_uri = routeMatchList.uri,
_pathParameters =
: matches = List<RouteMatch>.unmodifiable(routeMatchList.matches),
uri = routeMatchList.uri,
pathParameters =
Map<String, String>.unmodifiable(routeMatchList.pathParameters);

final List<RouteMatch> _matches;
final Uri _uri;
final Map<String, String> _pathParameters;
/// The route matches.
final List<RouteMatch> matches;

/// The uri of the current match.
final Uri uri;

/// Parameters for the matched route, URI-encoded.
final Map<String, String> pathParameters;

/// Creates a new [RouteMatchList] from this UnmodifiableRouteMatchList.
RouteMatchList get modifiableMatchList => RouteMatchList(
List<RouteMatch>.from(_matches),
_uri,
Map<String, String>.from(_pathParameters));
List<RouteMatch>.from(matches),
uri,
Map<String, String>.from(pathParameters));

@override
bool operator ==(Object other) {
Expand All @@ -177,13 +182,13 @@ class UnmodifiableRouteMatchList {
if (other is! UnmodifiableRouteMatchList) {
return false;
}
return listEquals<RouteMatch>(other._matches, _matches) &&
other._uri == _uri &&
mapEquals<String, String>(other._pathParameters, _pathParameters);
return listEquals<RouteMatch>(other.matches, matches) &&
other.uri == uri &&
mapEquals<String, String>(other.pathParameters, pathParameters);
}

@override
int get hashCode => Object.hash(_matches, _uri, _pathParameters);
int get hashCode => Object.hash(matches, uri, pathParameters);
}

/// An error that occurred during matching.
Expand Down
18 changes: 16 additions & 2 deletions packages/go_router/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
) {
late final RouteMatchList initialMatches;
try {
initialMatches = matcher.findMatch(routeInformation.location!,
extra: routeInformation.state);
if (routeInformation is PreParsedRouteInformation) {
initialMatches = routeInformation.matchlist;
} else {
initialMatches = matcher.findMatch(routeInformation.location!,
extra: routeInformation.state);
}
} on MatcherError {
log.info('No initial matches: ${routeInformation.location}');

Expand Down Expand Up @@ -124,3 +128,13 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
);
}
}

/// Pre-parsed [RouteInformation] that contains a [RouteMatchList].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc should also mention the how parser will treat the class instances if customer uses this

class PreParsedRouteInformation extends RouteInformation {
/// Creates a [PreParsedRouteInformation].
PreParsedRouteInformation(
{super.location, super.state, required this.matchlist});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause issue with state restoration, but I think we should update the router to be able to handle RouteInformation subclass.

We should at least check the location generated from matchlist is the same as the input location though

Copy link
Contributor

@chunhtai chunhtai Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Can we create a special class to insert both extra and matchlist into RouteInformation.state? and the parser can be smart enough to read the class and separate this two before it proceed to parsing. That way we don't need to worry about subclass issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on state restoration now, and I think we perhaps may have to handle that in a separate StatefulWidget (added in RouteBuilder), since we probably need to serialize the location (graph) of the matchlists as strings somehow, and then parse them upon restoration. But we'll see.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serialize the location (graph) of the matchlists as strings somehow

yes, think so. but I don't think we need a separate StatefulWidget as long as we are able to rebuild the widget with the RouteInformation


/// The pre-parsed [RouteMatchList].
final RouteMatchList matchlist;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matchList.

}
Loading