简体   繁体   English

如何将焦点添加到具有验证错误的 textformfield

[英]How to add focus to textformfield which has validation error

I have textformfields in my form.我的表单中有文本表单字段。 When I click on submit button the validation error displays under the textformfield.当我单击提交按钮时,验证错误会显示在 textformfield 下。 I want to add focus to that particular field so when user clicks on save button the field pops up.我想将焦点添加到该特定字段,因此当用户单击保存按钮时,该字段会弹出。 user should not need to scroll up and type the info.用户不需要向上滚动并输入信息。 User should be able to directly start typing.用户应该能够直接开始输入。 In current scenario user gets confused and is unaware of why save button didnt work.在当前情况下,用户会感到困惑并且不知道为什么保存按钮不起作用。 Bcoz until scrolled up the error is unknown. Bcoz 直到向上滚动错误是未知的。

 TextFormField(
                  controller: NoteController,
                   validator: (String value){
                    if(value.isEmpty)
                      {
                        return "Note can't be null";
                      }
                    else
                      return null;
                   },
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide: const BorderSide(width: 2.0),)),
                  keyboardType: TextInputType.multiline,
                  minLines: 5,
                  maxLines: 5, 
                  onChanged: (value) {
                    this.note.note = value;
                  },

                ),

bool validateAndSave() {
    final form = globalFormKey.currentState;
    if (form.validate()) {
      form.save();
      return true;
    }
    return false;
  }

void _save() async {
    if (validateAndSave()) {
}
}

You can achieve that using FocusNode and TextField.您可以使用 FocusNode 和 TextField 来实现。 you can read more about it here https://flutter.dev/docs/cookbook/forms/focus你可以在这里阅读更多关于它的信息https://flutter.dev/docs/cookbook/forms/focus

Let me show you an example (Tried it and it works):让我向您展示一个示例(尝试过并且有效):

Before the build method:在构建方法之前:

  final FocusNode noteFocus = FocusNode();

In the build method:在构建方法中:

 Form(
        key: _formKey,
        child: Column(
          children: [
            TextFormField(
              // Add FocusNode to TextFieldForm
              focusNode: noteFocus,
              validator: (value) {
                if (value == null || value.isEmpty) {
                  // Request focus in case of error
                  noteFocus.requestFocus();
                  return "Note can't be null";
                }
                return null;
              },
            ),
            ElevatedButton(
              onPressed: () {
                if (_formKey.currentState!.validate()) {
                  print('Validated');
                }
              },
              child: Text('Submit'),
            )
          ],
        ),
      ),

Let me know if you need anything else:)需要帮助请叫我:)

Use FocusNode like this.像这样使用 FocusNode。

FocusNode focusNode = FocusNode();

TextFormField(
                  controller: NoteController,
                  focusNode: focusNode,
                   validator: (String value){
                    if(value.isEmpty)
                      {
                        Focus.of(context).requestFocus(focusNode);
                        return "Note can't be null";
                      }
                    else
                      return null;
                   },
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide: const BorderSide(width: 2.0),)),
                  keyboardType: TextInputType.multiline,
                  minLines: 5,
                  maxLines: 5, 
                  onChanged: (value) {
                    this.note.note = value;
                  },

                ),

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

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