0

When the keyboard is open I am clicking on the device back button keyboard is not hiding it is navigating to the previous page.

Jagadeesh
  • 277
  • 1
  • 4
  • 11

3 Answers3

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
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.