有没有办法在Flutter的多个PageRoutes中使用InheritedWidget? [英] Is there a way to use an InheritedWidget in multiple PageRoutes in Flutter?

查看:112
本文介绍了有没有办法在Flutter的多个PageRoutes中使用InheritedWidget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序的根目录中有一个 InheritedWidget ,基本上在我的第一个 Scaffold 。我可以使用 .of(context)函数引用该小部件,该功能对于第一页的所有子小部件都很好。如果我在 Navigator 堆栈中推送另一个页面,则 InheritedWidget.of()调用将失败,因为我认为应该这样。有没有办法共享对此小部件的访问权限?我试图避免不必要地将小部件传递到树上。 ScopedModel具有相同的限制。我当时正在考虑将上下文变量传递到树上,但这听起来不正确,也不能解决问题。

I have an InheritedWidget at the root of my application, basically at the top of the first build function of my first Scaffold. I can reference the widget using the .of(context) function just fine from all sub-widgets of the first page. if I push another page on the Navigator stack, the InheritedWidget.of() call fails, as I assume it should. Is there a way to share access to this widget? I'm trying to avoid passing widgets down the tree unnecessarily. ScopedModel has the same limitation. I was thinking about passing the context variable down the tree, but that doesn't sound right and it doesn't solve the issue either.

推荐答案

您应该用它包装您的根窗口小部件,例如您的 MaterialApp

You should wrap your root widget with it, for example your MaterialApp

MyInheritedWidget(
      child: MaterialApp(
        home: home))

这篇关于有没有办法在Flutter的多个PageRoutes中使用InheritedWidget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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