答案 0 :(得分:27)
要隐藏键盘并保持光标可见,请将readOnly
设置为true
,将showCursor
设置为true
。
TextFormField(
showCursor: true,
readOnly: true),
答案 1 :(得分:1)
尝试使用 input_with_keyboard_control
包
它帮助我解决了从条码扫描仪接收文本而不显示键盘的问题
答案 2 :(得分:0)
插入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;
}
}
答案 3 :(得分: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;
}