简体   繁体   中英

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)

  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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