Home > Software engineering >  try To make a Input Text Field in flutter But I can't Make a validator function that send as re
try To make a Input Text Field in flutter But I can't Make a validator function that send as re

Time:10-28

That is a Component File and i need help with that:

Widget defaultTextFormField({
  required TextEditingController controller,
  required TextInputType keyboardtype,
  required Function () submitFunction,
  required Function validator,
  required IconData prefix,
  ValueChanged<String>?  onchange,
  String labelText =  'Enter Text ...',

}) => TextFormField(
  controller: controller,
  keyboardType: keyboardtype,
  decoration: InputDecoration(
    labelText: labelText,
    prefixIcon: prefix,
    border: OutlineInputBorder(),
  ),
  onFieldSubmitted: submitFunction(),
  onChanged: onchange,
  validator: validator(),
);

Try to fix my code problem . expected to find help with that.

CodePudding user response:

The validator has been defined like

typedef FormFieldValidator<T> = String? Function(T? value);

It should return nullable data and provide value on callback. You can use

required FormFieldValidator validator,

It will be same as required String Function(String?) validator,

and use like validator: validator,

Widget defaultTextFormField({
  required TextEditingController controller,
  required TextInputType keyboardtype,
  required Function(String?) submitFunction,
  required FormFieldValidator validator,
  required Widget prefix,
  ValueChanged<String>? onchange,
  String labelText = 'Enter Text ...',
}) =>
    TextFormField(
      controller: controller,
      keyboardType: keyboardtype,
      decoration: InputDecoration(
        labelText: labelText,
        prefixIcon: prefix,
        border: OutlineInputBorder(),
      ),
      onFieldSubmitted: submitFunction(),
      onChanged: onchange,
      validator: validator,
    );
  • Related