我需要专注于TextField而不显示键盘。 如果用户点击TextField,则仅需要显示键盘。
我尝试了两种方法。
这样,键盘显示出来,并且在构建屏幕后隐藏了,看不到。
Builder:
TextFormField(
controller: barcodeStringController,
focusNode: myFocusNode,
autofocus: true,
textAlign: TextAlign.right,
textInputAction: TextInputAction.done,
textCapitalization: TextCapitalization.characters,
style: TextStyle(fontSize: 20),
maxLines: null,
onTap: () {
SystemChannels.textInput.invokeMethod('TextInput.show');
},
//... Other event listeners to handle submit
),
并且超出了build():
void _onAfterBuild(BuildContext context) {
SystemChannels.textInput.invokeMethod('TextInput.hide');
}
通过这种方式,键盘可以隐藏在较低的位置,并且看起来更好看。问题在于,从不调用onTap,所以键盘从不显示,也从不显示onTap。
Builder:
GestureDetector(
onTap: () {
SystemChannels.textInput.invokeMethod('TextInput.show');
},
child: TapOnlyFocusTextField(
controller: barcodeStringController,
focusNode: myFocusNode, //this is a TapOnlyFocusTextFieldFocusNode
textAlign: TextAlign.right,
textInputAction: TextInputAction.done,
textCapitalization: TextCapitalization.characters,
style: TextStyle(fontSize: 20),
cursorColor: Colors.black,
//... Other event listeners to handle submit
),
),
TapOnlyFocusTextField类:
class TapOnlyFocusTextField extends EditableText {
TapOnlyFocusTextField({
@required TextEditingController controller,
@required TextStyle style,
@required Color cursorColor,
bool autofocus = false,
Color selectionColor,
FocusNode focusNode,
TextAlign textAlign,
TextInputAction textInputAction,
TextCapitalization textCapitalization,
onSubmitted,
onChanged,
}) : super(
controller: controller,
focusNode: focusNode,
style: style,
cursorColor: cursorColor,
autofocus: autofocus,
selectionColor: selectionColor,
textAlign: textAlign,
textInputAction: textInputAction,
textCapitalization: textCapitalization,
maxLines: null,
onSubmitted: onSubmitted,
backgroundCursorColor: Colors.black,
onChanged: onChanged,
);
@override
EditableTextState createState() {
return TapOnlyFocusTextFieldState();
}
}
class TapOnlyFocusTextFieldState extends EditableTextState {
@override
void requestKeyboard() {
super.requestKeyboard();
//hide keyboard
SystemChannels.textInput.invokeMethod('TextInput.hide');
}
}
TapOnlyFocusTextFieldFocusNode类:
class TapOnlyFocusTextFieldFocusNode extends FocusNode {
@override
bool consumeKeyboardToken() {
// prevents keyboard from showing on first focus
return false;
}
}
我知道Flutter Github中已经存在一个未解决的问题,但是我需要找到一个解决方案: https://github.com/flutter/flutter/issues/16863
答案 0 :(得分:0)
text_input.dart有一个方法
void show({bool ifShowKeyBoard = true}) {
assert(attached);
if (ifShowKeyBoard) {
TextInput._instance._show();
}
}
我这样更改了方法,它有效
答案 1 :(得分:0)
如下所示:https://stackoverflow.com/a/60392327/4693979
但我更喜欢在 initState 中执行此操作:
void initState() {
super.initState();
Future.delayed(
Duration(),
() => SystemChannels.textInput.invokeMethod('TextInput.hide'),
);
}
不要忘记将自动对焦设置为 true。