TextFormField验证程序,TextFormField上不可见错误

时间:2018-11-29 09:31:54

标签: dart flutter flutter-layout

我正在尝试创建带有列的表单。

例如,表单=>列=>文本字段,

TextFields将具有验证器。 另外,TextFields会进行修饰。

但是当我单击RaisedButton时,在TextField上找不到错误。 尽管TextFormField中的返回已成功执行。 (我使用调试器进行了检查)

到目前为止,我所引用的链接如下: https://www.youtube.com/watch?v=xiEYNzU4bDg

https://iirokrankka.com/2017/10/17/validating-forms-in-flutter/

https://medium.com/@nitishk72/form-validation-in-flutter-d762fbc9212c

下面是我的代码,请帮助我解决此问题

import 'package:flutter/material.dart';

class Profile extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new ProfileState();
  } //createState closes here....
} //Profile closes here.....

class ProfileState extends State<Profile> {
  @override
  Widget build(BuildContext context) {
    final _formKey = GlobalKey<FormState>();

    return new Scaffold(
        appBar: new AppBar(title: new Text("Profile")),
        body: Form(
          key: _formKey,
          child: new Column(
            children: <Widget>[
              //Full Name TextGormFeild goes below....
              new TextFormField(
                  validator: (String value) {
                    if (value.isEmpty) {
                      return "Full name cannot be empty.";//Control comes here when I check using the Debugger.
                    } //if(value.isEmpty) closes here....
                  },
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.next,
                  maxLines: 1,
                  decoration: InputDecoration(
                      border: new OutlineInputBorder(
                          borderSide: new BorderSide(color: Colors.grey)),
                      hintText: "Enter name")),

              new RaisedButton(
                child: new Text("Test"),
                onPressed: () {
                  setState(() {
                    if (_formKey.currentState.validate()) {
                      print("Validations are correct.");
                    }
                  });
                },
              )
            ],
          ),
        )

        //,
        // helperText: "Enter full name"
        );
  } //build closes here.....

} //ProfileState closes here....

我以前使用的是helperText,但是我将其删除了。

1 个答案:

答案 0 :(得分:1)

尝试从onPressed上移除setState

setState(() {

});

仅放置

if (_formKey.currentState.validate()) {
  print("Validations are correct.");
}

像下面这样:

onPressed: () {
    if (_formKey.currentState.validate()) {
      print("Validations are correct.");
    }
},