Skip to main content

MaskInputFormatter

C2CMaskInputFormatter is a Flutter TextInputFormatter used for applying custom masks to text input in a TextField. It enables formatting and validating text as per a specified mask pattern.

Usage

To use C2CMaskInputFormatter, initialize it with a mask, filter, and type, then apply it to a TextField as an input formatter.

final formatter = C2CMaskInputFormatter(
mask: "+0 (###) ###-##-##",
filter: {"#": RegExp(r'[0-9]'), "A": RegExp(r'[^0-9]')},
type: MaskAutoCompletionType.lazy,
);

TextField(
inputFormatters: [formatter],
onChanged: (text) {
// Handle formatted text
},
)

AutoCompletion Types

  • lazy (default): Autocompletes unfiltered characters once the following filtered character is input.
  • eager: Autocompletes unfiltered characters when the previous filtered character is input.

Constructor

C2CMaskInputFormatter({String? mask, Map<String, RegExp>? filter, String? initialText, this.type = MaskAutoCompletionType.lazy})

  • mask: Pattern to apply.
  • filter: Defines characters to replace in the mask and their validation.
  • initialText: Initial text to format.
  • type: Autocompletion behavior (lazy or eager).

Methods

  • updateMask({String? mask, Map<String, RegExp>? filter, TextEditingValue? newValue}): Updates the mask.
  • getMask(): Returns the current mask.
  • getMaskedText(): Returns the masked text.
  • getUnmaskedText(): Returns the unmasked text.
  • isFill(): Checks if the mask is fully filled.
  • clear(): Clears the masked text of the formatter.
  • maskText(String text): Masks a given text.
  • unmaskText(String text): Unmasks a given text.
  • formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue): Formats the text according to the mask and updates it.

Note

Call the clear method if you clear the text of the TextField since the formatter does not activate for empty text.