0

I started adopting Flutter Hooks recently and REALLY loving it. I was able to convert a few of my StatefulWidgets to HookWidgets, and it's pretty cool. The one thing I'm struggling to figure out is how to properly remove listeners from from a FocusNode created by useFocusNode.

If I do this the StatefulWidget way it seems to be working well (basically following this example from the Flutter docs):

class ItemWidget extends StatefulWidget {
...
}

class _ItemWidgetState extends State<ItemWidget> {
...

late FocusNode _focusNode;

@override
void initState() {
    super.initState();
    _focusNode = FocusNode(debugLabel: widget.key.toString());
    _focusNode.addListener(_handleFocusChange);
}

void _handleFocusChange() {
    debugPrint('${_focusNode.debugLabel} has ${_focusNode.hasFocus ? '' : 'lost'} focus');
}

@override
Widget build(BuildContext context) {
...
}

@override
void dispose() {
    _focusNode.removeListener(_handleFocusChange);
    super.dispose();
}

But then how would I do it using WidgetHook and useFocusNode? I know how to create the FocusNode and add the listener, but then where do I remove it? Should I be using useEffect for that? Unsure how to do it.

Thanks!

Mr. Oak
  • 179
  • 9
  • Well - I found an answer here: https://stackoverflow.com/a/54907267/5225270 Sorry about the spam. – Mr. Oak Dec 15 '21 at 03:58

0 Answers0