Home > OS >  Remove text field soft focus WillPopScope
Remove text field soft focus WillPopScope

Time:04-08

everything is fine to remove the focus from text field except for WillPopScope function, after i press back button the keyboard dismiss but the focus line still in the textfield.

let me know if you need more information about the code

removeFocus Code

void removeFocus() {
    FocusManager.instance.primaryFocus?.unfocus();
  }

WillPopScope Code

onWillPop: () async {
        removeFocus();
        return true;
      },

i mean this blue line won't dismiss

enter image description here

CodePudding user response:

This is what I am using in my project:

onWillPop: () async {
  FocusScope.of(context).requestFocus(FocusNode());
  return true;
},

CodePudding user response:

Change your removeFocus() method as the following:

void removeFocus() {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus) {
       currentFocus.unfocus();
    }
}

  • Related