如何覆盖 Flutter 中的后退按钮行为?
How to override the back button behaviour in Flutter?
我有一个提供商,我想在使用 后退按钮转到上一个屏幕时重置它 ,所以我想 手动编辑代码后退按钮的导航器。此代码位于何处?
一种解决方案是在应用栏中添加一个后退按钮,但 Android 后退按钮仍会执行其自己的过程,我会错过必要的代码。
刚找到这个,对我有用:https://flutteragency.com/how-to-execute-when-clicking-back-button-in-flutter/
用 WillPopScope
小部件包裹 Scaffold
小部件,让 Scaffold
成为它的子部件。
将 onWillPop 属性 交给 WillPopScope
并确保你做了两件事:
Navigator.pop(context, false);
return Future.value(false);
return 很重要,您可以在此 return 语句之前编辑或执行任何您需要的其他 pop。
您可以使用 WillPopScope 小部件处理后退按钮导航。
在这个小部件中,您可以在 onWillPop 方法中处理它。
例子
@override
Widget build(BuildContext context) {
return new WillPopScope(
child: new Scaffold(
appBar: new AppBar(
title: new Text('Page 2'),
),
body: new Center(
child: new Text('PAGE 2'),
),
),
onWillPop: () async {
return false;
},
);
}
我有一个提供商,我想在使用 后退按钮转到上一个屏幕时重置它 ,所以我想 手动编辑代码后退按钮的导航器。此代码位于何处?
一种解决方案是在应用栏中添加一个后退按钮,但 Android 后退按钮仍会执行其自己的过程,我会错过必要的代码。
刚找到这个,对我有用:https://flutteragency.com/how-to-execute-when-clicking-back-button-in-flutter/
用
WillPopScope
小部件包裹Scaffold
小部件,让Scaffold
成为它的子部件。将 onWillPop 属性 交给
WillPopScope
并确保你做了两件事:
Navigator.pop(context, false);
return Future.value(false);
return 很重要,您可以在此 return 语句之前编辑或执行任何您需要的其他 pop。
您可以使用 WillPopScope 小部件处理后退按钮导航。
在这个小部件中,您可以在 onWillPop 方法中处理它。
例子
@override
Widget build(BuildContext context) {
return new WillPopScope(
child: new Scaffold(
appBar: new AppBar(
title: new Text('Page 2'),
),
body: new Center(
child: new Text('PAGE 2'),
),
),
onWillPop: () async {
return false;
},
);
}