-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Refactors page API #137792
Refactors page API #137792
Conversation
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie or stuartmorgan on the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
I will add test later if this approach looks good |
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.
I've been away from predictive back for awhile, but this approach looks good to me. It seems a lot simpler with no onPopPage and the same canPop
and onPopInvoked
API everywhere. Especially because it works with system backs out of the box as users will expect.
Thank you for refactoring this!
), | ||
library: 'widget library', | ||
stack: StackTrace.current, | ||
bool _debugCheckPageApiParameters() { |
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.
Good call pulling this out into a function.
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.
Nice reafactoring, indeed!
/// contain the [Page] for the given [Route]. The next time the [pages] list | ||
/// is updated, if the [Page] corresponding to this [Route] is still present, | ||
/// it will be interpreted as a new route to display. | ||
final DidRemovePageCallback? onDidRemovePage; |
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.
The name sounds good to me.
@@ -636,6 +662,28 @@ abstract class Page<T> extends RouteSettings { | |||
/// Flutter. | |||
final String? restorationId; | |||
|
|||
/// Called after a pop on the associated route was handled. |
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.
Nit: Is there anything that could be deduplicated with a macro in these docs? Here and for canPop
.
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.
the content is slightly different. I can't find a good way to dedup these doc
@justinmc There is one thing I am not too sure about the popscope. Is there a way for PopScope to know what result the original pop used? For example if the pop is generated by Navigator.maybePop('some text'), if you want to launch a confirmation dialog in popScope, there isn't seem to be a way to get the string as pop result if user click confirm |
dc9e288
to
db0ea04
Compare
db0ea04
to
26af71d
Compare
), | ||
library: 'widget library', | ||
stack: StackTrace.current, | ||
bool _debugCheckPageApiParameters() { |
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.
Nice reafactoring, indeed!
26af71d
to
0fbd1eb
Compare
0fbd1eb
to
595de72
Compare
595de72
to
95405e7
Compare
Hi 👋 Any idea when this might land? 🙏 |
I pasted an example below that hopefully covers what you mean. I don't think you can get the result of the pop that triggered onPopInvoked from inside of onPopInvoked. I'm not sure we want to enable that. I think that's the same as it was with WillPopScope. See the example, where I show the dialog from inside of onPopInvoked and receive the result there. Exampleimport 'package:flutter/material.dart';
void main() => runApp(const NavigatorPopHandlerApp());
class NavigatorPopHandlerApp extends StatelessWidget {
const NavigatorPopHandlerApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/home',
routes: <String, WidgetBuilder>{
'/home': (BuildContext context) => const _HomePage(),
'/two': (BuildContext context) => const _PageTwo(),
},
);
}
}
class _HomePage extends StatefulWidget {
const _HomePage();
@override
State<_HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<_HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page One'),
TextButton(
onPressed: () {
Navigator.of(context).pushNamed('/two');
},
child: const Text('Next page'),
),
],
),
),
);
}
}
class _PageTwo extends StatefulWidget {
const _PageTwo();
@override
State<_PageTwo> createState() => _PageTwoState();
}
class _PageTwoState extends State<_PageTwo> {
Future<bool?> _showBackDialog() {
return showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Are you sure?'),
content: const Text(
'Are you sure you want to leave this page?',
),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Nevermind'),
onPressed: () {
Navigator.pop(context, false);
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Leave'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page Two'),
PopScope(
canPop: false,
onPopInvoked: (bool didPop) async {
if (didPop) {
return;
}
final NavigatorState navigator = Navigator.of(context);
final bool? shouldPop = await _showBackDialog();
if (shouldPop ?? false) {
navigator.pop();
}
},
child: TextButton(
onPressed: () {
_showBackDialog();
},
child: const Text('Go back'),
),
),
],
),
),
);
}
} |
@btrautmann The author is on leave until April, but maybe I can take this over if people need it. |
See this example. void main() {
runApp(MaterialApp(
home: Home(),
));
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: () async {
final myMessage = await Navigator.of(context).push(MaterialPageRoute(builder: (_) => MyWidget()));
print(myMessage);
},
child: const Text('push'),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page Two'),
PopScope(
canPop: false,
onPopInvoked: (bool didPop) async {
if (didPop) {
return;
}
final NavigatorState navigator = Navigator.of(context);
final bool? shouldPop = await _showBackDialog();
if (shouldPop ?? false) {
// I want to be able to pass original result 'my result' to the pending future in Home!!
}
},
child: TextButton(
onPressed: () {
Navigator.pop(context, 'my result');
},
child: const Text('Go back'),
),
),
],
),
),
);
}
} I have a pr up for adding this feature #139164 |
@chunhtai Thanks, I see what you mean. Indeed I don't think it's possible to do that right now without your PR. I think we should put both PRs on hold for now until you get back. |
This is ready for review @justinmc @goderbauer |
I've just encountered this issue, and it coincided with an update; I'm waiting online. lol. |
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.
LGTM with nits 👍
@@ -610,6 +633,15 @@ class RouteSettings { | |||
/// The type argument `T` is the corresponding [Route]'s return type, as | |||
/// used by [Route.currentResult], [Route.popped], and [Route.didPop]. | |||
/// | |||
/// The [canPop] and [onPopInvoked] are used for intercepting pops. |
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.
"The [canPop] and [onPopInvoked]" => "The [canPop] and [onPopInvoked] parameters"
/// The [canPop] and [onPopInvoked] are used for intercepting pops. | ||
/// | ||
/// {@tool dartpad} | ||
/// This sample demonstrates how to use this [canPop] and [onPopInvoked] to |
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.
"this [canPop] and [onPopInvoked]" => "the [canPop] and [onPopInvoked] parameters"
return Navigator( | ||
key: navigatorKey, | ||
pages: _getPages(), | ||
onDidRemovePage: (Page<Object?> page) { |
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.
So this is called when a system back happens I guess?
' The Navigator.onPopPage must be provided to use the\n' | ||
' Navigator.pages API\n', | ||
' Either onDidRemovePage or onPopPage must be provided to use the\n' | ||
' Navigator.pages API but not both.\n', |
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.
Anything else that should be tested here besides the example tests?
migration guide for flutter/flutter#137792 ## Presubmit checklist - [ ] This PR doesn’t contain automatically generated corrections (Grammarly or similar). - [ ] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [ ] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer.
auto label is removed for flutter/flutter/137792, due to - The status or check suite Linux web_skwasm_tests_6 has failed. Please fix the issues identified (or deflake) before re-applying this label. |
auto label is removed for flutter/flutter/137792, due to - The status or check suite Windows build_tests_4_7 has failed. Please fix the issues identified (or deflake) before re-applying this label. |
flutter/flutter@1255435...d2da1b2 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 491f460a25fb to 7bf865774d06 (1 revision) (flutter/flutter#148325) 2024-05-14 engine-flutter-autoroll@skia.org Roll Packages from 1412041 to fd714bd (1 revision) (flutter/flutter#148324) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from c381b852605f to 491f460a25fb (1 revision) (flutter/flutter#148319) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from b4b798d2e706 to c381b852605f (1 revision) (flutter/flutter#148303) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 0de6701b537a to b4b798d2e706 (2 revisions) (flutter/flutter#148299) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 764c33c3c773 to 0de6701b537a (2 revisions) (flutter/flutter#148297) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 797eab742925 to 764c33c3c773 (1 revision) (flutter/flutter#148290) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 84687fe0f199 to 797eab742925 (1 revision) (flutter/flutter#148288) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from bee398d95abe to 84687fe0f199 (3 revisions) (flutter/flutter#148282) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from a1cdcb6a6687 to bee398d95abe (2 revisions) (flutter/flutter#148280) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 13a561cb6d5a to a1cdcb6a6687 (1 revision) (flutter/flutter#148276) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 7dcbd93e5c1a to 13a561cb6d5a (6 revisions) (flutter/flutter#148274) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from aeff9b174c84 to 7dcbd93e5c1a (1 revision) (flutter/flutter#148266) 2024-05-13 magder@google.com Mark platform_views_scroll_perf_ad_banners__timeline_summary not flaky (flutter/flutter#148263) 2024-05-13 andrewrkolos@gmail.com add more print traces in hot runner workflow (flutter/flutter#148258) 2024-05-13 47866232+chunhtai@users.noreply.github.com Refactors page API (flutter/flutter#137792) 2024-05-13 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.3 to 3.25.5 (flutter/flutter#148262) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 11502404a52a to aeff9b174c84 (2 revisions) (flutter/flutter#148260) 2024-05-13 73608287+ellet0@users.noreply.github.com Use super.key instead of manually passing the Key parameter to the parent class (flutter/flutter#147621) 2024-05-13 dacoharkes@google.com Try fix module test (flutter/flutter#147934) 2024-05-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Reland Native ios context menu (#143002) (#148238)" (flutter/flutter#148254) 2024-05-13 jmccandless@google.com Reland Native ios context menu (#143002) (flutter/flutter#148238) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 636374fd00ee to 11502404a52a (1 revision) (flutter/flutter#148242) 2024-05-13 engine-flutter-autoroll@skia.org Roll Packages from 6c4482a to 1412041 (16 revisions) (flutter/flutter#148239) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 0050bf9a8094 to 636374fd00ee (1 revision) (flutter/flutter#148216) 2024-05-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Native ios context menu (#143002)" (flutter/flutter#148237) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC rmistry@google.com,stuartmorgan@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…r#6729) flutter/flutter@1255435...d2da1b2 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 491f460a25fb to 7bf865774d06 (1 revision) (flutter/flutter#148325) 2024-05-14 engine-flutter-autoroll@skia.org Roll Packages from 1412041 to fd714bd (1 revision) (flutter/flutter#148324) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from c381b852605f to 491f460a25fb (1 revision) (flutter/flutter#148319) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from b4b798d2e706 to c381b852605f (1 revision) (flutter/flutter#148303) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 0de6701b537a to b4b798d2e706 (2 revisions) (flutter/flutter#148299) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 764c33c3c773 to 0de6701b537a (2 revisions) (flutter/flutter#148297) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 797eab742925 to 764c33c3c773 (1 revision) (flutter/flutter#148290) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 84687fe0f199 to 797eab742925 (1 revision) (flutter/flutter#148288) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from bee398d95abe to 84687fe0f199 (3 revisions) (flutter/flutter#148282) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from a1cdcb6a6687 to bee398d95abe (2 revisions) (flutter/flutter#148280) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 13a561cb6d5a to a1cdcb6a6687 (1 revision) (flutter/flutter#148276) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 7dcbd93e5c1a to 13a561cb6d5a (6 revisions) (flutter/flutter#148274) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from aeff9b174c84 to 7dcbd93e5c1a (1 revision) (flutter/flutter#148266) 2024-05-13 magder@google.com Mark platform_views_scroll_perf_ad_banners__timeline_summary not flaky (flutter/flutter#148263) 2024-05-13 andrewrkolos@gmail.com add more print traces in hot runner workflow (flutter/flutter#148258) 2024-05-13 47866232+chunhtai@users.noreply.github.com Refactors page API (flutter/flutter#137792) 2024-05-13 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.3 to 3.25.5 (flutter/flutter#148262) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 11502404a52a to aeff9b174c84 (2 revisions) (flutter/flutter#148260) 2024-05-13 73608287+ellet0@users.noreply.github.com Use super.key instead of manually passing the Key parameter to the parent class (flutter/flutter#147621) 2024-05-13 dacoharkes@google.com Try fix module test (flutter/flutter#147934) 2024-05-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Reland Native ios context menu (#143002) (#148238)" (flutter/flutter#148254) 2024-05-13 jmccandless@google.com Reland Native ios context menu (#143002) (flutter/flutter#148238) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 636374fd00ee to 11502404a52a (1 revision) (flutter/flutter#148242) 2024-05-13 engine-flutter-autoroll@skia.org Roll Packages from 6c4482a to 1412041 (16 revisions) (flutter/flutter#148239) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 0050bf9a8094 to 636374fd00ee (1 revision) (flutter/flutter#148216) 2024-05-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Native ios context menu (#143002)" (flutter/flutter#148237) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC rmistry@google.com,stuartmorgan@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…r#6729) flutter/flutter@1255435...d2da1b2 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 491f460a25fb to 7bf865774d06 (1 revision) (flutter/flutter#148325) 2024-05-14 engine-flutter-autoroll@skia.org Roll Packages from 1412041 to fd714bd (1 revision) (flutter/flutter#148324) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from c381b852605f to 491f460a25fb (1 revision) (flutter/flutter#148319) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from b4b798d2e706 to c381b852605f (1 revision) (flutter/flutter#148303) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 0de6701b537a to b4b798d2e706 (2 revisions) (flutter/flutter#148299) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 764c33c3c773 to 0de6701b537a (2 revisions) (flutter/flutter#148297) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 797eab742925 to 764c33c3c773 (1 revision) (flutter/flutter#148290) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 84687fe0f199 to 797eab742925 (1 revision) (flutter/flutter#148288) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from bee398d95abe to 84687fe0f199 (3 revisions) (flutter/flutter#148282) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from a1cdcb6a6687 to bee398d95abe (2 revisions) (flutter/flutter#148280) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 13a561cb6d5a to a1cdcb6a6687 (1 revision) (flutter/flutter#148276) 2024-05-14 engine-flutter-autoroll@skia.org Roll Flutter Engine from 7dcbd93e5c1a to 13a561cb6d5a (6 revisions) (flutter/flutter#148274) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from aeff9b174c84 to 7dcbd93e5c1a (1 revision) (flutter/flutter#148266) 2024-05-13 magder@google.com Mark platform_views_scroll_perf_ad_banners__timeline_summary not flaky (flutter/flutter#148263) 2024-05-13 andrewrkolos@gmail.com add more print traces in hot runner workflow (flutter/flutter#148258) 2024-05-13 47866232+chunhtai@users.noreply.github.com Refactors page API (flutter/flutter#137792) 2024-05-13 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.25.3 to 3.25.5 (flutter/flutter#148262) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 11502404a52a to aeff9b174c84 (2 revisions) (flutter/flutter#148260) 2024-05-13 73608287+ellet0@users.noreply.github.com Use super.key instead of manually passing the Key parameter to the parent class (flutter/flutter#147621) 2024-05-13 dacoharkes@google.com Try fix module test (flutter/flutter#147934) 2024-05-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Reland Native ios context menu (#143002) (#148238)" (flutter/flutter#148254) 2024-05-13 jmccandless@google.com Reland Native ios context menu (#143002) (flutter/flutter#148238) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 636374fd00ee to 11502404a52a (1 revision) (flutter/flutter#148242) 2024-05-13 engine-flutter-autoroll@skia.org Roll Packages from 6c4482a to 1412041 (16 revisions) (flutter/flutter#148239) 2024-05-13 engine-flutter-autoroll@skia.org Roll Flutter Engine from 0050bf9a8094 to 636374fd00ee (1 revision) (flutter/flutter#148216) 2024-05-13 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Native ios context menu (#143002)" (flutter/flutter#148237) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC rmistry@google.com,stuartmorgan@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
fixes #137458
Chagnes:
migration guide flutter/website#10523
Pre-launch Checklist
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.