StringDotLengthLimiterTextInputFormatter class
StringDotLengthLimiterTextInputFormatter is a TextInputFormatter that limits text by String.length, not user-perceived characters (grapheme clusters).
The important detail is that it counts with String.length, but when it cuts text,
it walks user-visible characters (with the characters package), so it does not
split an emoji or combined character in half.
Use StringDotLengthLimiterTextInputFormatter when the allowed length of some text is based on Dart's String.length, such as when a server, database, or file format has that same limit.
This is different from Flutter's native LengthLimitingTextInputFormatter,
which counts user-visible characters using the characters package, not
String.length. Because of that, Flutter's formatter may result in text that
does not really fit the limit of String.length, and may even allow text that
exceeds that limit. For example, if the limit is 10, Flutter's formatter may
allow 10 emojis, which can be more than 10 characters in String.length.
This can cause issues if you are trying to enforce a limit based on String.length,
such as when a server or database has that same limit.
Note, when using StringDotLengthLimiterTextInputFormatter you should not show the normal character count to the user in the UI, because what the user sees is not the real limit.
- Inheritance
-
- Object
- TextInputFormatter
- StringDotLengthLimiterTextInputFormatter
Constructors
- StringDotLengthLimiterTextInputFormatter(int? maxLength)
-
Creates a formatter that prevents the insertion of more characters than a
limit. The
maxLengthmust be null, -1 or greater than zero. If it is null or -1, then no limit is enforced.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- maxLength → int?
-
The limit on the number of String.length characters that this formatter
will allow. The value must be null or greater than zero. If it is null or -1,
then no limit is enforced.
final
- 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.
override
-
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
Static Methods
-
truncate(
TextEditingValue value, int maxLength) → TextEditingValue - Truncate the given TextEditingValue to maxLength using String.length (not user-perceived characters).
-
truncateString(
String value, int maxLength) → String -
Truncate the given String to
maxLengthusing String.length (not user-perceived characters).