Skip to content

[go_router] Implement popUntil #2728

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@
## 6.1.0

- Adds `popUntil` method to `GoRouterDelegate`, `GoRouter` and `GoRouterHelper`.

## 6.0.4

- Fixes redirection info log.
Expand Down
11 changes: 11 additions & 0 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
return true;
}

/// Calls [pop] repeatedly until the predicate returns `true`.
void popUntil(RoutePredicate predicate) {
final _NavigatorStateIterator iterator = _createNavigatorStateIterator();
while (iterator.moveNext()) {
if (iterator.current.canPop()) {
iterator.current.popUntil(predicate);
return;
}
}
}

/// Replaces the top-most page of the page stack with the given one.
///
/// See also:
Expand Down
13 changes: 13 additions & 0 deletions packages/go_router/lib/src/misc/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,21 @@ extension GoRouterHelper on BuildContext {

/// Pop the top page off the Navigator's page stack by calling
/// [Navigator.pop].
///
/// See also:
/// * [popUntil] which calls [pop] repeatedly until the predicate returns
/// `true`.
void pop<T extends Object?>([T? result]) => GoRouter.of(this).pop(result);

/// Calls [pop] repeatedly until the predicate returns true.
///
/// See also:
///
/// * [pop] which pops the top page off the Navigator's page stack by calling
/// [Navigator.pop].
void popUntil(RoutePredicate predicate) =>
GoRouter.of(this).popUntil(predicate);

/// Replaces the top-most page of the page stack with the given URL location
/// w/ optional query parameters, e.g. `/family/f2/person/p1?color=blue`.
///
Expand Down
5 changes: 5 additions & 0 deletions packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ class GoRouter extends ChangeNotifier implements RouterConfig<RouteMatchList> {
_routerDelegate.pop<T>(result);
}

/// Calls [pop] repeatedly until the predicate returns true.
void popUntil(RoutePredicate predicate) {
_routerDelegate.popUntil(predicate);
}

/// Refresh the route.
void refresh() {
assert(() {
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: 6.0.4
version: 6.1.0
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
21 changes: 21 additions & 0 deletions packages/go_router/test/delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ void main() {
});
});

group('popUntil', () {
testWidgets('It should pop 2 pages', (WidgetTester tester) async {
final GoRouter goRouter = await createGoRouter(tester)
..push('/a')
..push('/a');

await tester.pumpAndSettle();
expect(goRouter.routerDelegate.matches.matches.length, 3);
goRouter.routerDelegate.addListener(expectAsync0(count: 2, () {}));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current implementation (simply calling pop, the notifiers are notified for each pop). In this case, 2 times. Is it correct or should the router delegate notify only once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may need to refactor the code a bit, every time it notify the listener, the Gorouter will also notify its listener about location change, so if url = '/a/b/c/d/e' and popUtil to '/a'
if we notify each time it pop
goRoute will notify its listener with
'/a/b/c/d'
'/a/b/c'
'/a/b'
'/a'

instead just
'/a'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, to be correct, this test should pass with count: 1, is that correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @chunhtai , I'm not sure it will be possible to notify the listeners only once even when popping several times (and make the test pass with count: 1).

I pushed some code in 2c48f39 . Itis not correct as I guess it doesn't work with multiple nested navigators, it's more of a demonstration example. it uses directly popUntil from the NavigatorState and it still notifies the listeners twice.

Or would you know a way to make this work?

final RouteMatch second = goRouter.routerDelegate.matches.matches[1];
final RouteMatch last = goRouter.routerDelegate.matches.matches.last;

int count = 0;
goRouter.popUntil((Route<dynamic> routeMatch) => count++ >= 2);

expect(goRouter.routerDelegate.matches.matches.length, 1);
expect(goRouter.routerDelegate.matches.matches.contains(second), false);
expect(goRouter.routerDelegate.matches.matches.contains(last), false);
});
});

group('push', () {
testWidgets(
'It should return different pageKey when push is called',
Expand Down