我想将用户导航到主屏幕,并在按下另一个屏幕上的某个按钮后删除以前的导航堆栈。目前我使用 Navigator.pushNamedAndRemoveUntil 方法。但情况是,当我按下主屏幕上的设备后退按钮时,我的应用程序崩溃了,所以我想处理或覆盖设备后退按钮操作。
像这样使用
WillPopScope
小部件
return new WillPopScope(
onWillPop: () async {
// your logic here
//
return Future.value(false); // true if you want the back button to take effect
},
child: new Scaffold(
//then the rest of your code...
这个答案已经过时了。您可以使用 popscope() 或覆盖 dispose()。