【问题标题】:Flutter How to always hide keyboard when click on TextField but keep focus(Keep show cursor)Flutter单击TextField时如何始终隐藏键盘但保持焦点(保持显示光标)
【发布时间】:2019-08-09 13:40:07
【问题描述】:

我无法控制键盘的显示或隐藏,在我的项目中,我需要始终隐藏键盘但保持专注以显示我的自定义键盘(一个小部件)。

这就是我想要的

这是我的问题

【问题讨论】:

  • 导入 'package:flutter/services.dart'; SystemChannels.textInput.invokeMethod('TextInput.hide');
  • @Kenneth Li void initState() { focusNode.addListener(_changeFocus); SystemChannels.textInput.invokeMethod('TextInput.hide'); super.initState(); } 不行
  • @Günter Zöchbauer 这种方式是使用解决方案自定义 EditableText 并添加 SystemChannels.textInput.invokeMethod('TextInput.hide') 它可以工作,但我们可以自定义 TextField 并在请求键盘中添加此代码?
  • @quyenphongtranvuong 你能告诉我你是怎么得到这个功能的,因为我需要同样的东西。

标签: flutter flutter-layout


【解决方案1】:

插入NoKeyboardEditableText 而不是你的TextField

class NoKeyboardEditableText extends EditableText {

  NoKeyboardEditableText({
    @required TextEditingController controller,
    TextStyle style = const TextStyle(),
    Color cursorColor = Colors.black,
    bool autofocus = false,
    Color selectionColor
  }):super(
      controller: controller,
      focusNode: NoKeyboardEditableTextFocusNode(),
      style: style,
      cursorColor: cursorColor,
      autofocus: autofocus,
      selectionColor: selectionColor,
      backgroundCursorColor: Colors.black
  );

  @override
  EditableTextState createState() {
    return NoKeyboardEditableTextState();
  }

}

class NoKeyboardEditableTextState extends EditableTextState {

  @override
  Widget build(BuildContext context) {
    Widget widget = super.build(context);
    return Container(
      decoration: UnderlineTabIndicator(borderSide: BorderSide(color: Colors.blueGrey)),
      child: widget,
    );
  }

  @override
  void requestKeyboard() {
    super.requestKeyboard();
    //hide keyboard
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }
}

class NoKeyboardEditableTextFocusNode extends FocusNode {
  @override
  bool consumeKeyboardToken() {
    // prevents keyboard from showing on first focus
    return false;
  }
}

【讨论】:

  • 谢谢!我得到了这种方式,但它在 TextField 中没有我需要的某些属性,例如:将光标移动到在单词之前进行编辑...有什么方法可以在焦点时禁用 TextField 的键盘?
  • 我认为,移动光标可以使用TextEditingController。我不知道TextField 的合适解决方案,但我建议您查看EditableText 来源。在我的示例中,属性很少,但EditableText 有很多。所以,也许你可以将所有需要的字段添加到你的类中,扩展 EditableText 就足够了。
【解决方案2】:

你可以使用自定义的focusNode

这可以防止键盘仅在第一次点击时出现:

TextField(focusNode: FirstDisabledFocusNode(),)

class FirstDisabledFocusNode extends FocusNode {
  @override
  bool consumeKeyboardToken() {
    return false;
  }
}

这总是防止:

TextField(focusNode: AlwaysDisabledFocusNode())

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  bool get hasFocus => false;
}

【讨论】:

  • 感谢您的回答@Defiant UA - 在第一种方式中,当您双击 TextField 键盘时,FirstDisabledFocusNode 仍然显示第二种方式我需要显示光标。有什么方法可以禁用键盘仍然继续使用 TextField
  • 抱歉,我不知道其他解决方案。是的 - 正如我所写 - 我的第一个解决方案仅适用于第一次点击 TextField
【解决方案3】:

要隐藏键盘并保持光标可见,请将readOnly 设置为true 并将showCursor 设置为true

TextFormField(
  showCursor: true,
  readOnly: true),

flutter/issues/#16863

【讨论】:

  • 需要注意的是,当 readOnly 为 true 时,不能使用 AutofillHints。
【解决方案4】:

尝试使用input_with_keyboard_control

它帮助我解决了我的问题,即在不显示键盘的情况下从条形码扫描仪接收文本

【讨论】:

    【解决方案5】:

    仅供参考,TextInputType.none 是在 #83974 中引入的:

    文本字段( 键盘类型:TextInputType.none, ... )

    【讨论】:

    • 为我工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2015-07-24
    • 2021-09-16
    • 1970-01-01
    • 2020-09-26
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多