Reputation: 307
When the keyboard is open I am clicking on the device back button keyboard is not hiding it is navigating to the previous page.
Upvotes: 0
Views: 3437
Reputation: 34170
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();
}
Upvotes: 2
Reputation: 923
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.
Upvotes: 0
Reputation: 1151
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(.....)
);
Upvotes: 2