showdialog中的Navigator.push()无法正常工作

编程入门 行业动态 更新时间:2024-10-13 00:34:31
本文介绍了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()无法正常工作

本文发布于:2023-11-27 19:58:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   showdialog   Navigator   push

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!