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.words
TextCapitalization.characters
TextCapitalization.sentences
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
:
Result screen using TextCapitalization.characters
:
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.
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.
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,
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()}";
}
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).
The best of all of them is to use onchanged:
TextField(
controller: mytext,
onChanged: (value) {
mytext.value = TextEditingValue(
text: value.toUpperCase(),
selection: mytext.selection
);
}
)