访问 InheritedWidget 的上下文
Get access to the context of InheritedWidget
我有以下设置:
MaterialApp(home:SplashScreen)
SplashScreen(
///do some auth checks then
Navigator.of(context).push( MaterialPageRoute(
builder: (_) => StateInheritedWidget(
user: userData,
child: HomePage(),
)))
)
然后在我的 HomePage
中,我有一个推新路线的 FAB。这条新路线无法访问我继承的小部件的上下文。
是否无法在新路由中获取 InheritedWidget
的上下文,或者有没有办法解决这个问题?
更新
这是我的 MaterialApp 的构建器 属性
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;
这些是我得到的错误。
第一个好像和我的HomePage
里面的Drawer
有关:
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)
I/flutter (12688): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
没有。这是不可能的(或至少不推荐)。
问题在于您继承的 Widget 不应该在内部,而是在 路线之上。通常在 MaterialApp
以上
But I want my Authentification to be able to push a login screen, I can't put it above MaterialApp
!
这就是为什么我说 usually
。 :D
身份验证是这些例外之一。您可能希望您的身份验证层访问导航器。但仍想与 all 路由共享该身份验证层。
这是使用 MaterialApp
中有用的 属性 的地方:builder
Builder 在通过后,将用于在您的路线上方插入任何类型的小部件。但仍然在 Navigator
之后,这样您仍然可以在需要时推送新路线。
最后,你会
new MaterialApp(
builder: (context, child) {
return new MyAuthent(child: child);
},
...
// home: SplashSceen ?
);
这很可能需要您进行重构。但这是实现这一目标的正确方法。
我有以下设置:
MaterialApp(home:SplashScreen)
SplashScreen(
///do some auth checks then
Navigator.of(context).push( MaterialPageRoute(
builder: (_) => StateInheritedWidget(
user: userData,
child: HomePage(),
)))
)
然后在我的 HomePage
中,我有一个推新路线的 FAB。这条新路线无法访问我继承的小部件的上下文。
是否无法在新路由中获取 InheritedWidget
的上下文,或者有没有办法解决这个问题?
更新
这是我的 MaterialApp 的构建器 属性
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;
这些是我得到的错误。
第一个好像和我的HomePage
里面的Drawer
有关:
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)
I/flutter (12688): Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
没有。这是不可能的(或至少不推荐)。
问题在于您继承的 Widget 不应该在内部,而是在 路线之上。通常在 MaterialApp
But I want my Authentification to be able to push a login screen, I can't put it above
MaterialApp
!
这就是为什么我说 usually
。 :D
身份验证是这些例外之一。您可能希望您的身份验证层访问导航器。但仍想与 all 路由共享该身份验证层。
这是使用 MaterialApp
中有用的 属性 的地方:builder
Builder 在通过后,将用于在您的路线上方插入任何类型的小部件。但仍然在 Navigator
之后,这样您仍然可以在需要时推送新路线。
最后,你会
new MaterialApp(
builder: (context, child) {
return new MyAuthent(child: child);
},
...
// home: SplashSceen ?
);
这很可能需要您进行重构。但这是实现这一目标的正确方法。