24

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.

TimeToCode
  • 1,458
  • 3
  • 18
  • 60

9 Answers9

37

Try below code hope its helpful to you. refer TextCapitalization Here

  • Capital first letter TextCapitalization.words
  • Capital each letter TextCapitalization.characters
  • Capital first letter of textfield TextCapitalization.sentences
  • Default lowercase letter of textfield TextCapitalization.none

Your Widget:

   TextField(
      textCapitalization: TextCapitalization.words,
      decoration: InputDecoration(
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        prefixIcon: Icon(
          Icons.search,
        ),
        hintText: 'Search',
      ),
    ),

Result screen using TextCapitalization.words: image output

Result screen using TextCapitalization.characters: image

Result screen using TextCapitalization.sentences: images

Result screen using TextCapitalization.none: none image

Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
11

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

Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38
9
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,

6

To make it work with TextFormField, you must add

textCapitalization: TextCapitalization.sentences // Capital first letter
keyboardType: TextInputType.text, 

for instance with keyboardType: TextInputType.name, it won't work.

debovitch
  • 76
  • 1
  • 3
1

Just compliting Safeela Nasarin answer, which works fine.

Simply make sure to use the embedded emulator keyboard, or the device embedded keyboard depending on your case. It won't work with external or computer keyboard.

1

If you have a TextFormField instead of a TextField, use:

textCapitalization: TextCapitalization.sentences

This will capitalise the first letter. Use it with:

keyboardType: TextInputType.text,  
reeco
  • 47
  • 9
0

Jahidul Islam answer didn't work for me because I need to capitalize all word in a sentence. The textCapitalization property didn't make the job because it just sugested to the user by auto capitalize the keyboard and my requirements were that the input HAS to be word capitalized.

So I need to implemented it be myself, using the base TextEditingValue provided by Jahidul. This CHANGES the input, so the user can't violate our requirements to this field.

Hope it help others.

enter image description here

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

String capitalizeAllWordsInFullSentence(String str) {
  int i;
  String constructedString = "";
  for (i = 0; i < str.length; i++) {
    if (i == 0) {
      constructedString += str[0].toUpperCase();
    } else if (str[i - 1] == ' ') {
      // mandatory to have index>1 !
      constructedString += str[i].toUpperCase();
    } else {
      constructedString += str[i];
    }
  }
  // print('constructed: $constructedString');
  return constructedString;
}

String capitalize1Word(String value) {
  if (value.trim().isEmpty) return "";
  return "${value[0].toUpperCase()}${value.substring(1).toLowerCase()}";
}
MarcoFerreira
  • 195
  • 1
  • 14
0

I was having the same issue TextCapitalization.sentences or TextCapitalization.words didn't work for me because of keyboardType: TextInputType.name. When I changed it to keyboardType: TextInputType.text then textCapitalization started to work in both (sentences & words).

Siddharth Mehra
  • 1,691
  • 1
  • 9
  • 32
0

The best of all of them is to use onchanged:

TextField(
controller: mytext,
onChanged: (value) {               
  mytext.value = TextEditingValue(
      text: value.toUpperCase(), 
      selection: mytext.selection
  );
}
)
Muhammad Ullah
  • 263
  • 2
  • 7