I am creating a reusable TextFormFields
with a reusable validations for its validator. However I am facing an error which is type 'String' is not a subtype of type '((String?) => String?)?
Here is the code for my reusable TextFormFields
class FormFields {
static textFormFieldWidget({
TextEditingController? controller,
String? hintText,
String? helpText,
IconData? prefixIcon,
IconData? suffixIcon,
bool? isPassword,
bool? enabled,
bool? readOnly,
Color? borderColor,
Color? borderSide,
Color? colorFilled,
FocusNode? textFieldFocus,
TextStyle? hintStyle,
String? labelText,
TextStyle? labelStyle,
TextInputType? textInputType,
double rightValue = 0,
double leftValue = 0,
dynamic validateFunction,
}) {
return Padding(
padding: EdgeInsets.only(right: rightValue, left: leftValue),
child: TextFormField(
keyboardType: textInputType,
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: controller,
readOnly: readOnly == null ? false : true,
obscureText: isPassword == null ? false : true,
decoration: InputDecoration(
fillColor: colorFilled,
filled: true,
focusedBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(
color: borderSide ?? const Color(0xFF6949FF), width: 1.15),
),
enabledBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(
color: borderSide ?? const Color(0xFFCDD1E0),
width: 1.15,
),
),
labelText: labelText ?? '',
labelStyle: labelStyle ?? const TextStyle(),
// dont forget this line
hintText: hintText ?? '',
hintStyle: hintStyle ?? const TextStyle(),
helperText: helpText ?? '',
prefixIcon: null == prefixIcon ? null : Icon(prefixIcon),
suffix: null == suffixIcon ? null : Icon(suffixIcon),
enabled: null == enabled ? true : false,
),
focusNode: textFieldFocus,
textAlignVertical: TextAlignVertical.center,
validator: validateFunction,
),
);
}
}
and I am trying to implement a EmailValidator for it using this code.
class EmailValidator {
static String? validate(String? value) {
bool emailValid =
RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(value!);
// return value==null || value.isEmpty ? "Email can't be empty" : null;
if(value.isEmpty) {
return "E-mail field is empty, please type your email.";
}
if(!emailValid){
return "The email is invalid, please check carefully!";
}
return null;
}
}
and I am calling it the FormFields.textFormFieldWidget
and inside of my login_page.dart
FormFields.textFormFieldWidget(
textInputType: TextInputType.emailAddress,
leftValue: 2.w,
rightValue: 2.w,
borderColor: ColourPalette.primaryColor,
borderSide: ColourPalette.secondaryColor,
colorFilled: colorEmailField,
controller: userEmail,
textFieldFocus: userEmailFocusNode,
helpText:
"Type your email that you've registered with us!",
hintStyle: CustomStyleText.reusableTextStyle(
customColor: Colors.black.withOpacity(0.5),
customFontSize: 14,
customFontWeight: FontWeight.normal),
hintText: "Enter your email.",
labelStyle: CustomStyleText.reusableTextStyle(
customColor: Colors.black.withOpacity(0.5),
customFontSize: 14,
customFontWeight: FontWeight.normal,
),
labelText: "Email",
// here is my validate function and it does not work and it gives me
// the error
validateFunction:
EmailValidator.validate(userEmail.text)),
The validateFunction
needs a function and not a String.
Change this:
validateFunction: EmailValidator.validate(userEmail.text)
to this:
validateFunction: EmailValidator.validate