简体   繁体   English

Flutter focusnode 问题不起作用 - 只有单击它才有效

[英]Flutter focusnode problem not working - only by clicking it works

iam using a device that scans barcode, I want after each read the focus to return the TextFormFiled - the code below do the work and I see the cursor is focused on the TextFormFiled but when I read next time its show nothing, I need to manually just click by my finger on the textfiled to activate the focus,can somebody help me ( the device returned LF after each read)我正在使用扫描条形码的设备,我希望在每次阅读焦点后返回 TextFormFiled - 下面的代码完成工作,我看到 cursor 专注于 TextFormFiled 但是当我下次阅读时它什么也没显示,我需要手动只需用我的手指点击文本文件即可激活焦点,有人可以帮助我吗(设备在每次阅读后返回 LF)

  TextFormField(
                                    decoration: new InputDecoration(
                                        border: new OutlineInputBorder(
                                          borderRadius: const BorderRadius.all(
                                            const Radius.circular(10.0),
                                          ),
                                        ),
                                        filled: true,
                                        hintStyle: new TextStyle(
                                            color: Colors.grey[800]),
                                        hintText: "Read BarCode",
                                        fillColor: Colors.white70),
                                    focusNode: myFocusNode,
                                    controller: search,
                                    autofocus: true,
                                    maxLines: null,
                                    validator: (value) {
                                      //    print(value.toString().runes);
                                      if (value.toString().contains("\n")) {
                                        fetchProducts(value!);
                                        search.text = "";
                                      } else {}
                                    },
                                  ),

Use your myFocusNode to activate the focus on textField.使用您的myFocusNode激活对 textField 的焦点。

void function(){
  /// after scanning is complete call this 
  focusNode.requestFocus()
}

I pass for this and did solve it like this:我通过了这个并确实像这样解决了它:

_addItem() {
    final isValid = _formKey.currentState?.validate() ?? false;
    if (!isValid) {
        return;
    }
    final ean = int.parse(_eanController.text);
    listaEan.add(ean);
    _eanController.text = '';
    setState(() {
        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
            _eanFocus.requestFocus();
        });
    });
}

but in physical scan device, did works fine.但在物理扫描设备中,确实工作正常。 without use the addPostFramaCallback.不使用 addPostFramaCallback。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM