From e581489da8e0e866e48a448c62b3845315fca005 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Wed, 20 Jul 2022 19:58:55 +0900 Subject: [PATCH 1/6] [go_router] Allow any number of the same page on the stack --- packages/go_router/lib/src/delegate.dart | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/delegate.dart b/packages/go_router/lib/src/delegate.dart index 1af969c5376..38d0bb20b1d 100644 --- a/packages/go_router/lib/src/delegate.dart +++ b/packages/go_router/lib/src/delegate.dart @@ -45,10 +45,27 @@ class GoRouterDelegate extends RouterDelegate final GlobalKey _key = GlobalKey(); RouteMatchList _matches = RouteMatchList.empty(); + final Map _pushCounts = {}; /// Push the given location onto the page stack void push(RouteMatch match) { - _matches.push(match); + // remap the pageKey so allow any number of the same page on the stack + final String fullPath = match.fullpath; + final int count = (_pushCounts[fullPath] ?? 0) + 1; + _pushCounts[fullPath] = count; + final ValueKey pageKey = ValueKey('$fullPath-p$count'); + final RouteMatch newPageKeyMatch = RouteMatch( + route: match.route, + subloc: match.subloc, + fullpath: match.fullpath, + encodedParams: match.encodedParams, + queryParams: match.queryParams, + extra: match.extra, + error: match.error, + pageKey: pageKey, + ); + + _matches.push(newPageKeyMatch); notifyListeners(); } From f276569ac4865dfef6efc81f0b8ff02efa057839 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Wed, 20 Jul 2022 20:25:54 +0900 Subject: [PATCH 2/6] [go_router] Update pubspec and CHANGELOG --- packages/go_router/CHANGELOG.md | 4 ++++ packages/go_router/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 9ce2c9e015b..2069f74113b 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.2.2 + +- Fixes a bug where the ValueKey to be the same when a page was pushed multiple times. + ## 4.2.1 - Refactors internal classes and methods diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 1433f92d905..4c8da3d9c5f 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -1,7 +1,7 @@ name: go_router description: A declarative router for Flutter based on Navigation 2 supporting deep linking, data-driven routes and more -version: 4.2.1 +version: 4.2.2 repository: https://github.com/flutter/packages/tree/main/packages/go_router issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22 From 12388eb591d0544c360d3f3880f69879a57acf7f Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Wed, 20 Jul 2022 21:33:04 +0900 Subject: [PATCH 3/6] [go_router] Add test --- packages/go_router/test/delegate_test.dart | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/go_router/test/delegate_test.dart b/packages/go_router/test/delegate_test.dart index a85d53668a3..e57d56cc90d 100644 --- a/packages/go_router/test/delegate_test.dart +++ b/packages/go_router/test/delegate_test.dart @@ -16,6 +16,7 @@ Future createGoRouter( initialLocation: '/', routes: [ GoRoute(path: '/', builder: (_, __) => const DummyStatefulWidget()), + GoRoute(path: '/a', builder: (_, __) => const DummyStatefulWidget()), GoRoute( path: '/error', builder: (_, __) => const ErrorScreen(null), @@ -56,6 +57,34 @@ void main() { }); }); + group('push', () { + testWidgets( + 'It should return different pageKey when push is called', + (WidgetTester tester) async { + final GoRouter goRouter = await createGoRouter(tester); + expect(goRouter.routerDelegate.matches.matches.length, 1); + expect( + goRouter.routerDelegate.matches.matches[0].pageKey, + null, + ); + + goRouter.push('/a'); + expect(goRouter.routerDelegate.matches.matches.length, 2); + expect( + goRouter.routerDelegate.matches.matches[1].pageKey, + const Key('/a-p1'), + ); + + goRouter.push('/a'); + expect(goRouter.routerDelegate.matches.matches.length, 3); + expect( + goRouter.routerDelegate.matches.matches[2].pageKey, + const Key('/a-p2'), + ); + }, + ); + }); + group('canPop', () { testWidgets( 'It should return false if there is only 1 match in the stack', From feb193d52497c1594e248a789e00094caaf45999 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Tue, 26 Jul 2022 08:57:22 +0900 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/lib/src/delegate.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go_router/lib/src/delegate.dart b/packages/go_router/lib/src/delegate.dart index 38d0bb20b1d..29f0f251f33 100644 --- a/packages/go_router/lib/src/delegate.dart +++ b/packages/go_router/lib/src/delegate.dart @@ -47,9 +47,9 @@ class GoRouterDelegate extends RouterDelegate RouteMatchList _matches = RouteMatchList.empty(); final Map _pushCounts = {}; - /// Push the given location onto the page stack + /// Pushes the given location onto the page stack void push(RouteMatch match) { - // remap the pageKey so allow any number of the same page on the stack + // Remap the pageKey to allow any number of the same page on the stack final String fullPath = match.fullpath; final int count = (_pushCounts[fullPath] ?? 0) + 1; _pushCounts[fullPath] = count; From 2ee5fd6b94869021ad5702b15736ada948e3d1b7 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Tue, 26 Jul 2022 09:00:16 +0900 Subject: [PATCH 5/6] [go_router] Add await tester.pumpAndSettle() to verify --- packages/go_router/test/delegate_test.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/go_router/test/delegate_test.dart b/packages/go_router/test/delegate_test.dart index e57d56cc90d..b4a417368e0 100644 --- a/packages/go_router/test/delegate_test.dart +++ b/packages/go_router/test/delegate_test.dart @@ -69,6 +69,8 @@ void main() { ); goRouter.push('/a'); + await tester.pumpAndSettle(); + expect(goRouter.routerDelegate.matches.matches.length, 2); expect( goRouter.routerDelegate.matches.matches[1].pageKey, @@ -76,6 +78,8 @@ void main() { ); goRouter.push('/a'); + await tester.pumpAndSettle(); + expect(goRouter.routerDelegate.matches.matches.length, 3); expect( goRouter.routerDelegate.matches.matches[2].pageKey, From 4496b4fb1fe9f05684917ad2d41dc17b56fc12e2 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Sat, 30 Jul 2022 22:23:49 +0900 Subject: [PATCH 6/6] [go_router] Fix conflicts --- packages/go_router/CHANGELOG.md | 4 ++++ packages/go_router/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 2ddd57a06ea..465df984b23 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.2.3 + +- Fixes a bug where the ValueKey to be the same when a page was pushed multiple times. + ## 4.2.2 - Fixes a bug where go_router_builder wasn't detecting annotations. diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 4c8da3d9c5f..0b2d6f0279f 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -1,7 +1,7 @@ name: go_router description: A declarative router for Flutter based on Navigation 2 supporting deep linking, data-driven routes and more -version: 4.2.2 +version: 4.2.3 repository: https://github.com/flutter/packages/tree/main/packages/go_router issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22