1

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

1 回答 1

0

图书馆作者并得到了答案。要点是应避免使用密钥。

于 2021-02-01T07:11:36.163 回答