TextInputFormatter class abstract
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
- TextInputFormatter()
-
This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
const
- TextInputFormatter.withFunction(TextInputFormatFunction formatFunction)
-
A shorthand to creating a custom TextInputFormatter which formats
incoming text input changes with the given function.
constfactory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
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 nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited