简体   繁体   English

flutter - 需要双击按钮来获取 FocusNode

[英]flutter - requires to double tap on the button to get the FocusNode

I have this simple code which contains a TextField and a Edit button.我有这个简单的代码,其中包含一个 TextField 和一个 Edit 按钮。 TextField is having hint text initially. TextField 最初有提示文本。 When user clicks on the Edit button I want to enable the textfield and open the keyboard for editing that value.当用户单击“编辑”按钮时,我想启用文本字段并打开键盘以编辑该值。

 Row(
        children: [
          Container(
            width: MediaQuery.of(context).size.width - 100,
            margin: const EdgeInsets.symmetric(horizontal: 16),
            child: TextFormField(
              focusNode: myFocusNode,
              key: Key("editusernamekey"),
              decoration: InputDecoration(
                hintText: "Username",
                hintStyle: TextStyle(color: Colors.white),
              ),
              enabled: editprovider.isEnabled,
            ),
          ),
          IconButton(
            icon: Icon(Icons.edit, color: Colors.white),
            onPressed: () {
              print("onpressed get called this time");
              myFocusNode.requestFocus();
              editprovider.isEnabled = true;
            },
          ),
        ],

The "editProvider" is a variable declared in a another class whose code is as follows: “editProvider”是另一个class中声明的变量,其代码如下:

class UpdateDataProvider with ChangeNotifier {
  bool _isEnabled = false;

 bool get isEnabled => _isEnabled;
  set isEnabled(bool value) {
    this._isEnabled = value;
    notifyListeners();
  }
}

The issue is it's working but I need to tap two times on the button in order to get focus on textfield.问题是它正在工作,但我需要在按钮上点击两次才能专注于文本字段。 This is my ui:这是我的用户界面: 在此处输入图像描述

Also I wanted to know How I can run a Update Query on the database without the document Id.我还想知道如何在没有文档 ID 的情况下在数据库上运行更新查询。 Suppose I have to do a query like update usersTable set name = "Abc" where rollno = 12;假设我必须做一个查询,比如 update usersTable set name = "Abc" where rollno = 12; this is sql form query.这是 sql 表单查询。 how this can be done in flutter.如何在 flutter 中做到这一点。 Thank you for any help..感谢您的任何帮助..

I had the same problem.我有同样的问题。 what I did was first enable the text field and then after waiting for 20 milliseconds I used the focus node.我所做的是首先启用文本字段,然后在等待 20 毫秒后使用焦点节点。 like this:像这样:

Row(
    children: [
      Container(
        width: MediaQuery.of(context).size.width - 100,
        margin: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          focusNode: myFocusNode,
          key: Key("editusernamekey"),
          decoration: InputDecoration(
            hintText: "Username",
            hintStyle: TextStyle(color: Colors.white),
          ),
          enabled: editprovider.isEnabled,
        ),
      ),
      IconButton(
        icon: Icon(Icons.edit, color: Colors.white),
        onPressed: () {
          print("onpressed get called this time");
          editprovider.isEnabled = true; // changed here and line below
          Timer(Duration(milliseconds: 20), (){
          myFocusNode.requestFocus();});
        },
      ),
    ],

It seems like they need time to first enable the field and then focus on it.似乎他们需要时间先启用该领域,然后再专注于它。 Depending on your work the time may be longer or shorter.根据您的工作,时间可能更长或更短。 Mine was in the same class so maybe with a provider, you would need more time.我的在同一个 class 所以也许有一个供应商,你需要更多的时间。 I hope it works.我希望它有效。

It looks like StatelessWidget is used.看起来像使用了 StatelessWidget。 If it is, then widget at first gets focus, then it rebuilds from scratch with new FocusNode, and you have to tap it second time to request for new FocusNode.如果是,则小部件首先获得焦点,然后使用新的 FocusNode 从头开始重建,并且您必须第二次点击它以请求新的 FocusNode。 One possible solution is to convert your widget to StatefulWidget and put FocusNode initialization in initState().一种可能的解决方案是将您的小部件转换为 StatefulWidget 并将 FocusNode 初始化放在 initState() 中。

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

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