Is there GlobalKey counterpart of HookWidget?

Issue

I want to call the onPopPage in the following code in the parent widget.
In traditional way I may use GlobalKey<SomeState>().currentState.someMethod.

But what’s the desirable way with 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,
    );
  }
}

Solution

I asked to library author and got an answer. The gist is key usage should be avoided.

Answered By – jeiea

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.