A.A
A.A

Reputation: 4101

Hide TextField's cursor when unfocused

In flutter, When we edit a TextField and close soft-keyboard by touching outside, the cursor of TextField is showing and blinking

How can I exit editing mode when unfocused? i.e. keyboard is closed

Upvotes: 2

Views: 1123

Answers (1)

Marcin Wróblewski
Marcin Wróblewski

Reputation: 1237

You can listen to keyboard close event via eg. flutter_keyboard_visibility and then call FocusScope.of(context).unfocus():

@override
void initState() {
  super.initState();
  KeyboardVisibility.onChange.listen((bool visible) {
    if (!visible) FocusScope.of(context).unfocus();
  });
}

If you already have your own way to detect that user is done with editing the text, then just call FocusScope.of(context).unfocus(); -- it will disable the cursor and close the keyboard.

Upvotes: 2

Related Questions