Flutter - Identifying when the user touches outside of the Textformfield after finishing typing

Viewed 187

When the user typing a text in Textformfield, onChanged callback is triggered per character and onFieldSubmitted callback is triggered if the user presses enter button after finish the typing. But, I want different behavior than onChanged and onFieldSubmitted.

(What I mean by outside in the following is background or any other UI element)

Does anyone know a way to identify when the user touches outside of the Textformfield after finish typing? What I am asking is very similar to the behavior of the onFieldSubmitted. But without pressing the enter button.

Thank you!

2 Answers

use GestureDetector to detect taps on the screen:

body: GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => _onFieldSubmitted(), // call to the function
      child: ...  //  your Widgets is here
),

use gesture detector to detect tap outside any widget

and if I'm correct you need to drop the keyboard when the user press outside the widger use this code to do that

 GestureDetector(
  onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
  child: Scaffold...
Related