我使用的form_field_validator
包提供了许多简单快速的验证方法,但我面临着一个奇怪的问题。我正在尝试创建一个可重复使用的TextFormField
输入,它只接受验证和一些字符串,但flutter拒绝让我使用这种类型的验证,尽管如果我直接使用它而不传递它,它会很有魅力。代码:
Widget input(String name, String value, var validator) {
return Padding(
padding: EdgeInsets.only(
left: responsiveWidth(20), right: responsiveWidth(20)),
child: Column(
children: [
Text(name),
SizedBox(
height: responsiveHeight(10),
),
Container(
width: SizeConfig.screenWidth * 0.9,
child: TextFormField(
validator: validator,
decoration: inputDecoration(context, value),
),
),
],
),
);
}
Main:
// Other code
input("Email", "email@gmail.com",
RequiredValidator(errorText: "Required")
)
// Other code
但我得到了这个错误:type 'RequiredValidator' is not a subtype of type '((String?) => String?)?'
如有任何帮助,我们将不胜感激。
试试这个:
Main:
input("Email", "email@gmail.com")
自定义小工具为:
final requiredValidator = EmailValidator(errorText: 'Error Text'); //Add this on top of input widget.
Widget input(String name, String value) {
return Padding(
padding: EdgeInsets.only(
left: responsiveWidth(20), right: responsiveWidth(20)),
child: Column(
children: [
Text(name),
SizedBox(
height: responsiveHeight(10),
),
Container(
width: SizeConfig.screenWidth * 0.9,
child: TextFormField(
validator: requiredValidator,
decoration: inputDecoration(context, value),
),
),
],
),
);
}