带有导航键而不是传递上下文的Flutter showDialog [英] Flutter showDialog with navigator key rather than passing context

查看:93
本文介绍了带有导航键而不是传递上下文的Flutter showDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,它非常忙于显示应用程序中任何代码层的对话框,这仅仅是因为必须在其中传递上下文.因此,我认为要传递navigatorKey.currentContext(导航器键是传递给Material应用程序navigatorKey参数的全局键)以显示对话框.但是我得到了错误

Currently its very hectic to show dialog from any layer of code in app just because one has to pass context in it. Hence i thought to pass navigatorKey.currentContext (Navigator key is a global key passed to Material app navigatorKey parameter) to show dialog. But i got the error

请求的导航器操作的上下文不包含导航器.用于从导航器推送或弹出路由的上下文必须是作为导航器小部件的后代的小部件的上下文."

"Navigator operation requested with a context that does not include a Navigator.The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget."

问题是showDialog在内部调用Navigator.of(context)并查找导航器的祖先,因为导航器本身是根,所以它当然会返回null.因此,它将不会找到导航器作为祖先.

The issue is showDialog calls Navigator.of(context) internally and which looks for the navigator ancestor which ofcourse will return null as the navigator is itself the root. Hence it will not find the navigator as ancestor.

有没有一种方法可以直接将导航器的状态/上下文传递给showDialog函数以显示对话框?或者,如果我们想从团体中显示对话框,是否有一种更简单的方法来显示对话框而不向其传递上下文?

Is there a way we can directly pass the navigator state/context to showDialog function to show the dialog? Or is there a more easy way to show Dialog without passing context to it if we want to show it from bloc?

推荐答案

我找到了一个简单的解决方案:

I found a simple solution:

navigatorKey.currentState.overlay.context

我在保留navigatorKey的redux中间件中使用了此功能,并且希望在我每次调度特定操作时在应用程序中的任何位置全局显示对话框.

I use this in a redux middleware where I keep navigatorKey, and want to show a dialog globally anywhere in the app everytime I dispatch a specific action.

这篇关于带有导航键而不是传递上下文的Flutter showDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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