【问题标题】:flutter - requires to double tap on the button to get the FocusNode颤振 - 需要双击按钮来获取 FocusNode
【发布时间】:2021-03-29 13:34:18
【问题描述】:

我有这个简单的代码,其中包含一个 TextField 和一个 Edit 按钮。 TextField 最初有提示文本。当用户单击“编辑”按钮时,我想启用文本字段并打开键盘以编辑该值。

 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;
            },
          ),
        ],

“editProvider”是在另一个类中声明的变量,其代码如下:

class UpdateDataProvider with ChangeNotifier {
  bool _isEnabled = false;

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

问题是它正在工作,但我需要在按钮上点击两次才能将注意力集中在文本字段上。 这是我的用户界面:

我还想知道如何在没有文档 ID 的情况下在数据库上运行更新查询。 假设我必须做一个查询,比如 update usersTable set name = "Abc" where rollno = 12;这是 sql 表单查询。 如何在颤振中做到这一点。 谢谢你的帮助..

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    看起来使用了 StatelessWidget。如果是,则小部件首先获得焦点,然后使用新的 FocusNode 从头开始​​重建,并且您必须第二次点击它以请求新的 FocusNode。一种可能的解决方案是将您的小部件转换为 StatefulWidget 并将 FocusNode 初始化放在 initState() 中。

    【讨论】:

    • 哦。我还没有考虑过。会尝试的。谢谢
    【解决方案2】:

    我遇到了同样的问题。我所做的是首先启用文本字段,然后在等待 20 毫秒后使用焦点节点。像这样:

    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();});
            },
          ),
        ],
    

    他们似乎需要时间先启用该领域,然后再专注于它。 根据您的工作,时间可能更长或更短。我的在同一个班级,所以也许有一个提供者,你需要更多的时间。我希望它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多