问题描述
我有一个登录表单验证程序,该验证程序无法按预期工作。当我将email
或password
字段留空或输入基于我在validator
上设置的内容而无法接受的内容时,虽然显示了错误,但我却被定向到登录单击“登录”按钮成功页面,情况并非如此。我想保留在登录页面上,直到输入正确的值。
登录表单
class SignForm extends StatefulWidget {
@override
_SignFormState createState() => _SignFormState();
}
class _SignFormState extends State<SignForm> {
// GlobalKey This uniquely identifies the Form,and allows validation of the form in a later step.
final _formKey = GlobalKey<FormState>();
String email,password;
bool remember = false;
final List<String> errors = [];
// func with named parameter
void addError({String error}) {
if (!errors.contains(error))
setState(() {
errors.add(error);
});
}
void removeError({String error}) {
if (errors.contains(error))
setState(() {
errors.remove(error);
});
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,child: Column(
children: [
// TextFormField - Creates a [FormField] that contains a [TextField].
buildEmailFormField(),SizedBox(height: getProportionateScreenHeight(30)),buildPasswordFormField(),Row(
children: [
CheckBox(
value: remember,activeColor: kPrimaryColor,onChanged: (value) {
setState(() {
remember = value;
});
},),Text("Remember me"),Spacer(),GestureDetector(
onTap: () => Navigator.pushNamed(
context,ForgotPasswordScreen.routeName),child: Text(
"Forgot Password?",style: TextStyle(decoration: Textdecoration.underline),)
],FormError(errors: errors),SizedBox(height: getProportionateScreenHeight(20)),DefaultButton( < -- WHERE I THINK THE ERROR IS
text: 'Login',press: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save(); < -- WHERE I THINK THE ERROR IS
// if all are valid then go to success screen
Navigator.pushReplacementNamed(
context,LoginSuccessScreen.routeName);
}
},)
],);
}
TextFormField buildPasswordFormField() {
return TextFormField(
// obscure visibility of the password
obscureText: true,onSaved: (newValue) => password = newValue,onChanged: (value) {
if (value.isNotEmpty && errors.contains(kPassNullError)) {
removeError(error: kPassNullError);
} else if (value.length >= 8) {
removeError(error: kShortPassError);
}
// In case a user removed some characters below the threshold,show alert
else if (value.length < 8 && value.isNotEmpty) {
addError(error: kShortPassError);
}
return null;
},validator: (value) {
if (value.isEmpty) {
addError(error: kPassNullError);
removeError(error: kShortPassError);
} else if (value.length < 8 && value.isNotEmpty) {
addError(error: kShortPassError);
}
return null;
},decoration: Inputdecoration(
// uses the InputdecorationTheme defined in my theme.dart file
labelText: "Password",hintText: "Enter your password",// When [FloatingLabelBehavior.always] the label will always float at the top of the field above the content.
floatingLabelBehavior: FloatingLabelBehavior.always,suffixIcon: CustomSuffixIcon(
svgIcon: "assets/icons/Lock.svg",);
}
TextFormField buildEmailFormField() {
return TextFormField(
// Requests a keyboard with ready access to the "@" and "." keys.
keyboardType: TextInputType.emailAddress,onSaved: (newValue) => email = newValue,onChanged: (value) {
if (value.isNotEmpty && errors.contains(kEmailNullError)) {
removeError(error: kEmailNullError);
} else if (emailValidatorRegExp.hasMatch(value)) {
removeError(error: kInvalidEmailError);
} else if (value.isNotEmpty && !emailValidatorRegExp.hasMatch(value)) {
addError(error: kInvalidEmailError);
return null;
}
},validator: (value) {
if (value.isEmpty) {
addError(error: kEmailNullError);
removeError(error: kInvalidEmailError);
} else if (value.isNotEmpty && !emailValidatorRegExp.hasMatch(value)) {
addError(error: kInvalidEmailError);
}
return null;
},decoration: Inputdecoration(
// uses the InputdecorationTheme defined in my theme.dart file
labelText: "Email",hintText: "Enter your email",suffixIcon: CustomSuffixIcon(
svgIcon: "assets/icons/Mail.svg",);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)