添加预测性返回手势
了解如何在 Flutter 中启用和处理 Android 的预测性返回手势。
The Android predictive back gesture lets users preview where a back gesture will navigate to, whether that's the previous screen or the home screen, before they commit to or cancel it.
Overview
#Starting with Android 14 (API level 34), predictive back animations are enabled by default for system gestures when supported by the application. Flutter provides built-in support for predictive back animations across default page route transitions and custom pop handling.
Enable predictive back in Android
#To support predictive back gestures in your Flutter app on Android 13 or later:
- Open
android/app/src/main/AndroidManifest.xml. -
Add
android:enableOnBackInvokedCallback="true"to the<application>tag:
Handle back gestures with PopScope
#
To customize back navigation or prevent users from accidentally leaving a
screen, use the PopScope
widget. PopScope replaces the deprecated WillPopScope widget and
supports predictive back gestures.
Callback parameters
#PopScope uses the onPopInvokedWithResult callback:
-
didPop: A boolean indicating whether the pop operation succeeded. IfcanPopisfalse,didPopisfalse. -
result: An optional return payload passed when popping the route (for example, withNavigator.pop(context, result)).
Example: Intercepting back navigation
#PopScope(
canPop: false,
onPopInvokedWithResult: (bool didPop, Object? result) async {
if (didPop) {
return;
}
final shouldLeave = await _showExitConfirmationDialog(context);
if (shouldLeave && context.mounted) {
Navigator.of(context).pop(result);
}
},
child: Scaffold(
appBar: AppBar(title: const Text('Form Screen')),
body: const Center(child: Text('Complete the form before leaving.')),
),
)
More information
#For more details on API migrations, check out the Android predictive back migration guide.
除非另有说明,本文档之所提及适用于 Flutter 3.44.7 版本。本页面最后更新时间:2026-08-01。查看文档源码 或者 为本页面内容提出建议。