Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add current state to router
  • Loading branch information
cedvdb committed Sep 15, 2024
commit dfa514d2c945ceaa9ba5a34ba9e7134ecb407510
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 @@ -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';
Expand Down Expand Up @@ -167,6 +168,10 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
}());
}

/// The current [GoRouterState]
GoRouterState? get currentState => currentConfiguration.last
.buildState(_configuration, currentConfiguration);

/// 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
47 changes: 47 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,53 @@ void main() {
),
);
});

testWidgets(
'should return the current GoRouterState when router.currentState is called',
(WidgetTester tester) async {
final List<GoRoute> routes = <GoRoute>[
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<ValueNotifier<String>> {
Expand Down