我正在尝试创建一个带有列的表单。
例如,表单=>列=> 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
下面是我的代码,请求帮助我解决这个问题。
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,但我已经删除了它。
发布于 2018-11-29 09:43:36
尝试从您的setState中删除onPressed
setState(() {
});只放
if (_formKey.currentState.validate()) {
print("Validations are correct.");
}如下所示:
onPressed: () {
if (_formKey.currentState.validate()) {
print("Validations are correct.");
}
},https://stackoverflow.com/questions/53535770
复制相似问题