简体   繁体   English

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

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

I want to close page and back to main page in the stack after clicking alert dialog "yes" button.单击警报对话框“是”按钮后,我想关闭页面并返回堆栈中的主页。 My page contains textformfields and when i click icon of textformfield search page is opened.我的页面包含 textformfields,当我单击 textformfield 搜索页面的图标时会打开。 After i click any item of search list i back to form page.单击搜索列表中的任何项目后,我将返回表单页面。 And I click back button for alert dialog is shown.我单击后退按钮显示警报对话框。 And click yes button, the bug occures.然后单击是按钮,错误发生。 And if i don't use alert dialog, there is no bug.如果我不使用警报对话框,就没有错误。 Alert navigates me to main page but search list page is opened again.警报将我导航到主页,但再次打开搜索列表页面。 How can i solve this bug?我该如何解决这个错误?

My alert dialog:我的警报对话框:

 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;
  }

I use code above to navigate to main page.我使用上面的代码导航到主页。 Is there any alternative?有什么替代方法吗?

I think youre asking about how to remove/clear previous routes.我想您是在询问如何删除/清除以前的路线。 There are various ways you can do that.有多种方法可以做到这一点。 1 Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => MainPage()), (Route<dynamic> route) => false, ); 1 Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => MainPage()), (Route<dynamic> route) => false, );

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM