showdialog中的Navigator.push()无法正常工作 [英] Navigator.push() inside showdialog not working

查看:64
本文介绍了showdialog中的Navigator.push()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Navigator.push()导航到新屏幕,但不起作用.我创建了一个自定义类以显示AlertDialog,并使用该对象调用该类以显示alertDialog

Trying to navigate to a new screen using Navigator.push(), but it's not working. I have created a custom class to show AlertDialog and call the class with the object to show alertDialog

_customerAlertDialog.showConfirmAlertDialog(
   context: context,
   title: "Login In",
   subTitle: "You need to login to purchase.",
   onTapResponse: (bool val) async {
     if (val) {
       /// close AlertDialog
       Navigator.of(context).pop();
       Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginScreen()));
       print("show the login screen");

     } else {
       //TODO : when user click no.
     }
   });

navigator.pop()正在工作,打印声明有效,但Navigator.push无法正常工作.还尝试了这个:

navigator.pop() is working, print statement is working, but Navigator.push is not working. Also tried this:

Navigator.push(context,MaterialPageRoute(builder: (context) => LoginScreen())); 

推荐答案

您在 Navigator.of(context).pop()中使用的 context 对象是'不会注意到对话框.

The context object that you use in Navigator.of(context).pop() isn't aware of the dialog.

如果您的自定义警报对话框正在调用 showDialog ,请考虑传递由 builder 返回的 BuildContext 对象:

If your custom alert dialog is calling showDialog, consider passing on the BuildContext object that is returned by the builder:

showDialog(
  context: context,
  builder: (BuildContext ctx) { 
    // ctx is a context object that will be aware of the dialog
    // consider passing this along to onTapResponse as an argument
  },
);

然后,您可以使用该 BuildContext 获取将能够关闭对话框的导航器:

Then you can use that BuildContext to get the navigator that will be able to close the dialog:

onTapResponse: (BuildContext ctx, bool val) async {
  if (val) {
    // close AlertDialog
    Navigator.of(ctx).pop();
    Navigator.of(ctx).push(MaterialPageRoute(builder: (context) => LoginScreen()));
    print("show the login screen");
  } else {
    //TODO : when user click no.
  }
}

这篇关于showdialog中的Navigator.push()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆