Force uppercase TextFormField Flutter

Viewed 1194

How can I force the TextFormField to only have uppercase letters ? textCapitalization: TextCapitalization.characters lets the user switch back to lowercase, so it isn't sufficient for what I want.

2 Answers

add textInputFormatter to text field

TextField(inputFormatters: [UpperCaseTextFormatter()]),

formatter class

class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(text: newValue.text.toUpperCase(), selection: newValue.selection);
  }
}

Just add this to your TextFormField

inputFormatters: [UpperCaseTextFormatter()],
 textCapitalization: TextCapitalization.characters,

Full code

TextFormField(
      autofillHints: [AutofillHints.name],
      inputFormatters: [UpperCaseTextFormatter()],
      textCapitalization: TextCapitalization.characters,
      textInputAction: TextInputAction.next,
      style: TextStyle(fontSize: 20),
      decoration: InputDecoration(
      contentPadding: EdgeInsets.symmetric(
      horizontal: 0, vertical: 0),
      labelText: "Hello World",
      hintText: '',
    ),
 ),
Related