Home > Back-end >  Textfield remains focused after keyboard is dismissed?
Textfield remains focused after keyboard is dismissed?

Time:07-01

In my case i have used textfield for searching , when user click on it keyboard appears and when the user press back button the keyboard is dissapeared but the textfield remains focused . And when user open the drawer and close it the keyboard appears automatically .

SizedBox(
      height: 40,
      child:  TextField(
          onChanged: (val) {
          print(val);
          },
          decoration: InputDecoration(
              contentPadding: EdgeInsets.all(8),
           hintText:AppLocalizations.of(context)!.searchnameoremail,
              // hintStyle: TextStyle(fontSize: 15),
              fillColor: Theme.of(context).dialogBackgroundColor,
              filled: true,
              border: OutlineInputBorder(
                borderSide: BorderSide.none,
                borderRadius: const BorderRadius.all(Radius.circular(10)),
              ),
              suffixIcon: Padding(
                padding: const EdgeInsets.all(defaultPadding - 2),
                child: SvgPicture.asset("assets/icons/Search.svg",
                    color: Theme.of(context).dialogBackgroundColor),
              )
             
              ),
        ),
      
    );

CodePudding user response:

You have to add below line:

FocusManager.instance.primaryFocus.unfocus()

CodePudding user response:

FocusScope.of(context).unfocus(); _textEditingController.clear();

CodePudding user response:

You have to unfocus Keyboard. In simple words dismiss keyboard

Use it like this

hideKeyBoard(BuildContext context) {
  FocusScope.of(context).requestFocus(FocusNode());
}

call this hide keyboard method when you are tapping on back button.

CodePudding user response:

keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
      if(!visible){
        FocusManager.instance.primaryFocus?.unfocus();
      }
  });

  • Related