我想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,
);
}
}