gpt4 book ai didi

flutter - 即使在插入值后 TextFormField 也会显示错误消息(使用 Form Flutter)

转载 作者:行者123 更新时间:2023-12-03 03:53:41 27 4
gpt4 key购买 nike

我正在试验 flutter ,但遇到了一个愚蠢的问题。我在一个屏幕中定义了两个 TextInputField,我正在使用 Form 进行验证。一切正常,我唯一的问题是,即使插入的值正常,错误也不会消失。

以下是输入值之前的屏幕截图。错误也显示在屏幕的开头。

Screen at start

下面是输入值后的屏幕。当我点击注册按钮时,错误仍然显示。

Screen after entering the values

您可以看到,当我单击“注册”按钮时,值会打印在日志中。

Logs

小部件类

class SignUpForm extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SignUpState();
}
}

州级
class SignUpState extends State<SignUpForm> {
final _fbKey = GlobalKey<FormState>();
final UserDetails userDetails = UserDetails();
bool _autoValidate = true;

@override
Widget build(BuildContext context) {
return CustomScreenBg(
AppString.additional_information,
CustomCard(_getSignUpForm(context)),
);
}

_getSignUpForm(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(12),
child: Form(
key: _fbKey,
autovalidate: _autoValidate,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
RichText(
text: TextSpan(
text: AppString.welcome_to,
style: TextStyle(
color: AppColor.BLACK.toColor,
fontSize: 18,
),
children: <TextSpan>[
TextSpan(
text: '${AppString.app_name}',
style: TextStyle(
fontSize: 24,
color: AppColor.PRIMARY_COLOR.toColor,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
SizedBox(height: 4),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SubTitleText(AppString.lets_get_started),
],
),
SizedBox(height: 16),
Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: AppString.enter_name,
errorText: AppString.error_name,
),
keyboardType: TextInputType.text,
validator: (value) {
if (value.length < 4) {
return AppString.error_name;
}
return null;
},
onSaved: (name) {
userDetails.name = name;
},
),
SizedBox(height: 16),
TextFormField(
decoration: InputDecoration(
labelText: AppString.enter_email,
errorText: AppString.error_email,
),
keyboardType: TextInputType.text,
validator: (value) {
if (value.length < 5 || !value.contains('@')) {
return AppString.error_email;
}
return null;
},
onSaved: (email) {
userDetails.email = email;
},
),
],
),
SizedBox(height: 24),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
CustomButton(
AppString.sign_up,
() {
_onSubmit();
},
textColor: AppColor.WHITE.toColor,
bgColor: AppColor.PRIMARY_COLOR.toColor,
),
],
),
SizedBox(height: 12)
],
),
),
);
}

_onSubmit() {
if (_fbKey.currentState.validate()) {
_fbKey.currentState.save();
print(userDetails.name);
print(userDetails.email);
} else {
setState(() {
_autoValidate = true;
});
}
}
}


单击注册按钮时会调用此方法。
_onSubmit() {
if (_fbKey.currentState.validate()) {
_fbKey.currentState.save();
print(userDetails.name);
print(userDetails.email);
} else {
setState(() {
_autoValidate = true;
});
}
}


有人可以帮我弄清楚,我做错了什么吗?

最佳答案

您不必设置 errorText属性,当验证失败时,它将显示验证器回调返回的消息。因此,要解决此问题,您只需删除 errorText来自您的 TextFormField。

关于flutter - 即使在插入值后 TextFormField 也会显示错误消息(使用 Form Flutter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62168095/

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