TextInputFormatter class Null safety
A TextInputFormatter can be optionally injected into an EditableText to provide as-you-type validation and formatting of the text being edited.
Text modification should only be applied when text is being committed by the IME and not on text under composition (i.e., only when TextEditingValue.composing is collapsed).
See also the FilteringTextInputFormatter, a subclass that removes characters that the user tries to enter if they do, or do not, match a given pattern (as applicable).
To create custom formatters, extend the TextInputFormatter class and implement the formatEditUpdate method.
Handling emojis and other complex characters
It's important to always use characters when dealing with user input text that may contain complex characters. This will ensure that extended grapheme clusters and surrogate pairs are treated as single characters, as they appear to the user.
For example, when finding the length of some user input, use
string.characters.length
. Do NOT use string.length
or even
string.runes.length
. For the complex character "👨👩👦", this
appears to the user as a single character, and string.characters.length
intuitively returns 1. On the other hand, string.length
returns 8, and
string.runes.length
returns 5!
See also:
- EditableText on which the formatting apply.
- FilteringTextInputFormatter, a provided formatter for filtering characters.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
read-onlyinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-onlyinherited
Methods
-
formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) → TextEditingValue - Called when text is being typed or cut/copy/pasted in the EditableText.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
withFunction(
TextInputFormatFunction formatFunction) → TextInputFormatter - A shorthand to creating a custom TextInputFormatter which formats incoming text input changes with the given function.