我有一个TextField小部件,提交onSubmitted()
后,我想用readonly = false
继续关注这个TextField,但同时我想最小化键盘或关闭它,我不能使用
FocusScope.of(context).requestFocus(new FocusNode());
或
FocusScope.of(context).unfocus();
或
SystemChannels.textInput.invokeMethod('TextInput.hide');
当调用onSubmit时,键盘将被禁用,因为readOnly为true,但TextField将具有焦点。但当你点击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(() {});
},
),
),
),
));
}
}