maxLength property
The maximum number of characters (Unicode scalar values) to allow in the text field.
If set, a character counter will be displayed below the field, showing how many characters have been entered and how many are allowed. After maxLength characters have been input, additional input is ignored, unless maxLengthEnforced is set to false. The TextField enforces the length with a LengthLimitingTextInputFormatter, which is evaluated after the supplied inputFormatters, if any.
This value must be either null or greater than zero. If set to null (the default), there is no limit to the number of characters allowed.
Whitespace characters (e.g. newline, space, tab) are included in the character count.
If maxLengthEnforced is set to false, then more than maxLength
characters may be entered, but the error counter and divider will
switch to the decoration.errorStyle
when the limit is exceeded.
Limitations
The TextField does not currently count Unicode grapheme clusters (i.e. characters visible to the user), it counts Unicode scalar values, which leaves out a number of useful possible characters (like many emoji and composed characters), so this will be inaccurate in the presence of those characters. If you expect to encounter these kinds of characters, be generous in the maxLength used.
For instance, the character "ö" can be represented as '\u{006F}\u{0308}', which is the letter "o" followed by a composed diaeresis "¨", or it can be represented as '\u{00F6}', which is the Unicode scalar value "LATIN SMALL LETTER O WITH DIAERESIS". In the first case, the text field will count two characters, and the second case will be counted as one character, even though the user can see no difference in the input.
Similarly, some emoji are represented by multiple scalar values. The Unicode "THUMBS UP SIGN + MEDIUM SKIN TONE MODIFIER", "👍🏽", should be counted as a single character, but because it is a combination of two Unicode scalar values, '\u{1F44D}\u{1F3FD}', it is counted as two characters.
See also:
- LengthLimitingTextInputFormatter for more information on how it counts characters, and how it may differ from the intuitive meaning.
Implementation
final int maxLength