颤动如何在单击TextField时始终隐藏键盘但保持焦点(保持显示光标)

问题描述 投票:0回答:2

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

需要帮忙!!!

这是我想要的

这是我的问题

flutter flutter-layout
2个回答
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;
  }
}

0
投票

您可以使用自定义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;
}
© www.soinside.com 2019 - 2024. All rights reserved.