首先感谢您的阅读。
我正在尝试使用Flutter创建登录名,并为"TextFormField"
和其他"RaisedButton"
创建一个类。问题是,当我按下用于验证输入(电子邮件和密码)的按钮时,它不会更改状态(“验证文本不会出现)。
我只需要按下Login(登录)按钮并使用输入进行验证即可。
有人可以帮助我了解它的工作方式以及如何编写好的代码?
谢谢!
这是我的主要课程:
Form(
key: _formKey,
autovalidate: _autoValidate,
child: new Column(
children: <Widget>[
//Both with the same class
//This is my custom Input for email
new PrimaryInput("Email", (value){_email = value;}, email: true),
//This is my custom Input for password
PrimaryInput("Password", (value){_password = value;}, password: true),
//Custom Button validation
PrimaryButton(
text: "Login",
onClick: (){
validateAndSubmit(_email, _password);
},
),
],
),
),
这是输入窗口小部件
class PrimaryInput extends StatefulWidget {
PrimaryInput(String this.hintText, this.onClick, {this.email, this.password, this.textLength, this.minLength});
String hintText;
bool email;
bool password;
bool textLength;
int minLength = 0;
final Function(String value) onClick;
@override
PrimaryInputState createState() => PrimaryInputState();
}
class PrimaryInputState extends State<PrimaryInput> {
@override
Widget build(BuildContext context) {
return new Container(
decoration: new BoxDecoration(
border: new Border.all(
width: 2, color: Colors.red,
style: BorderStyle.solid
)
),
child: new TextFormField(
textAlign: TextAlign.start,
keyboardType: TextInputType.emailAddress,
decoration: new InputDecoration(
border: InputBorder.none,
hintText: widget.hintText,
contentPadding: EdgeInsets.only(left: 10, top: 10, bottom: 10),
),
validator: (String value) {
if(widget.email){
return FormValidation.validateEmail(value);
}else if(widget.password){
return FormValidation.validatePassword(value);
}else if(widget.textLength){
return FormValidation.validateMinLength(value, widget.minLength, "Lenght ${widget.minLength}");
}
},
onSaved: (String value){
widget.onClick(value);
},
)
)
}
}
这是Button类:
class PrimaryButton extends StatelessWidget {
PrimaryButton({this.onClick, this.text});
final String text;
final Function() onClick;
@override
Widget build(BuildContext context) {
return new RaisedButton(
onPressed: () {
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Button action");
},
elevation: 4,
color: Colors.red,
child: new Container(
child: new Center(
child: new Text(text,
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 23,
color: Colors.white,
letterSpacing: 2,
fontWeight: FontWeight.bold
)
),
),
width: 350,
height: 50,
),
textColor: Colors.white,
);
}
}