FilteringTextInputFormatter class

A TextInputFormatter that prevents the insertion of characters matching (or not matching) a particular pattern, by replacing the characters with the given replacementString.

Instances of filtered characters found in the new TextEditingValues will be replaced by the replacementString which defaults to the empty string, and the current TextEditingValue.selection and TextEditingValue.composing region will be adjusted to account for the replacement.

This formatter is typically used to match potentially recurring Patterns in the new TextEditingValue. It never completely rejects the new TextEditingValue and falls back to the current TextEditingValue when the given filterPattern fails to match. Consider using a different TextInputFormatter such as:

// _pattern is a RegExp or other Pattern object
TextInputFormatter.withFunction(
  (TextEditingValue oldValue, TextEditingValue newValue) {
    return _pattern.hasMatch(newValue.text) ? newValue : oldValue;
  },
),

for accepting/rejecting new input based on a predicate on the full string. As an example, FilteringTextInputFormatter typically shouldn't be used with RegExps that contain positional matchers (^ or $) since these patterns are usually meant for matching the whole string.

Quote characters on iOS

When filtering single (') or double (") quote characters, be aware that the default iOS keyboard actually inserts special directional versions of these characters ( and for single quote, and and for double quote). Consider including all three variants in your regular expressions to support iOS.

Inheritance

Constructors

FilteringTextInputFormatter(Pattern filterPattern, {required bool allow, String replacementString = ''})
Creates a formatter that replaces banned patterns with the given replacementString.
FilteringTextInputFormatter.allow(Pattern filterPattern, {String replacementString = ''})
Creates a formatter that only allows characters matching a pattern.
FilteringTextInputFormatter.deny(Pattern filterPattern, {String replacementString = ''})
Creates a formatter that blocks characters matching a pattern.

Properties

allow bool
Whether the pattern is an allow list or not.
final
filterPattern Pattern
A Pattern to match or replace in incoming TextEditingValues.
final
hashCode int
The hash code for this object.
no setterinherited
replacementString String
String used to replace banned patterns.
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 Properties

digitsOnly TextInputFormatter
A TextInputFormatter that takes in digits [0-9] only.
final
singleLineFormatter TextInputFormatter
A TextInputFormatter that forces input to be a single line.
final