Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 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,11 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
}());
}

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

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

Expand Down
4 changes: 4 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,10 @@ class GoRouter implements RouterConfig<RouteMatchList> {
}());
}

/// 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.
///
/// 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
72 changes: 72 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,78 @@ 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.state;
expect(state?.name, 'home');
expect(state?.fullPath, '/');

router.go('/books');
await tester.pumpAndSettle();
state = router.state;
expect(state?.name, 'books');
expect(state?.fullPath, '/books');

router.push('/boats');
await tester.pumpAndSettle();
state = router.state;
expect(state?.name, 'boats');
expect(state?.fullPath, '/boats');

router.pop();
await tester.pumpAndSettle();
state = router.state;
expect(state?.name, 'books');
expect(state?.fullPath, '/books');

router.go('/tulips');
await tester.pumpAndSettle();
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');
});
}

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