PopScope class - widgets library

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 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. iOS has a similar gesture that is recognized in Flutter by CupertinoRouteTransitionMixin, not by iOS, and is therefore not a system back gesture.

If canPop is false, then a system back gesture will not pop the route off 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 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. onPopInvokedWithResult will be called with didPop as true, unless the pop failed for reasons unrelated to PopScope, in which case it will be false.

This sample demonstrates showing a confirmation dialog before navigating away from a page.

link

To create a local project with this code sample, run:
flutter create --sample=widgets.PopScope.1 mysample

This sample demonstrates showing how to use PopScope to wrap widget that may pop the page with a result.

link

To create a local project with this code sample, run:
flutter create --sample=widgets.PopScope.2 mysample

See also:

Inheritance
Annotations

Constructors

PopScope({Key? key, required Widget child, bool canPop = true, PopInvokedWithResultCallback<T>? onPopInvokedWithResult, @Deprecated('Use onPopInvokedWithResult instead. ' 'This feature was deprecated after v3.22.0-12.0.pre.') PopInvokedCallback? onPopInvoked})
Creates a widget that registers a callback to veto attempts by the user to dismiss the enclosing ModalRoute.

const

Properties

canPop bool
When false, blocks the current route from being popped.

final

child Widget
The widget below this widget in the tree.

final

hashCode int
The hash code for this object.

no setterinherited

key Key?
Controls how one widget replaces another widget in the tree.

finalinherited

onPopInvoked PopInvokedCallback?
Called after a route pop was handled.

final

onPopInvokedWithResult PopInvokedWithResultCallback<T>?
Called after a route pop was handled.

final

runtimeType Type
A representation of the runtime type of the object.

no setterinherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.

inherited

createState() State<PopScope<T>>
Creates the mutable state for this widget at a given location in the tree.

override

debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.

inherited

debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.

inherited

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.

inherited

toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.

inherited

toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.

inherited

toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.

inherited

toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.

inherited

toStringShort() String
A short, textual description of this widget.

inherited

Operators

operator ==(Object other) bool
The equality operator.

inherited