Search code examples
flutterdartflutter-layout

how to change color on TextFormField validator: (value) => validateCharacters(value),


I want to change this color to grey But but its red How can I achieve that or is it possible or not? code:

    TextFormField(
                  autovalidateMode: AutovalidateMode.onUserInteraction,
//Want to change color HERE
                  validator: (value) => validateCharacters(value), 
                  inputFormatters: [
                    LengthLimitingTextInputFormatter(20),
                  ],
                 
                  },
                ),

String? validateCharacters(String? value) {
  String pattern = (r'^(?=.*?)(?=.*?[a-z])(?=.*?[20]).{20,}$');
  RegExp regex = RegExp(pattern);
  if (value == null || value.isEmpty || !regex.hasMatch(value)) {
    return 'note: `Only 20 characters allow`'; //Want this to be grey color
  } else {
    return null;
  }
}

Solution

  • You can add a error style inside decoration.

    Check this:

    TextFormField(
            autovalidateMode: AutovalidateMode.onUserInteraction,
            validator: (value) => validateCharacters(value), 
            decoration: InputDecoration(
               errorStyle: TextStyle(color: Colors.grey)
            ),
            inputFormatters: [
              LengthLimitingTextInputFormatter(20),
            ],
                         
          },
         ),