MaskFunctionTextInputFormatter constructor

MaskFunctionTextInputFormatter({
  1. required MaskFunction maskFunction,
  2. Map<String, RegExp>? filter,
  3. String? initialText,
})

Creates an TextInputFormatter based on the maskFunction formatter, for using in TextFields.

The keys of the filter assign which character in the mask should be replaced, and the values validate the entered character. By default # matches a number, and A matches a letter.

Implementation

MaskFunctionTextInputFormatter({
  required this.maskFunction,
  Map<String, RegExp>? filter,
  String? initialText,
}) {
  //
  var mask = _getMask(
    oldValue: const TextEditingValue(
      selection: TextSelection.collapsed(offset: 0),
    ),
    newValue: const TextEditingValue(
      selection: TextSelection.collapsed(offset: 0),
    ),
  );

  _updateMask(
    mask: mask,
    filter: filter ?? {"#": RegExp('[0-9]'), "A": RegExp('[^0-9]')},
  );

  if (initialText != null) {
    formatEditUpdate(
      const TextEditingValue(),
      TextEditingValue(text: initialText),
    );
  }
}