简体   繁体   English

Flutter 带有自定义节点的 TextField 在第一个焦点上隐藏键盘

[英]Flutter TextField with custom node is hiding keyboard on first focus

Hello I have a TextField that is required to be not focused on the first render, I have solved it by settings autofocus property to false and wrapping the whole page with GestureDetector with您好,我有一个 TextField 需要不关注第一个渲染,我通过将 autofocus 属性设置为 false 并使用GestureDetector包装整个页面来解决它

FocusScope.of(context).unfocus();
textfieldController.clear();

and achieved it but I'm having a little bit of a problem when I click text field the first time it hides the keyboard and stays focused, after clicking the second time it works as I expected.并实现了它但是当我第一次单击文本字段时我遇到了一点问题它隐藏了键盘并保持专注,第二次单击它按我预期的方式工作。 The expected result would be textfield starts unfocused and when I click it the keyboard should come out.预期的结果是文本字段开始没有焦点,当我单击它时键盘应该出来。

当前行为

Try using this way..尝试使用这种方式..

return GestureDetector(
      onTap: () => constants.dismissKeyboard(context),
      child: Scaffold(
       .....
      ),
    );

///// /////

void dismissKeyboard(BuildContext context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
    }
  }

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

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