32

I can't control keyboard show or hide, In my project I need to always hide keyboard but keep focused to display my custom keyboard(a widget).

This is I want

And this is my problem

Mijanur Rahaman
  • 353
  • 1
  • 5
  • 19

5 Answers5

87

To hide keyboard and keep the cursor visible, set readOnly to true and showCursor to true.

TextFormField(
  showCursor: true,
  readOnly: true),

See flutter/issues/#16863

Karen
  • 1,423
  • 8
  • 16
17

FYI, TextInputType.none was introduced in #83974:

TextField( keyboardType: TextInputType.none, ... )

Viral
  • 297
  • 3
  • 11
4

You can use custom focusNode

This prevents keyboard appearing only on first tap:

TextField(focusNode: FirstDisabledFocusNode(),)

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

This prevents always:

TextField(focusNode: AlwaysDisabledFocusNode())

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  bool get hasFocus => false;
}
  • Thank your answer @Defiant UA - In first way FirstDisabledFocusNode when you double tap on TextField keyboard still show In Second way I need show cursor. Have any way to disable keyboard still keep using TextField – quyen phong tran vuong Mar 20 '19 at 10:51
  • Sorry, I don't know another solutions. And yes - as I've written - my first solution works only with first tap on `TextField` –  Mar 21 '19 at 07:12
3

Insert NoKeyboardEditableText instead your 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;
  }
}
Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105
  • Thanks! I get this way, but it don't have some property in TextField I need, ex: Move cursor to edit before word... Have any way to disable Keyboard of TextField when focus? – quyen phong tran vuong Mar 20 '19 at 10:35
  • I think, for move cursor you can use `TextEditingController`. I don't know appropriate solution for `TextField`, but I'd recommended you to look in `EditableText` sources. In my example there are just few properties, but `EditableText` has quite a lot of them. So, maybe you can add all needed fields to you class, which extends `EditableText` and this'll be enough. – Andrii Turkovskyi Mar 20 '19 at 13:43
2

try use input_with_keyboard_control package

It helped me solve my problem, of receiving the text from a barcode scanner, without showing the keyboard

Jean Lucas
  • 284
  • 1
  • 9