-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[go_router] Fix ShellRoutes break iOS swipe back navigation #9968
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
Open
LukasMirbt
wants to merge
5
commits into
flutter:main
Choose a base branch
from
LukasMirbt:go-router/shell-route-ios-back-gesture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6a8c49e
Fix issue where ShellRoutes break iOS swipe back navigation
LukasMirbt c3c3f87
Add tests for Android back button
LukasMirbt a844fde
Add TODO
LukasMirbt e1fc828
Merge branch 'main' into go-router/shell-route-ios-back-gesture
LukasMirbt feecfee
Update changelog and version
LukasMirbt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
packages/go_router/test/shell_route_system_back_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
import 'test_helpers.dart'; | ||
|
||
// Regression test for https://github.com/flutter/flutter/issues/120353 | ||
void main() { | ||
LukasMirbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
group('iOS back gesture inside a ShellRoute', () { | ||
testWidgets('pops the top sub-route ' | ||
'when there is an active sub-route', (WidgetTester tester) async { | ||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS; | ||
|
||
await tester.pumpWidget(const _TestApp()); | ||
expect(find.text('Home'), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FilledButton)); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Post'), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FilledButton)); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Comment'), findsOneWidget); | ||
|
||
await simulateIosBackGesture(tester); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Post'), findsOneWidget); | ||
|
||
debugDefaultTargetPlatformOverride = null; | ||
}); | ||
|
||
testWidgets('pops ShellRoute ' | ||
'when there are no active sub-routes', (WidgetTester tester) async { | ||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS; | ||
|
||
await tester.pumpWidget(const _TestApp()); | ||
expect(find.text('Home'), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FilledButton)); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Post'), findsOneWidget); | ||
|
||
await simulateIosBackGesture(tester); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Home'), findsOneWidget); | ||
|
||
debugDefaultTargetPlatformOverride = null; | ||
}); | ||
}); | ||
|
||
group('Android back button inside a ShellRoute', () { | ||
testWidgets('pops the top sub-route ' | ||
'when there is an active sub-route', (WidgetTester tester) async { | ||
await tester.pumpWidget(const _TestApp()); | ||
expect(find.text('Home'), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FilledButton)); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Post'), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FilledButton)); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Comment'), findsOneWidget); | ||
|
||
await simulateAndroidBackButton(tester); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Post'), findsOneWidget); | ||
}); | ||
|
||
testWidgets('pops ShellRoute ' | ||
'when there are no active sub-routes', (WidgetTester tester) async { | ||
await tester.pumpWidget(const _TestApp()); | ||
expect(find.text('Home'), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FilledButton)); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Post'), findsOneWidget); | ||
|
||
await simulateAndroidBackButton(tester); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('Home'), findsOneWidget); | ||
}); | ||
}); | ||
} | ||
|
||
class _TestApp extends StatefulWidget { | ||
const _TestApp(); | ||
|
||
@override | ||
State<_TestApp> createState() => _TestAppState(); | ||
} | ||
|
||
class _TestAppState extends State<_TestApp> { | ||
final GoRouter _router = GoRouter( | ||
routes: <GoRoute>[ | ||
GoRoute( | ||
path: '/', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Home')), | ||
body: Center( | ||
child: FilledButton( | ||
onPressed: () { | ||
GoRouter.of(context).go('/post'); | ||
}, | ||
child: const Text('Go to Post'), | ||
), | ||
), | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
ShellRoute( | ||
builder: (BuildContext context, GoRouterState state, Widget child) { | ||
return child; | ||
}, | ||
routes: <GoRoute>[ | ||
GoRoute( | ||
path: '/post', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Post')), | ||
body: Center( | ||
child: FilledButton( | ||
onPressed: () { | ||
GoRouter.of(context).go('/post/comment'); | ||
}, | ||
child: const Text('Comment'), | ||
), | ||
), | ||
); | ||
}, | ||
routes: <GoRoute>[ | ||
GoRoute( | ||
path: 'comment', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Comment')), | ||
); | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
); | ||
|
||
@override | ||
void dispose() { | ||
_router.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router(routerConfig: _router); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@justinmc is this the right way to prevent an iOS backgesture? or should we use NavigatorPopHandler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like Justin is on vacation. Is there anybody else we can loop in or should we wait for him?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's wait for him. I "think" we should use
NavigatorPopHandler
and I also remember Justin was also fixing something about the nested navigator in go_routerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LukasMirbt Could you check if flutter/flutter#152330 fixes this issue? If so I'd prefer to wait for that PR. If not, we should confirm that any fix made inside of go_router here will still work after that PR lands.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@justinmc flutter/flutter#152330 does seem to fix this issue 🎉
However, I don't see a compelling reason to wait for your PR since it will most likely take a couple of months before it hits stable, even once it's merged, and the issue linked in this PR has over 50 upvotes.
All the
go_router
tests + regression tests seem to pass (on your fork) both with and without the fix added in this PR. I propose we add a TODO to remove thePopScope
once your fix has hit stable and merge this PR in the meantime.Let me know if you disagree!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the change here and flutter change coexist? If so, can you add a test to make sure android back button work correctly so that when stable roll into package won't break go_router?
Otherwise, adding a todo and merge this now sgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it seems like the fix is forward compatible 👍 The tests are passing with flutter/flutter#152330.
Done ✅
TODO added as well!