是否有 HookWidget 的 GlobalKey 副本?
Is there GlobalKey counterpart of HookWidget?
我想在父窗口小部件的以下代码中调用 onPopPage
。
以传统方式,我可能会使用 GlobalKey<SomeState>().currentState.someMethod
.
但是 HookWidget 的理想方式是什么?
class SomeWidget extends HookWidget {
final innerNavigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
final isPasswordReset = useState(false);
final onPopPage = useMemoized(() => (_, __) {
if (isInInnerRoute.value &&
innerNavigatorKey.currentState?.onWillPop() == true) {
return true;
}
return false;
});
return Navigator(
pages: [
NoTransitionPage(child: _buildLogin(() {
isInInnerRoute.value = true;
})),
if (isInInnerRoute.value)
NoTransitionPage(
child: SomePage(navigatorKey: innerNavigatorKey)),
],
onPopPage: onPopPage,
);
}
}
class NoTransitionPage extends Page {
final Widget child;
NoTransitionPage({Key key, this.child}) : super(key: key);
Route createRoute(BuildContext context) {
return PageRouteBuilder(
maintainState: false,
settings: this,
pageBuilder: (_, __, ___) => child,
);
}
}
我asked给图书馆作者并得到了答复。要点是应避免使用密钥。
我想在父窗口小部件的以下代码中调用 onPopPage
。
以传统方式,我可能会使用 GlobalKey<SomeState>().currentState.someMethod
.
但是 HookWidget 的理想方式是什么?
class SomeWidget extends HookWidget {
final innerNavigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
final isPasswordReset = useState(false);
final onPopPage = useMemoized(() => (_, __) {
if (isInInnerRoute.value &&
innerNavigatorKey.currentState?.onWillPop() == true) {
return true;
}
return false;
});
return Navigator(
pages: [
NoTransitionPage(child: _buildLogin(() {
isInInnerRoute.value = true;
})),
if (isInInnerRoute.value)
NoTransitionPage(
child: SomePage(navigatorKey: innerNavigatorKey)),
],
onPopPage: onPopPage,
);
}
}
class NoTransitionPage extends Page {
final Widget child;
NoTransitionPage({Key key, this.child}) : super(key: key);
Route createRoute(BuildContext context) {
return PageRouteBuilder(
maintainState: false,
settings: this,
pageBuilder: (_, __, ___) => child,
);
}
}
我asked给图书馆作者并得到了答复。要点是应避免使用密钥。