Reapply new PopScope API (#147607)
same as previous but with soft transition on modalroute ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat [Data Driven Fixes]: https://github.com/flutter/flutter/wiki/Data-driven-Fixes
This commit is contained in:
@@ -1230,9 +1230,9 @@ class _MasterDetailFlowState extends State<_MasterDetailFlow> implements _PageOp
|
||||
}
|
||||
|
||||
MaterialPageRoute<void> _detailPageRoute(Object? arguments) {
|
||||
return MaterialPageRoute<dynamic>(builder: (BuildContext context) {
|
||||
return PopScope(
|
||||
onPopInvoked: (bool didPop) {
|
||||
return MaterialPageRoute<void>(builder: (BuildContext context) {
|
||||
return PopScope<void>(
|
||||
onPopInvokedWithResult: (bool didPop, void result) {
|
||||
// No need for setState() as rebuild happens on navigation pop.
|
||||
focus = _Focus.master;
|
||||
},
|
||||
|
||||
@@ -55,16 +55,22 @@ class Form extends StatefulWidget {
|
||||
super.key,
|
||||
required this.child,
|
||||
this.canPop,
|
||||
this.onPopInvoked,
|
||||
@Deprecated(
|
||||
'Use canPop and/or onPopInvoked instead. '
|
||||
'Use onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
this.onPopInvoked,
|
||||
this.onPopInvokedWithResult,
|
||||
@Deprecated(
|
||||
'Use canPop and/or onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.12.0-1.0.pre.',
|
||||
)
|
||||
this.onWillPop,
|
||||
this.onChanged,
|
||||
AutovalidateMode? autovalidateMode,
|
||||
}) : autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled,
|
||||
assert((onPopInvoked == null && canPop == null) || onWillPop == null, 'onWillPop is deprecated; use canPop and/or onPopInvoked.');
|
||||
assert(onPopInvokedWithResult == null || onPopInvoked == null, 'onPopInvoked is deprecated; use onPopInvokedWithResult'),
|
||||
assert(((onPopInvokedWithResult ?? onPopInvoked ?? canPop) == null) || onWillPop == null, 'onWillPop is deprecated; use canPop and/or onPopInvokedWithResult.');
|
||||
|
||||
/// Returns the [FormState] of the closest [Form] widget which encloses the
|
||||
/// given context, or null if none is found.
|
||||
@@ -144,7 +150,7 @@ class Form extends StatefulWidget {
|
||||
/// * [WillPopScope], another widget that provides a way to intercept the
|
||||
/// back button.
|
||||
@Deprecated(
|
||||
'Use canPop and/or onPopInvoked instead. '
|
||||
'Use canPop and/or onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.12.0-1.0.pre.',
|
||||
)
|
||||
final WillPopCallback? onWillPop;
|
||||
@@ -160,12 +166,19 @@ class Form extends StatefulWidget {
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [onPopInvoked], which also comes from [PopScope] and is often used in
|
||||
/// * [onPopInvokedWithResult], which also comes from [PopScope] and is often used in
|
||||
/// conjunction with this parameter.
|
||||
/// * [PopScope.canPop], which is what [Form] delegates to internally.
|
||||
final bool? canPop;
|
||||
|
||||
/// {@macro flutter.widgets.navigator.onPopInvoked}
|
||||
/// {@macro flutter.widgets.navigator.onPopInvokedWithResult}
|
||||
@Deprecated(
|
||||
'Use onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
final PopInvokedCallback? onPopInvoked;
|
||||
|
||||
/// {@macro flutter.widgets.navigator.onPopInvokedWithResult}
|
||||
///
|
||||
/// {@tool dartpad}
|
||||
/// This sample demonstrates how to use this parameter to show a confirmation
|
||||
@@ -178,8 +191,8 @@ class Form extends StatefulWidget {
|
||||
///
|
||||
/// * [canPop], which also comes from [PopScope] and is often used in
|
||||
/// conjunction with this parameter.
|
||||
/// * [PopScope.onPopInvoked], which is what [Form] delegates to internally.
|
||||
final PopInvokedCallback? onPopInvoked;
|
||||
/// * [PopScope.onPopInvokedWithResult], which is what [Form] delegates to internally.
|
||||
final PopInvokedWithResultCallback<Object?>? onPopInvokedWithResult;
|
||||
|
||||
/// Called when one of the form fields changes.
|
||||
///
|
||||
@@ -193,6 +206,14 @@ class Form extends StatefulWidget {
|
||||
/// {@macro flutter.widgets.FormField.autovalidateMode}
|
||||
final AutovalidateMode autovalidateMode;
|
||||
|
||||
void _callPopInvoked(bool didPop, Object? result) {
|
||||
if (onPopInvokedWithResult != null) {
|
||||
onPopInvokedWithResult!(didPop, result);
|
||||
return;
|
||||
}
|
||||
onPopInvoked?.call(didPop);
|
||||
}
|
||||
|
||||
@override
|
||||
FormState createState() => FormState();
|
||||
}
|
||||
@@ -258,10 +279,10 @@ class FormState extends State<Form> {
|
||||
break;
|
||||
}
|
||||
|
||||
if (widget.canPop != null || widget.onPopInvoked != null) {
|
||||
return PopScope(
|
||||
if (widget.canPop != null || (widget.onPopInvokedWithResult ?? widget.onPopInvoked) != null) {
|
||||
return PopScope<Object?>(
|
||||
canPop: widget.canPop ?? true,
|
||||
onPopInvoked: widget.onPopInvoked,
|
||||
onPopInvokedWithResult: widget._callPopInvoked,
|
||||
child: _FormScope(
|
||||
formState: this,
|
||||
generation: _generation,
|
||||
|
||||
@@ -348,14 +348,25 @@ abstract class Route<T> extends _RoutePlaceholder {
|
||||
return isFirst ? RoutePopDisposition.bubble : RoutePopDisposition.pop;
|
||||
}
|
||||
|
||||
/// {@template flutter.widgets.navigator.onPopInvoked}
|
||||
/// Called after a route pop was handled.
|
||||
///
|
||||
/// Even when the pop is canceled, for example by a [PopScope] widget, this
|
||||
/// will still be called. The `didPop` parameter indicates whether or not the
|
||||
/// back navigation actually happened successfully.
|
||||
@Deprecated(
|
||||
'Override onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
void onPopInvoked(bool didPop) {}
|
||||
|
||||
/// {@template flutter.widgets.navigator.onPopInvokedWithResult}
|
||||
/// Called after a route pop was handled.
|
||||
///
|
||||
/// Even when the pop is canceled, for example by a [PopScope] widget, this
|
||||
/// will still be called. The `didPop` parameter indicates whether or not the
|
||||
/// back navigation actually happened successfully.
|
||||
/// {@endtemplate}
|
||||
void onPopInvoked(bool didPop) {}
|
||||
void onPopInvokedWithResult(bool didPop, T? result) => onPopInvoked(didPop);
|
||||
|
||||
/// Whether calling [didPop] would return false.
|
||||
bool get willHandlePopInternally => false;
|
||||
@@ -3109,7 +3120,7 @@ class _RouteEntry extends RouteTransitionRecord {
|
||||
assert(isPresent);
|
||||
pendingResult = result;
|
||||
currentState = _RouteLifecycle.pop;
|
||||
route.onPopInvoked(true);
|
||||
route.onPopInvokedWithResult(true, result);
|
||||
}
|
||||
|
||||
bool _reportRemovalToObserver = true;
|
||||
@@ -5239,7 +5250,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
pop(result);
|
||||
return true;
|
||||
case RoutePopDisposition.doNotPop:
|
||||
lastEntry.route.onPopInvoked(false);
|
||||
lastEntry.route.onPopInvokedWithResult(false, result);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -5282,7 +5293,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
assert(entry.route._popCompleter.isCompleted);
|
||||
entry.currentState = _RouteLifecycle.pop;
|
||||
}
|
||||
entry.route.onPopInvoked(true);
|
||||
entry.route.onPopInvokedWithResult(true, result);
|
||||
} else {
|
||||
entry.pop<T>(result);
|
||||
assert (entry.currentState == _RouteLifecycle.pop);
|
||||
|
||||
@@ -81,9 +81,9 @@ class _NavigatorPopHandlerState extends State<NavigatorPopHandler> {
|
||||
Widget build(BuildContext context) {
|
||||
// When the widget subtree indicates it can handle a pop, disable popping
|
||||
// here, so that it can be manually handled in canPop.
|
||||
return PopScope(
|
||||
return PopScope<Object?>(
|
||||
canPop: !widget.enabled || _canPop,
|
||||
onPopInvoked: (bool didPop) {
|
||||
onPopInvokedWithResult: (bool didPop, Object? result) {
|
||||
if (didPop) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -8,12 +8,29 @@ import 'framework.dart';
|
||||
import 'navigator.dart';
|
||||
import 'routes.dart';
|
||||
|
||||
/// A callback type for informing that a navigation pop has been invoked,
|
||||
/// whether or not it was handled successfully.
|
||||
///
|
||||
/// Accepts a didPop boolean indicating whether or not back navigation
|
||||
/// succeeded.
|
||||
@Deprecated(
|
||||
'Use PopWithResultInvokedCallback instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
typedef PopInvokedCallback = void Function(bool didPop);
|
||||
|
||||
/// Manages back navigation gestures.
|
||||
///
|
||||
/// The generic type should match or be a supertype of the generic type of the
|
||||
/// enclosing [Route]. For example, if the enclosing Route is a
|
||||
/// `MaterialPageRoute<int>`, you can define [PopScope] with `int` or any
|
||||
/// supertype of `int`.
|
||||
///
|
||||
/// The [canPop] parameter disables back gestures when set to `false`.
|
||||
///
|
||||
/// The [onPopInvoked] parameter reports when pop navigation was attempted, and
|
||||
/// `didPop` indicates whether or not the navigation was successful.
|
||||
/// The [onPopInvokedWithResult] parameter reports when pop navigation was attempted, and
|
||||
/// `didPop` indicates whether or not the navigation was successful. The
|
||||
/// `result` contains the pop result.
|
||||
///
|
||||
/// Android has a system back gesture that is a swipe inward from near the edge
|
||||
/// of the screen. It is recognized by Android before being passed to Flutter.
|
||||
@@ -22,15 +39,15 @@ import 'routes.dart';
|
||||
/// back gesture.
|
||||
///
|
||||
/// If [canPop] is false, then a system back gesture will not pop the route off
|
||||
/// of the enclosing [Navigator]. [onPopInvoked] will still be called, and
|
||||
/// of the enclosing [Navigator]. [onPopInvokedWithResult] will still be called, and
|
||||
/// `didPop` will be `false`. On iOS when using [CupertinoRouteTransitionMixin]
|
||||
/// with [canPop] set to false, no gesture will be detected at all, so
|
||||
/// [onPopInvoked] will not be called. Programmatically attempting pop
|
||||
/// navigation will also result in a call to [onPopInvoked], with `didPop`
|
||||
/// [onPopInvokedWithResult] will not be called. Programmatically attempting pop
|
||||
/// navigation will also result in a call to [onPopInvokedWithResult], with `didPop`
|
||||
/// indicating success or failure.
|
||||
///
|
||||
/// If [canPop] is true, then a system back gesture will cause the enclosing
|
||||
/// [Navigator] to receive a pop as usual. [onPopInvoked] will be called with
|
||||
/// [Navigator] to receive a pop as usual. [onPopInvokedWithResult] will be called with
|
||||
/// `didPop` as true, unless the pop failed for reasons unrelated to
|
||||
/// [PopScope], in which case it will be false.
|
||||
///
|
||||
@@ -41,30 +58,43 @@ import 'routes.dart';
|
||||
/// ** See code in examples/api/lib/widgets/pop_scope/pop_scope.0.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// {@tool dartpad}
|
||||
/// This sample demonstrates showing how to use PopScope to wrap widget that
|
||||
/// may pop the page with a result.
|
||||
///
|
||||
/// ** See code in examples/api/lib/widgets/pop_scope/pop_scope.1.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [NavigatorPopHandler], which is a less verbose way to handle system back
|
||||
/// gestures in simple cases of nested [Navigator]s.
|
||||
/// * [Form.canPop] and [Form.onPopInvoked], which can be used to handle system
|
||||
/// * [Form.canPop] and [Form.onPopInvokedWithResult], which can be used to handle system
|
||||
/// back gestures in the case of a form with unsaved data.
|
||||
/// * [ModalRoute.registerPopEntry] and [ModalRoute.unregisterPopEntry],
|
||||
/// which this widget uses to integrate with Flutter's navigation system.
|
||||
class PopScope extends StatefulWidget {
|
||||
@optionalTypeArgs
|
||||
class PopScope<T> extends StatefulWidget {
|
||||
/// Creates a widget that registers a callback to veto attempts by the user to
|
||||
/// dismiss the enclosing [ModalRoute].
|
||||
const PopScope({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.canPop = true,
|
||||
this.onPopInvokedWithResult,
|
||||
@Deprecated(
|
||||
'Use onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
this.onPopInvoked,
|
||||
});
|
||||
}) : assert(onPopInvokedWithResult == null || onPopInvoked == null, 'onPopInvoked is deprecated, use onPopInvokedWithResult');
|
||||
|
||||
/// The widget below this widget in the tree.
|
||||
///
|
||||
/// {@macro flutter.widgets.ProxyWidget.child}
|
||||
final Widget child;
|
||||
|
||||
/// {@template flutter.widgets.PopScope.onPopInvoked}
|
||||
/// {@template flutter.widgets.PopScope.onPopInvokedWithResult}
|
||||
/// Called after a route pop was handled.
|
||||
/// {@endtemplate}
|
||||
///
|
||||
@@ -78,11 +108,38 @@ class PopScope extends StatefulWidget {
|
||||
/// indicates whether or not the back navigation actually happened
|
||||
/// successfully.
|
||||
///
|
||||
/// The `result` contains the pop result.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [Route.onPopInvoked], which is similar.
|
||||
/// * [Route.onPopInvokedWithResult], which is similar.
|
||||
final PopInvokedWithResultCallback<T>? onPopInvokedWithResult;
|
||||
|
||||
/// Called after a route pop was handled.
|
||||
///
|
||||
/// It's not possible to prevent the pop from happening at the time that this
|
||||
/// method is called; the pop has already happened. Use [canPop] to
|
||||
/// disable pops in advance.
|
||||
///
|
||||
/// This will still be called even when the pop is canceled. A pop is canceled
|
||||
/// when the relevant [Route.popDisposition] returns false, such as when
|
||||
/// [canPop] is set to false on a [PopScope]. The `didPop` parameter
|
||||
/// indicates whether or not the back navigation actually happened
|
||||
/// successfully.
|
||||
@Deprecated(
|
||||
'Use onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
final PopInvokedCallback? onPopInvoked;
|
||||
|
||||
void _callPopInvoked(bool didPop, T? result) {
|
||||
if (onPopInvokedWithResult != null) {
|
||||
onPopInvokedWithResult!(didPop, result);
|
||||
return;
|
||||
}
|
||||
onPopInvoked?.call(didPop);
|
||||
}
|
||||
|
||||
/// {@template flutter.widgets.PopScope.canPop}
|
||||
/// When false, blocks the current route from being popped.
|
||||
///
|
||||
@@ -99,14 +156,21 @@ class PopScope extends StatefulWidget {
|
||||
final bool canPop;
|
||||
|
||||
@override
|
||||
State<PopScope> createState() => _PopScopeState();
|
||||
State<PopScope<T>> createState() => _PopScopeState<T>();
|
||||
}
|
||||
|
||||
class _PopScopeState extends State<PopScope> implements PopEntry {
|
||||
class _PopScopeState<T> extends State<PopScope<T>> implements PopEntry<T> {
|
||||
ModalRoute<dynamic>? _route;
|
||||
|
||||
@override
|
||||
PopInvokedCallback? get onPopInvoked => widget.onPopInvoked;
|
||||
void onPopInvoked(bool didPop) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
void onPopInvokedWithResult(bool didPop, T? result) {
|
||||
widget._callPopInvoked(didPop, result);
|
||||
}
|
||||
|
||||
@override
|
||||
late final ValueNotifier<bool> canPopNotifier;
|
||||
@@ -129,7 +193,7 @@ class _PopScopeState extends State<PopScope> implements PopEntry {
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(PopScope oldWidget) {
|
||||
void didUpdateWidget(PopScope<T> oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
canPopNotifier.value = widget.canPop;
|
||||
}
|
||||
|
||||
@@ -1669,7 +1669,9 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
|
||||
final List<WillPopCallback> _willPopCallbacks = <WillPopCallback>[];
|
||||
|
||||
final Set<PopEntry> _popEntries = <PopEntry>{};
|
||||
// Holding as Object? instead of T so that PopScope in this route can be
|
||||
// declared with any supertype of T.
|
||||
final Set<PopEntry<Object?>> _popEntries = <PopEntry<Object?>>{};
|
||||
|
||||
/// Returns [RoutePopDisposition.doNotPop] if any of callbacks added with
|
||||
/// [addScopedWillPopCallback] returns either false or null. If they all
|
||||
@@ -1717,14 +1719,14 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [Form], which provides an `onPopInvoked` callback that is similar.
|
||||
/// * [Form], which provides an `onPopInvokedWithResult` callback that is similar.
|
||||
/// * [registerPopEntry], which adds a [PopEntry] to the list this method
|
||||
/// checks.
|
||||
/// * [unregisterPopEntry], which removes a [PopEntry] from the list this
|
||||
/// method checks.
|
||||
@override
|
||||
RoutePopDisposition get popDisposition {
|
||||
for (final PopEntry popEntry in _popEntries) {
|
||||
for (final PopEntry<Object?> popEntry in _popEntries) {
|
||||
if (!popEntry.canPopNotifier.value) {
|
||||
return RoutePopDisposition.doNotPop;
|
||||
}
|
||||
@@ -1734,9 +1736,9 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
}
|
||||
|
||||
@override
|
||||
void onPopInvoked(bool didPop) {
|
||||
for (final PopEntry popEntry in _popEntries) {
|
||||
popEntry.onPopInvoked?.call(didPop);
|
||||
void onPopInvokedWithResult(bool didPop, T? result) {
|
||||
for (final PopEntry<Object?> popEntry in _popEntries) {
|
||||
popEntry.onPopInvokedWithResult(didPop, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1786,14 +1788,14 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
/// Registers the existence of a [PopEntry] in the route.
|
||||
///
|
||||
/// [PopEntry] instances registered in this way will have their
|
||||
/// [PopEntry.onPopInvoked] callbacks called when a route is popped or a pop
|
||||
/// [PopEntry.onPopInvokedWithResult] callbacks called when a route is popped or a pop
|
||||
/// is attempted. They will also be able to block pop operations with
|
||||
/// [PopEntry.canPopNotifier] through this route's [popDisposition] method.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [unregisterPopEntry], which performs the opposite operation.
|
||||
void registerPopEntry(PopEntry popEntry) {
|
||||
void registerPopEntry(PopEntry<Object?> popEntry) {
|
||||
_popEntries.add(popEntry);
|
||||
popEntry.canPopNotifier.addListener(_handlePopEntryChange);
|
||||
_handlePopEntryChange();
|
||||
@@ -1804,7 +1806,7 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
/// See also:
|
||||
///
|
||||
/// * [registerPopEntry], which performs the opposite operation.
|
||||
void unregisterPopEntry(PopEntry popEntry) {
|
||||
void unregisterPopEntry(PopEntry<Object?> popEntry) {
|
||||
_popEntries.remove(popEntry);
|
||||
popEntry.canPopNotifier.removeListener(_handlePopEntryChange);
|
||||
_handlePopEntryChange();
|
||||
@@ -2413,11 +2415,13 @@ typedef RouteTransitionsBuilder = Widget Function(BuildContext context, Animatio
|
||||
///
|
||||
/// Accepts a didPop boolean indicating whether or not back navigation
|
||||
/// succeeded.
|
||||
typedef PopInvokedCallback = void Function(bool didPop);
|
||||
///
|
||||
/// The `result` contains the pop result.
|
||||
typedef PopInvokedWithResultCallback<T> = void Function(bool didPop, T? result);
|
||||
|
||||
/// Allows listening to and preventing pops.
|
||||
///
|
||||
/// Can be registered in [ModalRoute] to listen to pops with [onPopInvoked] or
|
||||
/// Can be registered in [ModalRoute] to listen to pops with [onPopInvokedWithResult] or
|
||||
/// to enable/disable them with [canPopNotifier].
|
||||
///
|
||||
/// See also:
|
||||
@@ -2425,15 +2429,23 @@ typedef PopInvokedCallback = void Function(bool didPop);
|
||||
/// * [PopScope], which provides similar functionality in a widget.
|
||||
/// * [ModalRoute.registerPopEntry], which unregisters instances of this.
|
||||
/// * [ModalRoute.unregisterPopEntry], which unregisters instances of this.
|
||||
abstract class PopEntry {
|
||||
/// {@macro flutter.widgets.PopScope.onPopInvoked}
|
||||
PopInvokedCallback? get onPopInvoked;
|
||||
abstract class PopEntry<T> {
|
||||
|
||||
/// {@macro flutter.widgets.PopScope.onPopInvokedWithResult}
|
||||
@Deprecated(
|
||||
'Use onPopInvokedWithResult instead. '
|
||||
'This feature was deprecated after v3.22.0-12.0.pre.',
|
||||
)
|
||||
void onPopInvoked(bool didPop) { }
|
||||
|
||||
/// {@macro flutter.widgets.PopScope.onPopInvokedWithResult}
|
||||
void onPopInvokedWithResult(bool didPop, T? result) => onPopInvoked(didPop);
|
||||
|
||||
/// {@macro flutter.widgets.PopScope.canPop}
|
||||
ValueListenable<bool> get canPopNotifier;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'PopEntry canPop: ${canPopNotifier.value}, onPopInvoked: $onPopInvoked';
|
||||
return 'PopEntry canPop: ${canPopNotifier.value}, onPopInvoked: $onPopInvokedWithResult';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,7 +305,7 @@ void main() {
|
||||
BottomNavigationBarItem(label: '', icon: Text('2'))
|
||||
],
|
||||
),
|
||||
tabBuilder: (_, int i) => PopScope(
|
||||
tabBuilder: (_, int i) => PopScope<Object?>(
|
||||
canPop: false,
|
||||
child: CupertinoTabView(
|
||||
navigatorKey: key,
|
||||
|
||||
@@ -2989,7 +2989,7 @@ void main() {
|
||||
const List<Page<void>> myPages = <Page<void>>[
|
||||
MaterialPage<void>(child: Text('page1')),
|
||||
MaterialPage<void>(
|
||||
child: PopScope(
|
||||
child: PopScope<void>(
|
||||
canPop: false,
|
||||
child: Text('page2'),
|
||||
),
|
||||
@@ -4908,9 +4908,9 @@ void main() {
|
||||
home: StatefulBuilder(
|
||||
builder: (BuildContext context, StateSetter setState) {
|
||||
builderSetState = setState;
|
||||
return PopScope(
|
||||
return PopScope<Object?>(
|
||||
canPop: canPop(),
|
||||
onPopInvoked: (bool success) {
|
||||
onPopInvokedWithResult: (bool success, Object? result) {
|
||||
if (success || pages.last == _Page.noPop) {
|
||||
return;
|
||||
}
|
||||
@@ -5024,9 +5024,9 @@ void main() {
|
||||
MaterialApp(
|
||||
home: StatefulBuilder(
|
||||
builder: (BuildContext context, StateSetter setState) {
|
||||
return PopScope(
|
||||
return PopScope<Object?>(
|
||||
canPop: canPop(),
|
||||
onPopInvoked: (bool success) {
|
||||
onPopInvokedWithResult: (bool success, Object? result) {
|
||||
if (success || pages.last == _Page.noPop) {
|
||||
return;
|
||||
}
|
||||
@@ -5117,9 +5117,9 @@ void main() {
|
||||
MaterialApp(
|
||||
home: StatefulBuilder(
|
||||
builder: (BuildContext context, StateSetter setState) {
|
||||
return PopScope(
|
||||
return PopScope<Object?>(
|
||||
canPop: canPop(),
|
||||
onPopInvoked: (bool success) {
|
||||
onPopInvokedWithResult: (bool success, Object? result) {
|
||||
if (success || pages.last == _PageWithYesPop.noPop) {
|
||||
return;
|
||||
}
|
||||
@@ -5189,7 +5189,7 @@ void main() {
|
||||
child: _LinksPage(
|
||||
title: 'Can pop page',
|
||||
canPop: true,
|
||||
onPopInvoked: (bool didPop) {
|
||||
onPopInvoked: (bool didPop, void result) {
|
||||
onPopInvokedCallCount += 1;
|
||||
},
|
||||
),
|
||||
@@ -5556,7 +5556,7 @@ class _LinksPage extends StatelessWidget {
|
||||
final bool? canPop;
|
||||
final VoidCallback? onBack;
|
||||
final String title;
|
||||
final PopInvokedCallback? onPopInvoked;
|
||||
final PopInvokedWithResultCallback<Object?>? onPopInvoked;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -5575,9 +5575,9 @@ class _LinksPage extends StatelessWidget {
|
||||
child: const Text('Go back'),
|
||||
),
|
||||
if (canPop != null)
|
||||
PopScope(
|
||||
PopScope<void>(
|
||||
canPop: canPop!,
|
||||
onPopInvoked: onPopInvoked,
|
||||
onPopInvokedWithResult: onPopInvoked,
|
||||
child: const SizedBox.shrink(),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -47,7 +47,7 @@ void main() {
|
||||
builder: (BuildContext buildContext, StateSetter stateSetter) {
|
||||
context = buildContext;
|
||||
setState = stateSetter;
|
||||
return PopScope(
|
||||
return PopScope<Object?>(
|
||||
canPop: canPop,
|
||||
child: const Center(
|
||||
child: Column(
|
||||
@@ -79,6 +79,94 @@ void main() {
|
||||
variant: TargetPlatformVariant.all(),
|
||||
);
|
||||
|
||||
testWidgets('pop scope can receive result', (WidgetTester tester) async {
|
||||
Object? receivedResult;
|
||||
final Object poppedResult = Object();
|
||||
final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
initialRoute: '/',
|
||||
navigatorKey: nav,
|
||||
home: Scaffold(
|
||||
body: PopScope<Object?>(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (bool didPop, Object? result) {
|
||||
receivedResult = result;
|
||||
},
|
||||
child: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text('Home/PopScope Page'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
nav.currentState!.maybePop(poppedResult);
|
||||
await tester.pumpAndSettle();
|
||||
expect(receivedResult, poppedResult);
|
||||
},
|
||||
variant: TargetPlatformVariant.all(),
|
||||
);
|
||||
|
||||
testWidgets('pop scope can have Object? generic type while route has stricter generic type', (WidgetTester tester) async {
|
||||
Object? receivedResult;
|
||||
const int poppedResult = 13;
|
||||
final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
initialRoute: '/',
|
||||
navigatorKey: nav,
|
||||
home: Scaffold(
|
||||
body: PopScope<Object?>(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (bool didPop, Object? result) {
|
||||
receivedResult = result;
|
||||
},
|
||||
child: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text('Home/PopScope Page'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
nav.currentState!.push(
|
||||
MaterialPageRoute<int>(
|
||||
builder: (BuildContext context) {
|
||||
return Scaffold(
|
||||
body: PopScope<Object?>(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (bool didPop, Object? result) {
|
||||
receivedResult = result;
|
||||
},
|
||||
child: const Center(
|
||||
child: Text('new page'),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('new page'), findsOneWidget);
|
||||
|
||||
nav.currentState!.maybePop(poppedResult);
|
||||
await tester.pumpAndSettle();
|
||||
expect(receivedResult, poppedResult);
|
||||
},
|
||||
variant: TargetPlatformVariant.all(),
|
||||
);
|
||||
|
||||
testWidgets('toggling canPop on secondary route allows/prevents backs', (WidgetTester tester) async {
|
||||
final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
|
||||
bool canPop = true;
|
||||
@@ -115,9 +203,9 @@ void main() {
|
||||
builder: (BuildContext context, StateSetter stateSetter) {
|
||||
oneContext = context;
|
||||
setState = stateSetter;
|
||||
return PopScope(
|
||||
return PopScope<Object?>(
|
||||
canPop: canPop,
|
||||
onPopInvoked: (bool didPop) {
|
||||
onPopInvokedWithResult: (bool didPop, Object? result) {
|
||||
lastPopSuccess = didPop;
|
||||
},
|
||||
child: const Center(
|
||||
@@ -271,7 +359,7 @@ void main() {
|
||||
if (!usePopScope) {
|
||||
return child;
|
||||
}
|
||||
return const PopScope(
|
||||
return const PopScope<Object?>(
|
||||
canPop: false,
|
||||
child: child,
|
||||
);
|
||||
@@ -314,12 +402,12 @@ void main() {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
if (usePopScope1)
|
||||
const PopScope(
|
||||
const PopScope<Object?>(
|
||||
canPop: false,
|
||||
child: Text('hello'),
|
||||
),
|
||||
if (usePopScope2)
|
||||
const PopScope(
|
||||
const PopScope<Object?>(
|
||||
canPop: false,
|
||||
child: Text('hello'),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user