'Remove text field soft focus WillPopScope
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
Solution 1:[1]
This is what I am using in my project:
onWillPop: () async {
FocusScope.of(context).requestFocus(FocusNode());
return true;
},
Solution 2:[2]
Change your removeFocus() method as the following:
void removeFocus() {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Valentin Vignal |
| Solution 2 | Sowat Kheang |

