简体   繁体   English

初始化initstate时如何聚焦文本字段

[英]How to focus text field when initstate is initialized

I want to focus my text field when initstate is initialized.我想在 initstate 初始化时聚焦我的文本字段。 eg.例如。 when I open any page and then if there is any text field then text field needs to be automatically focused.当我打开任何页面,然后如果有任何文本字段,则文本字段需要自动聚焦。

TextFormField(

                          focusNode: focusNode,
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: orientation == Orientation.portrait
                                ? MediaQuery.of(context).size.width * 0.025
                                : MediaQuery.of(context).size.width * 0.015,
                          ),
                          validator: (val) => Validators.validateRequired(
                              val, " Product Baarcode"),
                          controller: _addproduct,
                          // focusNode: _addproductFocusNode,
                          decoration: InputDecoration(
                            errorStyle: TextStyle(color: Colors.yellow),
                            enabledBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.white),
                            ),
                            fillColor: Colors.white,
                            focusedBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.white),
                            ),
                            hintStyle: TextStyle(color: Colors.white),
                            labelStyle: TextStyle(color: Colors.white),
                            filled: false,
                            prefixIcon: Icon(
                              FontAwesomeIcons.barcode,
                              color: Colors.white,
                            ),
                            labelText: "Enter Product Barcode",
                            hintText: "Enter Product Barcode",
                          ),
                          onFieldSubmitted: (val) {
                            _addProduct();
                          },
                        ),

If you want a TextField to be focused when you open a page, then you can use the property autofocus :如果您希望在打开页面时聚焦TextField ,则可以使用属性autofocus

TextField(
  autofocus: true,
);

If you want it to be focused later point in time, then you can use the class FocusNode .如果您希望它在稍后的时间点得到关注,那么您可以使用FocusNode类。 You can check an example here:您可以在此处查看示例:

https://flutter.dev/docs/cookbook/forms/focus#focus-a-text-field-when-a-button-is-tapped https://flutter.dev/docs/cookbook/forms/focus#focus-a-text-field-when-a-button-is-tapped

You can use the TextFormField's autofocus: parameter and set it to true.您可以使用 TextFormField 的autofocus:参数并将其设置为 true。 This will however make the keyboard appear immediately as well.然而,这也会使键盘立即出现。

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

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