From dfa514d2c945ceaa9ba5a34ba9e7134ecb407510 Mon Sep 17 00:00:00 2001 From: cedvdb Date: Sun, 15 Sep 2024 21:19:28 +0200 Subject: [PATCH 1/7] add current state to router --- packages/go_router/lib/src/delegate.dart | 5 +++ packages/go_router/lib/src/router.dart | 3 ++ packages/go_router/test/go_router_test.dart | 47 +++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/packages/go_router/lib/src/delegate.dart b/packages/go_router/lib/src/delegate.dart index 06e4f08184f4..f463fbd679f4 100644 --- a/packages/go_router/lib/src/delegate.dart +++ b/packages/go_router/lib/src/delegate.dart @@ -8,6 +8,7 @@ import 'dart:math' as math; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; +import '../go_router.dart'; import 'builder.dart'; import 'configuration.dart'; import 'match.dart'; @@ -167,6 +168,10 @@ class GoRouterDelegate extends RouterDelegate }()); } + /// The current [GoRouterState] + GoRouterState? get currentState => currentConfiguration.last + .buildState(_configuration, currentConfiguration); + /// For use by the Router architecture as part of the RouterDelegate. GlobalKey get navigatorKey => _configuration.navigatorKey; diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 2bc2057a3bbe..fa5aba968b54 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -254,6 +254,9 @@ class GoRouter implements RouterConfig { }()); } + /// The current [GoRouterState]. + GoRouterState? get currentState => routerDelegate.currentState; + /// Whether the imperative API affects browser URL bar. /// /// The Imperative APIs refer to [push], [pushReplacement], or [replace]. diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index 10e6bb58d5bd..d7d51102fd37 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5502,6 +5502,53 @@ void main() { ), ); }); + + testWidgets( + 'should return the current GoRouterState when router.currentState is called', + (WidgetTester tester) async { + final List routes = [ + GoRoute( + name: 'home', + path: '/', + builder: (BuildContext context, GoRouterState state) => + const HomeScreen()), + GoRoute( + name: 'books', + path: '/books', + builder: (BuildContext context, GoRouterState state) => + const Text('books')), + GoRoute( + name: 'boats', + path: '/boats', + builder: (BuildContext context, GoRouterState state) => + const Text('boats')), + ]; + + final GoRouter router = await createRouter(routes, tester); + await tester.pumpAndSettle(); + + GoRouterState? state = router.currentState; + expect(state?.name, 'home'); + expect(state?.fullPath, '/'); + router.go('/books'); + await tester.pumpAndSettle(); + + state = router.currentState; + expect(state?.name, 'books'); + expect(state?.fullPath, '/books'); + router.push('/boats'); + await tester.pumpAndSettle(); + + state = router.currentState; + expect(state?.name, 'boats'); + expect(state?.fullPath, '/boats'); + router.pop(); + await tester.pumpAndSettle(); + + state = router.currentState; + expect(state?.name, 'books'); + expect(state?.fullPath, '/books'); + }); } class TestInheritedNotifier extends InheritedNotifier> { From 3cab86ad871b3e10d806f75286637ec43d1d409e Mon Sep 17 00:00:00 2001 From: cedvdb Date: Sun, 15 Sep 2024 21:20:53 +0200 Subject: [PATCH 2/7] version patch --- 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 137f8d2329d4..9562a6463522 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 14.2.8 + +- Add current state getter on `GoRouter` that returns the current `GoRouterState`. + ## 14.2.7 - Fixes issue so that the parseRouteInformationWithContext can handle non-http Uris. diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 6499a9cf4f26..7c494f15aea1 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: 14.2.7 +version: 14.2.8 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 25420c3a50f980ea2428d4a07d3ba2a54b627e51 Mon Sep 17 00:00:00 2001 From: cedvdb Date: Sun, 15 Sep 2024 22:41:48 +0200 Subject: [PATCH 3/7] add shell route in test --- packages/go_router/test/go_router_test.dart | 26 +++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index d7d51102fd37..9848be7b28e3 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5506,7 +5506,7 @@ void main() { testWidgets( 'should return the current GoRouterState when router.currentState is called', (WidgetTester tester) async { - final List routes = [ + final List routes = [ GoRoute( name: 'home', path: '/', @@ -5522,6 +5522,18 @@ void main() { path: '/boats', builder: (BuildContext context, GoRouterState state) => const Text('boats')), + ShellRoute( + builder: (BuildContext context, GoRouterState state, Widget child) => + child, + routes: [ + GoRoute( + name: 'tulips', + path: '/tulips', + builder: (BuildContext context, GoRouterState state) => + const Text('tulips'), + ), + ], + ) ]; final GoRouter router = await createRouter(routes, tester); @@ -5530,24 +5542,30 @@ void main() { GoRouterState? state = router.currentState; expect(state?.name, 'home'); expect(state?.fullPath, '/'); + router.go('/books'); await tester.pumpAndSettle(); - state = router.currentState; expect(state?.name, 'books'); expect(state?.fullPath, '/books'); + router.push('/boats'); await tester.pumpAndSettle(); - state = router.currentState; expect(state?.name, 'boats'); expect(state?.fullPath, '/boats'); + router.pop(); await tester.pumpAndSettle(); - state = router.currentState; expect(state?.name, 'books'); expect(state?.fullPath, '/books'); + + router.go('/tulips'); + await tester.pumpAndSettle(); + state = router.currentState; + expect(state?.name, 'tulips'); + expect(state?.fullPath, '/tulips'); }); } From 8c7ae7a3d34fceeef0cbc01496263597274ac09a Mon Sep 17 00:00:00 2001 From: cedvdb Date: Sun, 15 Sep 2024 22:43:31 +0200 Subject: [PATCH 4/7] fix import --- packages/go_router/lib/src/delegate.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/delegate.dart b/packages/go_router/lib/src/delegate.dart index f463fbd679f4..7fc58fe130b8 100644 --- a/packages/go_router/lib/src/delegate.dart +++ b/packages/go_router/lib/src/delegate.dart @@ -8,12 +8,12 @@ import 'dart:math' as math; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; -import '../go_router.dart'; import 'builder.dart'; import 'configuration.dart'; import 'match.dart'; import 'misc/errors.dart'; import 'route.dart'; +import 'state.dart'; /// GoRouter implementation of [RouterDelegate]. class GoRouterDelegate extends RouterDelegate From 04d076ada633404a33a4b56ec95b3e112379873e Mon Sep 17 00:00:00 2001 From: cedvdb Date: Tue, 24 Sep 2024 13:07:05 +0200 Subject: [PATCH 5/7] fix comments --- packages/go_router/lib/src/delegate.dart | 6 ++++-- packages/go_router/lib/src/router.dart | 6 ++++-- packages/go_router/test/go_router_test.dart | 17 ++++++++++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/go_router/lib/src/delegate.dart b/packages/go_router/lib/src/delegate.dart index 7fc58fe130b8..d2c5080941df 100644 --- a/packages/go_router/lib/src/delegate.dart +++ b/packages/go_router/lib/src/delegate.dart @@ -168,8 +168,10 @@ class GoRouterDelegate extends RouterDelegate }()); } - /// The current [GoRouterState] - GoRouterState? get currentState => currentConfiguration.last + /// This top [GoRouterState]. + /// This returns the state of the route that was last used in + /// either [GoRouter.go] or [GoRouter.push]. + GoRouterState? get state => currentConfiguration.last .buildState(_configuration, currentConfiguration); /// For use by the Router architecture as part of the RouterDelegate. diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index fa5aba968b54..8a950a4eee83 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -254,8 +254,10 @@ class GoRouter implements RouterConfig { }()); } - /// The current [GoRouterState]. - GoRouterState? get currentState => routerDelegate.currentState; + /// This top [GoRouterState]. + /// This returns the state of the route that was last used in + /// either [GoRouter.go] or [GoRouter.push]. + GoRouterState? get state => routerDelegate.state; /// Whether the imperative API affects browser URL bar. /// diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index 9848be7b28e3..891cc8ce5a50 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5539,31 +5539,38 @@ void main() { final GoRouter router = await createRouter(routes, tester); await tester.pumpAndSettle(); - GoRouterState? state = router.currentState; + GoRouterState? state = router.state; expect(state?.name, 'home'); expect(state?.fullPath, '/'); router.go('/books'); await tester.pumpAndSettle(); - state = router.currentState; + state = router.state; expect(state?.name, 'books'); expect(state?.fullPath, '/books'); router.push('/boats'); await tester.pumpAndSettle(); - state = router.currentState; + state = router.state; expect(state?.name, 'boats'); expect(state?.fullPath, '/boats'); router.pop(); await tester.pumpAndSettle(); - state = router.currentState; + state = router.state; expect(state?.name, 'books'); expect(state?.fullPath, '/books'); router.go('/tulips'); await tester.pumpAndSettle(); - state = router.currentState; + state = router.state; + expect(state?.name, 'tulips'); + expect(state?.fullPath, '/tulips'); + + router.go('/books'); + router.push('/tulips'); + await tester.pumpAndSettle(); + state = router.state; expect(state?.name, 'tulips'); expect(state?.fullPath, '/tulips'); }); From e93ae84781004b179271fa611ce7ea1b71eac01b Mon Sep 17 00:00:00 2001 From: cedvdb Date: Tue, 24 Sep 2024 13:08:56 +0200 Subject: [PATCH 6/7] improve doc --- packages/go_router/lib/src/delegate.dart | 5 ++--- packages/go_router/lib/src/router.dart | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/go_router/lib/src/delegate.dart b/packages/go_router/lib/src/delegate.dart index d2c5080941df..0d0afac7d6d6 100644 --- a/packages/go_router/lib/src/delegate.dart +++ b/packages/go_router/lib/src/delegate.dart @@ -168,9 +168,8 @@ class GoRouterDelegate extends RouterDelegate }()); } - /// This top [GoRouterState]. - /// This returns the state of the route that was last used in - /// either [GoRouter.go] or [GoRouter.push]. + /// The top [GoRouterState], the state of the route that was + /// last used in either [GoRouter.go] or [GoRouter.push]. GoRouterState? get state => currentConfiguration.last .buildState(_configuration, currentConfiguration); diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 8a950a4eee83..5898bd27cea0 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -254,9 +254,8 @@ class GoRouter implements RouterConfig { }()); } - /// This top [GoRouterState]. - /// This returns the state of the route that was last used in - /// either [GoRouter.go] or [GoRouter.push]. + /// The top [GoRouterState], the state of the route that was + /// last used in either [GoRouter.go] or [GoRouter.push]. GoRouterState? get state => routerDelegate.state; /// Whether the imperative API affects browser URL bar. From af41cd5568241574bf3eebf8c6cac82f2bf68cf0 Mon Sep 17 00:00:00 2001 From: cedvdb Date: Sun, 3 Nov 2024 13:29:13 +0100 Subject: [PATCH 7/7] add comment --- packages/go_router/lib/src/router.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index a0f448541142..11f40f505cac 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -257,6 +257,10 @@ class GoRouter implements RouterConfig { /// The top [GoRouterState], the state of the route that was /// last used in either [GoRouter.go] or [GoRouter.push]. + /// + /// Accessing this property via GoRouter.of(context).state will not + /// cause rebuild if the state has changed, consider using + /// GoRouterState.of(context) instead. GoRouterState? get state => routerDelegate.state; /// Whether the imperative API affects browser URL bar.