Skip to content

Commit

Permalink
feat(navigation): expose the previous route in navigation service (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
buehler committed May 13, 2024
1 parent 03a69bd commit 087cea6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/fluorflow/lib/src/navigation/navigation_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ class NavigationService {

/// Returns the current route name (or an empty string if no route
/// matches).
/// The whole route object (for more advanced usecases) can be accessed
/// via the `currentRoute` property of the observer.
String get currentRoute => _observer.currentRoute?.settings.name ?? '';

/// Returns the current route arguments (`dynamic` typed).
dynamic get currentArguments => _observer.currentRoute?.settings.arguments;

/// Returns the previous route name (or `null` if no previous route exists).
/// The whole route object (for more advanced usecases) can be accessed
/// via the `previousRoute` property of the observer.
String? get previousRoute => _observer.previousRoute?.settings.name;

/// Navigate "back" and return an optional result.
void back<T>([T? result]) => navigatorKey.currentState?.canPop() == true
? navigatorKey.currentState!.pop(result)
Expand Down
13 changes: 13 additions & 0 deletions packages/fluorflow/lib/src/navigation/navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ final class FluorflowNavigatorObserver extends NavigatorObserver {
Route<dynamic>? get currentRoute =>
_history.where((r) => r.isCurrent).firstOrNull;

Route<dynamic>? get previousRoute {
if (_history.length < 2) {
return null;
}

final current = currentRoute;
if (current == null) {
return null;
}

return _history[_history.indexOf(current) - 1];
}

@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
_history.add(route);
Expand Down
21 changes: 21 additions & 0 deletions packages/fluorflow/test/navigation/navigation_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ void main() {
expect(nav.currentArguments['foo'], 'bar');
});

testWidgets('should return no previous route if none exists.',
(tester) async {
await tester.pumpWidget(_app());
await tester.pumpAndSettle();

const nav = NavigationService();

expect(nav.previousRoute, null);
});

testWidgets('should return previous route.', (tester) async {
await tester.pumpWidget(_app());
await tester.pumpAndSettle();

const nav = NavigationService();
nav.navigateTo('/two');
await tester.pumpAndSettle();

expect(nav.previousRoute, '/one');
});

testWidgets('should correctly navigate back.', (tester) async {
await tester.pumpWidget(_app());
await tester.pumpAndSettle();
Expand Down

0 comments on commit 087cea6

Please sign in to comment.