When the keyboard is open I am clicking on the device back button keyboard is not hiding it is navigating to the previous page.
Asked
Active
Viewed 2,581 times
3 Answers
2
FocusScope.of(context).unfocus()
will close the keyboard, device back button is controlled by WillPopScope, you can do something like this
WillPopScope(
onWillPop: () async{
FocusScope.of(context).unfocus();
return false;
},
child: Scaffold(.....)
);

M.M.Hasibuzzaman
- 1,015
- 5
- 10
-
-
Just add a ```Navigator.pop(context)``` You can use ```MediaQuery.of(context).viewInsets.bottom``` to check if keyboard is currently open or closed. – M.M.Hasibuzzaman Dec 12 '20 at 11:02
2
FocusScope.of(context).isFirstFocus
it returns true when keyboard has focus, This only works if your keyboard open.
if (FocusScope.of(context).isFirstFocus) {
FocusScope.of(context).unfocus();
}

Jitesh Mohite
- 31,138
- 12
- 157
- 147
0
First you can use the keyboard_visibility package to do this effectively, I've used it and it works like charm.
To install
dependencies:
keyboard_visibility: ^0.5.2
Usage
import 'package:keyboard_visibility/keyboard_visibility.dart';
bool keyBoardState;
@protected
void initState() {
super.initState();
KeyboardVisibilityNotification().addNewListener(
onChange: (bool visible) {
print(visible);
keyBoardState = visible;
},
);
}
now before you call other page first check the keyBoardState is true or false then do your job.

Mohammad Mirshahbazi
- 793
- 9
- 22