简体   繁体   English

关闭键盘时从 TextField 中移除焦点

[英]Remove focus from TextField when the keyboard is dismissed

It seems that a TextField does not lose the focus when the keyboard is dismissed.关闭键盘时似乎 TextField 不会失去焦点。 That makes the cursor continually blink, and more seriously, when the main window gets focus again, for example, after showing a dialogue box, the keyboard pops up again.这使得 cursor 不断闪烁,更严重的是,当主 window 再次获得焦点时,例如显示一个对话框后,键盘再次弹出。 Can I make the TextField lose focus when the keyboard is dismissed?当键盘被关闭时,我可以让 TextField 失去焦点吗?

return Scaffold(
  body: Padding(
    padding: EdgeInsets.all(16),
      child: Center(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        TextField(decoration: InputDecoration(hintText: "Example"),),
        RaisedButton(
            child: Text("Test"), onPressed: () => onButtonClick(context))
      ],
    ),
  )),
);

在此处输入图像描述

Try using this when clicking on the button:单击按钮时尝试使用它:

FocusScope.of(context).unfocus();

and if you have a textEditingController (which I recommend using) you can call to clear the field如果您有一个 textEditingController (我推荐使用),您可以调用以清除该字段

_textEditingController.clear();

but you have to create the controller first:但您必须先创建 controller:

final _textEditingController = TextEditingController();

and when you create your TextField:当您创建 TextField 时:

TextField(
    decoration: InputDecoration(hintText: "Example"),
    controller: _textEditingController,
),

Use TextEditingController :使用TextEditingController

First Initialise:首先初始化:

TextEditingController _textController = new TextEditingController();

code:代码:

TextField(controller: _textController),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM