如何让这个TextField始终具有焦点
我正在使用外部条形码扫描仪,我想在提交后保持焦点,这样我就可以立即扫描其他条形码扫描仪,而无需再点击或单击它。
过程非常简单: 用户扫描条形码,然后在此条形码上执行一些操作,然后我想将焦点重新放在同一个文本字段上,这样我就可以扫描其他条形码,而无需单击文本字段来手动给出,我想自动进行。
如何存档? 谢谢您的宝贵时间。
这是我的代码:
@override
Widget build(BuildContext context) {
TextEditingController search = TextEditingController();
FocusNode searchNode = FocusNode();
return Padding(
padding: const EdgeInsets.all(20),
child: ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 50,
width: 400,
child: TextField(
focusNode: searchNode,
controller: search,
autofocus: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
label: Text("Cerca prodotto...")),
onChanged: (val) => _runFilter(val),
onSubmitted: (val) async {
print(await product.productExists());
},
),
),
SizedBox(
height: 50,
child: Row(
children: [
SizedBox(
height: 50,
width: 200,
child: CheckboxListTile(
value: autoInsert,
title: const Text("Aggiunta automatica"),
onChanged: (val){
setState(() {
autoInsert = val!;
});
},
)
),
SizedBox(
height: 50,
child: ElevatedButton(
onPressed: () {},
child: const Text("Nuovo prodotto")),
)
],
),
)
/**
* Il resto qui
*/
],
)
],
));
}
```
I tried with:
searchNode.requestFocus()
Focus.of(context).requestFocus(searchNode) <-- this gives error
I'm using an external barcode scanner and I would like to maintain the focus after the submit, so I can scan an other barcode scanner immediately, without tapping or clicking it anymore.
The process is very simple:
The user scan a barcode, then on this barcode will be done some operations, then I would like to give focus back on the same TextField, so I can scan an other barcode without clicking on the TextField for giving that manually, I would like to make that automatically.
How to archive that?
Thanks you for your time.
Here my code:
@override
Widget build(BuildContext context) {
TextEditingController search = TextEditingController();
FocusNode searchNode = FocusNode();
return Padding(
padding: const EdgeInsets.all(20),
child: ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 50,
width: 400,
child: TextField(
focusNode: searchNode,
controller: search,
autofocus: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
label: Text("Cerca prodotto...")),
onChanged: (val) => _runFilter(val),
onSubmitted: (val) async {
print(await product.productExists());
},
),
),
SizedBox(
height: 50,
child: Row(
children: [
SizedBox(
height: 50,
width: 200,
child: CheckboxListTile(
value: autoInsert,
title: const Text("Aggiunta automatica"),
onChanged: (val){
setState(() {
autoInsert = val!;
});
},
)
),
SizedBox(
height: 50,
child: ElevatedButton(
onPressed: () {},
child: const Text("Nuovo prodotto")),
)
],
),
)
/**
* Il resto qui
*/
],
)
],
));
}
```
I tried with:
searchNode.requestFocus()
Focus.of(context).requestFocus(searchNode) <-- this gives error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论