Is there a way to use an InheritedWidget in multip

2019-07-13 10:29发布

问题:

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.

回答1:

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

MyInheritedWidget(
      child: MaterialApp(
        home: home))


标签: dart flutter