Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go_router] add current state getter #7651

Merged
merged 9 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'configuration.dart';
import 'match.dart';
import 'misc/errors.dart';
import 'route.dart';
import 'state.dart';

/// GoRouter implementation of [RouterDelegate].
class GoRouterDelegate extends RouterDelegate<RouteMatchList>
Expand Down Expand Up @@ -167,6 +168,10 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
}());
}

/// The current [GoRouterState]
GoRouterState? get currentState => currentConfiguration.last
.buildState(_configuration, currentConfiguration);
cedvdb marked this conversation as resolved.
Show resolved Hide resolved

/// For use by the Router architecture as part of the RouterDelegate.
GlobalKey<NavigatorState> get navigatorKey => _configuration.navigatorKey;

Expand Down
3 changes: 3 additions & 0 deletions packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ class GoRouter implements RouterConfig<RouteMatchList> {
}());
}

/// 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].
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
65 changes: 65 additions & 0 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5502,6 +5502,71 @@ void main() {
),
);
});

testWidgets(
'should return the current GoRouterState when router.currentState is called',
(WidgetTester tester) async {
final List<RouteBase> routes = <RouteBase>[
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')),
ShellRoute(
builder: (BuildContext context, GoRouterState state, Widget child) =>
child,
routes: <RouteBase>[
GoRoute(
name: 'tulips',
path: '/tulips',
builder: (BuildContext context, GoRouterState state) =>
const Text('tulips'),
),
],
)
];

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');

router.go('/tulips');
cedvdb marked this conversation as resolved.
Show resolved Hide resolved
await tester.pumpAndSettle();
state = router.currentState;
expect(state?.name, 'tulips');
expect(state?.fullPath, '/tulips');
});
}

class TestInheritedNotifier extends InheritedNotifier<ValueNotifier<String>> {
Expand Down