Flutter How do I know if the TextField has been (lose focus , focus out) or not?

Viewed 1344

I have a page that contains multiple elements and it has a TextField widget and I want to know if I moved to another element to do something. Is there an event that is triggered when moving from one element to another? I tried onSubmitted but it didn't fulfill my request.

1 Answers

You need to use FocusNode

  1. Declare your FocusNode : final FocusNode focusNode;
  2. Pass it to TextField : TextField( ..., focusNode: focusNode, ... )
  3. Add listener to FocusNode :
focusNode.addListener(() {
  print("${focusNode.hasFocus}");
});
  1. FocusNode has hasFocus , true means TextField has focus
Related