Home > Software design >  How to capitialize first letter of TextFormField in flutter
How to capitialize first letter of TextFormField in flutter

Time:11-17

I'm trying to capitialize the first letter of Textformfield, for this i'm using the

textCapitalization: TextCapitalization.words,

but it's not working for Textformfield, and works for textfield

please help how to do this.

CodePudding user response:

You can try with formatter for upper case, in TextFormField you just use UpperCaseTextFormatter class in input formatters section

TextFormField(
            controller: _textEditingController,
            inputFormatters: <TextInputFormatter>[
              UpperCaseTextFormatter()
            ],
          )

Upper text formatter

class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(
      text: capitalize(newValue.text),
      selection: newValue.selection,
    );
  }
}
String capitalize(String value) {
  if(value.trim().isEmpty) return "";
  return "${value[0].toUpperCase()}${value.substring(1).toLowerCase()}";
}

output:

enter image description here

CodePudding user response:

TextFormField(
       textCapitalization: TextCapitalization.words,
       ),

This capitalize first letter of each word we type in a TextFormField.

After setting textCapitalization, try rebuilding on your emulator or device instance and check again. Let me know it works then,

CodePudding user response:

Try below code hope its helpful to you. refer TextCapitalization image output

  • Related