gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:13:01 25 4
gpt4 key购买 nike

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

喜欢,表单 => 列 => 文本字段,

TextFields 将有验证器。此外,TextFields 将进行装饰。

但是当我点击 RaisedButton 时,我找不到 TextField 上的错误。TextFormField中的return虽然执行成功了。 (我使用调试器检查过)

我目前引用的链接如下: 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,但我已经删除了它。

最佳答案

尝试从你的 onPressed 中移除 setState

setState(() {

});

只放

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

如下所示:

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

关于dart - TextFormField 验证器,错误在 TextFormField 上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53535770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com