【发布时间】:2023-04-06 02:16:01
【问题描述】:
所以我想在登录页面上制作错误消息,但是这些消息没有弹出或者在电子邮件和密码错误消息上都不能很好地工作。每当我点击“登录”按钮时,我总是会转到下一页,但它不应该这样做。这是我的代码:
登录表格
class sign_in_form extends StatefulWidget {
const sign_in_form({Key? key}) : super(key: key);
@override
_sign_in_formState createState() => _sign_in_formState();
}
class _sign_in_formState extends State<sign_in_form> {
final _formKey = GlobalKey<FormState>();
String? email;
String? password;
final List<String?> errors = [];
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: <Widget>[
buildEmailFormField(),
SizedBox(height: getProportionateScreenHeight(20)),
buildPasswordFormField(),
SizedBox(height: getProportionateScreenHeight(20)),
errors_form(errors: errors),
SizedBox(height: getProportionateScreenHeight(20)),
splash_button(
press: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
}
},
),
],
),
);
}
TextFormField buildEmailFormField() {
return TextFormField(
keyboardType: TextInputType.emailAddress,
onSaved: (newValue) => email = newValue,
onChanged: (value) {
if (value.isNotEmpty) {
removeError(error: "Please Enter your email");
}
else if (emailValidatorRegExp.hasMatch(value)) {
removeError(error: "Please Enter Valid Email");
}
return;
},
validator: (value) {
if (value!.isEmpty) {
addError(error: "Please Enter your email");
return "";
}
else if (!emailValidatorRegExp.hasMatch(value)) {
addError(error: "Please Enter Valid Email");
return "";
}
return value;
},
decoration: const InputDecoration(
labelText: "Email",
hintText: "Enter your email",
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: Padding(
padding: EdgeInsets.fromLTRB(0, 20, 20, 20),
child: Icon(Icons.email),
),
),
);
}
TextFormField buildPasswordFormField() {
return TextFormField(
obscureText: true,
onSaved: (newValue) => password = newValue,
onChanged: (value){
if (value.isNotEmpty) {
removeError(error: "Please Enter your password");
}
else if (value.length >= 8) {
removeError(error: "Password is too short");
}
return;
},
validator: (value){
if (value!.isEmpty) {
addError(error: "Please Enter your password");
return "";
}
else if (!emailValidatorRegExp.hasMatch(value)) {
addError(error: "Password is too short");
return "";
}
return null;
},
decoration: const InputDecoration(
labelText: "Password",
hintText: "Enter your password",
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: Padding(
padding: EdgeInsets.fromLTRB(0, 20, 20, 20),
child: Icon(Icons.lock),
),
),
);
}
}
我的错误表格
class errors_form extends StatelessWidget {
const errors_form({Key? key, required this.errors,}) : super(key: key);
final List<String?> errors;
@override
Widget build(BuildContext context) {
return Column(
children: List.generate(
errors.length, (index) => form_error_text(errors: errors[index]!)),
);
}
Row form_error_text ({required String errors}) {
return Row(
children: [
SvgPicture.asset(
"assets/icons/Error.svg",
height: getProportionateScreenWidth(14),
width: getProportionateScreenWidth(14),
),
SizedBox(width: getProportionateScreenWidth(14)),
Text(errors),
],
);
}
}
【问题讨论】:
标签: flutter flutter-test