如何让这个TextField始终具有焦点

发布于 2025-01-14 23:23:36 字数 2418 浏览 3 评论 0原文

我正在使用外部条形码扫描仪,我想在提交后保持焦点,这样我就可以立即扫描其他条形码扫描仪,而无需再点击或单击它。

过程非常简单: 用户扫描条形码,然后在此条形码上执行一些操作,然后我想将焦点重新放在同一个文本字段上,这样我就可以扫描其他条形码,而无需单击文本字段来手动给出,我想自动进行。

如何存档? 谢谢您的宝贵时间。

这是我的代码:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文