Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unfocus TextField that has custom FocusNode?

Tags:

flutter

I know that general answer to unfocusing is to use this piece of code: FocusScope.of(context).requestFocus(new FocusNode());

But when TextField has custom focusNode, this code doesn't seem to work.

SystemChannels.textInput.invokeMethod('TextInput.hide'); still works, but it only removes the keyboard - field itself is still selected.

The code (irrelevant parts removed):

class RegisterScreen extends StatelessWidget {   final phoneNumberTEC = TextEditingController();   final passwordTEC = TextEditingController();   final passwordFocusNode = FocusNode();    @override   Widget build(BuildContext context) {     return this.keyboardDismisser(       context: context,       child: Scaffold(         appBar: new AppBar(           title: new Text("Register"),         ),         body: this.page(context),         resizeToAvoidBottomPadding: false,       ),     );   }    Widget keyboardDismisser({BuildContext context, Widget child}) {     final gesture = GestureDetector(       onTap: () {         this.passwordFocusNode.unfocus();         FocusScope.of(context).requestFocus(new FocusNode());         SystemChannels.textInput.invokeMethod('TextInput.hide');       },       child: child,     );     return gesture;   }    Widget page(BuildContext context) {     return Column(         mainAxisAlignment: MainAxisAlignment.spaceBetween,         children: <Widget>[           Container(             padding: new EdgeInsets.all(16.0),             child: Column(                 mainAxisAlignment: MainAxisAlignment.center,                 crossAxisAlignment: CrossAxisAlignment.center,                 children: <Widget>[                   this.phoneNumberTextField(context),                   this.passwordTextField(context),                 ]             ),           ),           // cutting irrelevant widgets out           )         ]     );   }    Widget phoneNumberTextField(BuildContext context) {     return TextField(       controller: this.phoneNumberTEC,       decoration: InputDecoration(hintText: "Phone number"),       onSubmitted: (string) {         FocusScope.of(context).requestFocus(this.passwordFocusNode);       },     );   }    Widget passwordTextField(BuildContext context) {     return TextField(       controller: this.passwordTEC,       decoration: InputDecoration(hintText: "Password"),       obscureText: true,       focusNode: this.passwordFocusNode,       onSubmitted: (string) {         this.performRegister(context);       },     );   }  } 
like image 287
desudesudesu Avatar asked Nov 26 '18 12:11

desudesudesu


People also ask

How do you Unfocus FocusNode in Flutter?

Try setting focus on the four text fields by selecting them, and then select "UNFOCUS" to see what happens when the current FocusManager. primaryFocus is unfocused. Try pressing the TAB key after unfocusing to see what the next widget chosen is.

What is FocusNode in TextFormField Flutter?

FocusNode basically focuses on the keyboard event. Let's suppose you want to control the TextFormField widget as soon as the user taps on it, then you use FocusNode . So, FocusNode class is basically an object that can be used to obtain keyboard focus and to handle keyboard events.

How do I know if my TextField is focused in Flutter?

The easiest and simplest solution is to add the onTap method on TextField. There are other means for a TextField to get focus, for example if it's the first TextField on a page and future Flutter versions will support tabbing through fields.


1 Answers

Here is a similar answer to @kasiara's answer but another way.

FocusScope.of(context).unfocus(); _textEditingController.clear(); 
like image 134
Blasanka Avatar answered Oct 01 '22 18:10

Blasanka