[英]Flutter: How to close keyboard on onSubmitted() for TextField without lost focus
I have a TextField widget, After submit onSubmitted()
I want to keep focusing on this TextField with readonly = false
but in the same time I want to minimize the keyboard or close it, I can't use我有一个 TextField 小部件,在提交
onSubmitted()
之后,我想继续关注这个 TextField,使用readonly = false
但同时我想最小化键盘或关闭它,我不能使用
FocusScope.of(context).requestFocus(new FocusNode());
or或者
FocusScope.of(context).unfocus();
or或者
SystemChannels.textInput.invokeMethod('TextInput.hide');
When the onSubmit is called, keyboard will be disabled because readOnly is true but TextField would have the focus.当调用 onSubmit 时,键盘将被禁用,因为 readOnly 为真,但 TextField 将具有焦点。 But when you tap on the TextField, keyboard will appear because now readOnly is false and the it already had the focus
但是当您点击 TextField 时,会出现键盘,因为现在 readOnly 是 false 并且它已经有了焦点
class _MyAppState extends State<MyApp> {
bool read = false;
FocusNode focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Container(
padding: EdgeInsets.all(10),
child: TextField(
onTap: () {
read = false;
setState(() {});
},
focusNode: focusNode,
readOnly: read,
cursorColor: Colors.blue,
onSubmitted: (val) async {
print(val);
read = true;
focusNode.requestFocus(new FocusNode());
setState(() {});
},
),
),
),
));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.