首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextFormField验证器,错误在TextFormField上不可见

TextFormField验证器,错误在TextFormField上不可见
EN

Stack Overflow用户
提问于 2018-11-29 09:31:53
回答 1查看 2.7K关注 0票数 1

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

例如,表单=>列=> TextFields,

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

下面是我的代码,请求帮助我解决这个问题。

代码语言:javascript
复制
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,但我已经删除了它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-29 09:43:36

尝试从您的setState中删除onPressed

代码语言:javascript
复制
setState(() {

});

只放

代码语言:javascript
复制
if (_formKey.currentState.validate()) {
  print("Validations are correct.");
}

如下所示:

代码语言:javascript
复制
onPressed: () {
    if (_formKey.currentState.validate()) {
      print("Validations are correct.");
    }
},
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53535770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档