【问题标题】:Flutter GestureDetector misses onTap when another TextField has focus当另一个 TextField 具有焦点时,Flutter GestureDetector 错过了 onTap
【发布时间】:2020-06-21 11:55:32
【问题描述】:

您好,在 Flutter 中,我有一个 TextFormField 和一个 GestureDetector。问题是当 TextFormField 具有焦点(用户正在键入)然后用户单击 GestureDetector 时,onTap 未被调用,因此用户必须单击两次才能从 TextFormField 中移除焦点,然后再次激活 GestureDetector,这很烦人为用户。即使 TextFormField 有焦点,请有任何提示来检测点击?谢谢!

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我认为你可以添加

    FocusScope.of(context).unfocus();
    

    在“onEditingComplete”或“onTap”处从文本字段中移除焦点。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      您的 GestureDetector 是否只是失去焦点?如果是这样,请尝试使用 Gesture Detector 包裹您的脚手架并添加 onTap 如下代码

            onTap: () {
      FocusScopeNode currentFocus = FocusScope.of(context);
      if (!currentFocus.hasPrimaryFocus) {
        currentFocus.unfocus();
      }
      

      }

      点击脚手架中的任意位置后,TextFormField 将失去焦点

      【讨论】:

        【解决方案3】:

        谢谢大家!这实际上是我的错误,TextFormField 中有一个监听器触发了 setState 并淹没了 ontap。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-10-07
          • 1970-01-01
          • 1970-01-01
          • 2020-07-28
          • 1970-01-01
          • 2023-01-10
          • 1970-01-01
          相关资源
          最近更新 更多