Flutter-Alternative of Navigator.of(context).pop(true);



我想在单击警报对话框"是"按钮后关闭页面并返回堆栈中的主页。我的页面包含文本表单字段,当我单击文本表单字段搜索页面的图标时,将打开。单击搜索列表的任何项目后,我返回到表单页面。我单击返回按钮以显示警报对话框。然后单击"是"按钮,就会出现错误。 如果我不使用警报对话框,则没有错误。 警报将我导航到主页,但搜索列表页面再次打开。如何解决此错误?

我的警报对话框:

Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0))),
title: new Text(ml(context, LC.are_you_sure)),
content: new Text(ml(context, LC.are_you_sure_quit_query)),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child: roundedButton(ml(context, LC.no),
Theme.of(context).primaryColor, Colors.white),
),
new GestureDetector(
onTap: () {
Navigator.of(context).pop(true);
},
child: roundedButton(ml(context, LC.yes),
Theme.of(context).primaryColor, Colors.white),
),
],
),
) ??
false;
}

我使用上面的代码导航到主页。还有其他选择吗?

我想你在问如何删除/清除以前的路线。 有多种方法可以做到这一点。1Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => MainPage()), (Route<dynamic> route) => false, );

2Navigator.pushReplacementNamed(context, '/route') for named router

相关内容

最新更新