Get access to the context of InheritedWidget

五迷三道 提交于 2019-12-01 01:09:36

No. That's not possible (or at least not recommended).

The problem lies in the fact that your inheritedWidget shouldn't be inside the route but instead above the route. Usually above MaterialApp

But I want my Authentification to be able to push a login screen, I can't put it above MaterialApp !

Which is why I said usually. :D

Authentification is one of these exceptions. You may want your authent layer to access the Navigator. But still want to share that authent layer with all routes.

This is where an useful property of MaterialApp is used : builder

Builder, when passed, will be used to insert any kind of widget right above your routes. But still be after Navigator so that you can still push new routes when needed.

In the end, you'd have

new MaterialApp(
  builder: (context, child) {
    return new MyAuthent(child: child);
  },
  ...
  // home: SplashSceen ?
);

This will most likely requires a refactor on your end. But that's the proper way to achieve this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!