Skip to content

Commit da4c3ee

Browse files
authored
[go_router] add current state getter (flutter#7651)
Adds a current state getter that returns `GoRouterState`. I've no idea if this is correct but the added tests seem to be passing. Rebuilding the state may be expensive too ? Fix: flutter#129833 fixes flutter#148215
1 parent 6276210 commit da4c3ee

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
## 14.4.0
2+
3+
- Adds current state getter on `GoRouter` that returns the current `GoRouterState`.
4+
15
## 14.3.0
26

3-
- Added missing implementation for the routerNeglect parameter in GoRouter.
7+
- Adds missing implementation for the routerNeglect parameter in GoRouter.
48

59
## 14.2.9
610

packages/go_router/lib/src/delegate.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'configuration.dart';
1313
import 'match.dart';
1414
import 'misc/errors.dart';
1515
import 'route.dart';
16+
import 'state.dart';
1617

1718
/// GoRouter implementation of [RouterDelegate].
1819
class GoRouterDelegate extends RouterDelegate<RouteMatchList>
@@ -169,6 +170,11 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
169170
}());
170171
}
171172

173+
/// The top [GoRouterState], the state of the route that was
174+
/// last used in either [GoRouter.go] or [GoRouter.push].
175+
GoRouterState? get state => currentConfiguration.last
176+
.buildState(_configuration, currentConfiguration);
177+
172178
/// For use by the Router architecture as part of the RouterDelegate.
173179
GlobalKey<NavigatorState> get navigatorKey => _configuration.navigatorKey;
174180

packages/go_router/lib/src/router.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ class GoRouter implements RouterConfig<RouteMatchList> {
255255
}());
256256
}
257257

258+
/// The top [GoRouterState], the state of the route that was
259+
/// last used in either [GoRouter.go] or [GoRouter.push].
260+
///
261+
/// Accessing this property via GoRouter.of(context).state will not
262+
/// cause rebuild if the state has changed, consider using
263+
/// GoRouterState.of(context) instead.
264+
GoRouterState? get state => routerDelegate.state;
265+
258266
/// Whether the imperative API affects browser URL bar.
259267
///
260268
/// The Imperative APIs refer to [push], [pushReplacement], or [replace].

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 14.3.0
4+
version: 14.4.0
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/go_router_test.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5474,6 +5474,78 @@ void main() {
54745474
);
54755475
});
54765476

5477+
testWidgets(
5478+
'should return the current GoRouterState when router.currentState is called',
5479+
(WidgetTester tester) async {
5480+
final List<RouteBase> routes = <RouteBase>[
5481+
GoRoute(
5482+
name: 'home',
5483+
path: '/',
5484+
builder: (BuildContext context, GoRouterState state) =>
5485+
const HomeScreen()),
5486+
GoRoute(
5487+
name: 'books',
5488+
path: '/books',
5489+
builder: (BuildContext context, GoRouterState state) =>
5490+
const Text('books')),
5491+
GoRoute(
5492+
name: 'boats',
5493+
path: '/boats',
5494+
builder: (BuildContext context, GoRouterState state) =>
5495+
const Text('boats')),
5496+
ShellRoute(
5497+
builder: (BuildContext context, GoRouterState state, Widget child) =>
5498+
child,
5499+
routes: <RouteBase>[
5500+
GoRoute(
5501+
name: 'tulips',
5502+
path: '/tulips',
5503+
builder: (BuildContext context, GoRouterState state) =>
5504+
const Text('tulips'),
5505+
),
5506+
],
5507+
)
5508+
];
5509+
5510+
final GoRouter router = await createRouter(routes, tester);
5511+
await tester.pumpAndSettle();
5512+
5513+
GoRouterState? state = router.state;
5514+
expect(state?.name, 'home');
5515+
expect(state?.fullPath, '/');
5516+
5517+
router.go('/books');
5518+
await tester.pumpAndSettle();
5519+
state = router.state;
5520+
expect(state?.name, 'books');
5521+
expect(state?.fullPath, '/books');
5522+
5523+
router.push('/boats');
5524+
await tester.pumpAndSettle();
5525+
state = router.state;
5526+
expect(state?.name, 'boats');
5527+
expect(state?.fullPath, '/boats');
5528+
5529+
router.pop();
5530+
await tester.pumpAndSettle();
5531+
state = router.state;
5532+
expect(state?.name, 'books');
5533+
expect(state?.fullPath, '/books');
5534+
5535+
router.go('/tulips');
5536+
await tester.pumpAndSettle();
5537+
state = router.state;
5538+
expect(state?.name, 'tulips');
5539+
expect(state?.fullPath, '/tulips');
5540+
5541+
router.go('/books');
5542+
router.push('/tulips');
5543+
await tester.pumpAndSettle();
5544+
state = router.state;
5545+
expect(state?.name, 'tulips');
5546+
expect(state?.fullPath, '/tulips');
5547+
});
5548+
54775549
testWidgets('should allow route paths without leading /',
54785550
(WidgetTester tester) async {
54795551
final List<GoRoute> routes = <GoRoute>[

0 commit comments

Comments
 (0)