I am new in Flutter. I am looking for TextField value to always uppercase but I did not find any resource on that.
Another issue is the TextField onChanged event debounce implementation. When I type on TextField it immediately fires onChanged event which is not suitable for my goal. The onChange event will fire after 500ms on every text changed.
new TextField(
controller: _controller,
decoration: new InputDecoration(
hintText: 'Search here',
),
onChanged: (str) {
//need to implement debounce
}
)
In Flutter, this can be done using TextEditingController . First, create a TextEditingController and set it as a controller property of your TextField widget. In this example, I have added an extra Button and Text widget which will show the added text when you click the “Show Text” button.
TextFormField( textCapitalization: TextCapitalization. words, ), This capitalize first letter of each word we type in a TextFormField.
toUpperCase() + s. substring(1). toLowerCase(); in case the string is all upper case to start with.
Works on Android, iOS, Web, macOS, Windows and Linux
You can implement a custom TextInputFormatter
class UpperCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.toUpperCase(),
selection: newValue.selection,
);
}
}
Usage:
TextField(
inputFormatters: [
UpperCaseTextFormatter(),
]
)
Full example
Perhaps using textCapitalization: TextCapitalization.characters in the TextField could help you? Although this would capitalize the characters while something is being typed as well.
TextField(
textCapitalization: TextCapitalization.sentences,
)
You can use textCapitalization
property of a TextField
widget. Also do take a reference for detailed API info here Text Capitalization Official API
Illustration as follow
Example 1
TextField(
initialValue: flutter code camp
textCapitalization: TextCapitalization.characters,
)// OUTPUT : FLUTTER CODE CAMP
Example 2
TextField(
initialValue: flutter code camp
textCapitalization: TextCapitalization.words,
)// OUTPUT : Flutter Code Camp
Example 3
TextField(
initialValue: flutter code camp
textCapitalization: TextCapitalization.sentences,
)// OUTPUT : Flutter code camp
Example 4
TextField(
initialValue: flutter code camp
textCapitalization: TextCapitalization.none,
)// OUTPUT : flutter code camp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With