获取对InheritedWidget上下文的访问权限 [英] Get access to the context of InheritedWidget

查看:95
本文介绍了获取对InheritedWidget上下文的访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行以下设置:

MaterialApp(home:SplashScreen)


SplashScreen(
///do some auth checks then
Navigator.of(context).push( MaterialPageRoute(
                builder: (_) =>  StateInheritedWidget(
                  user: userData,
                  child:  HomePage(),

                )))
)

然后在我的主页中,我有一个FAB可以推动一条新路线。这条新路线无法访问我继承的窗口小部件的上下文。

Then in my HomePage I have a FAB that pushes a new route. This new route has no access to the context of my inherited widget.

是否不可能获取 InheritedWidget 在新路线中,还是有办法解决此问题?

Is it impossible to get the context of an InheritedWidget in new routes, or is there a way to solve this issue ?

更新

这是我的MaterialApp的构建器属性

This is my MaterialApp's builder property

    builder: (context,child){
///Doing some auth stuff here
        return new StateInheritedWidget(
          user: userData,
                      child: userData==null?const SplashScreen(child: const LoginPage(),): 
                      const SplashScreen(child: const HomePage(),),

        );
      },

我的 SplashScreen 生成方法:

return !_load? new Scaffold(
      body: new Center(
        child: new Text('Splash'),
      ),
    ):this.widget.child;

这些是我得到的错误。

第一个似乎与我的主页中的抽屉有关:

The first one seems to be related to the Drawer in my HomePage:

I/flutter (12688): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (12688): The following assertion was thrown building Tooltip("Open navigation menu", vertical offset: 24.0,
I/flutter (12688): position: below, dirty, state: _TooltipState#44ae9(ticker inactive)):
I/flutter (12688): No Overlay widget found.
I/flutter (12688): Tooltip widgets require an Overlay widget ancestor for correct operation.
I/flutter (12688): The most common way to add an Overlay to an application is to include a MaterialApp or Navigator
I/flutter (12688): widget in the runApp() call.
I/flutter (12688): The specific widget that failed to find an overlay was:
I/flutter (12688):   Tooltip("Open navigation menu", vertical offset: 24.0, position: below)
I/flutter (12688):

当我单击FAB时,出现第二个例外( Navigator.push)

The second exception appears when I click on FAB (Navigator.push)

I/flutter (12688): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.


推荐答案

否。这是不可能的(或至少不建议这样做)。

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

问题在于您的InheritedWidget不应位于路线的,而应位于路线的之上 。通常在 MaterialApp

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


但是我希望我的身份验证能够推送登录信息屏幕上,我不能将其放在 MaterialApp

这是为什么我说的通常是。 :D

Which is why I said usually. :D

身份验证是这些例外之一。您可能希望您的身份验证层访问导航器。但仍想与 all 路由共享该身份验证层。

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.

这是 MaterialApp有用的属性 builder

Builder在通过时将用于在路线上方插入任何类型的小部件。但是仍然要在 Navigator 之后,以便您仍可以在需要时推送新路线。

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.

最后, d have

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.

这篇关于获取对InheritedWidget上下文的访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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