quyen phong tran vuong
quyen phong tran vuong

Reputation: 413

Flutter How to always hide keyboard when click on TextField but keep focus(Keep show cursor)

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

Upvotes: 41

Views: 74347

Answers (6)

Leong
Leong

Reputation: 11

 TextField(
         obscureText: true, // Mask the input text
         keyboardType: TextInputType.emailAddress, //make it emailAddress type
            )

make keyboardtype as emailAddress, this will avoid phone pop up secure keyboard

Upvotes: 0

Viral
Viral

Reputation: 403

FYI, TextInputType.none was introduced in #83974:

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

Upvotes: 28

Jean Lucas
Jean Lucas

Reputation: 282

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

Upvotes: 1

Karen
Karen

Reputation: 1569

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

Upvotes: 97

user11227161
user11227161

Reputation:

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;
}

Upvotes: 12

Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29438

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;
  }
}

Upvotes: 4

Related Questions