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.
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.
Try below code hope its helpful to you. refer TextCapitalization here
TextCapitalization.wordsTextCapitalization.charactersTextCapitalization.sentencesTextCapitalization.noneYour Widget:
TextField(
textCapitalization: TextCapitalization.words,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
prefixIcon: Icon(
Icons.search,
),
hintText: 'Search',
),
),
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:
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,
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.
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.
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()}";
}