MaskedTextController constructor

MaskedTextController({
  1. required String mask,
  2. BeforeChangeCallback? beforeChange,
  3. AfterChangeCallback? afterChange,
  4. CursorBehaviour cursorBehavior = CursorBehaviour.unlocked,
  5. String? text,
  6. Map<String, RegExp>? translator,
})

Creates a controller for an editable text field.

This constructor treats a null text argument as if it were the empty string.

The translator is nullable and provides a way to customize the way the controller applies the mask, usefull when the expected input has a character from the mask.

The mask argument must follow the rules defined in the translator.

The beforeChange and afterChange callbacks are usefull to compare changes in the text.

The cursorBehavior parameter allows different behaviors for the cursor, we recommend the usage of the default behavior, the other ones have bugs that can be annoying in some cases.

Implementation

MaskedTextController({
  required this.mask,
  this.beforeChange,
  this.afterChange,
  this.cursorBehavior = CursorBehaviour.unlocked,
  String? text,
  Map<String, RegExp>? translator,
}) : super(text: text) {
  this.translator = translator ?? MaskedTextController.getDefaultTranslator();

  // Initialize the beforeChange and afterChange callbacks if they are null
  beforeChange ??= (previous, next) => true;
  afterChange ??= (previous, next) {};

  addListener(_listener);
  _lastCursor = this.text.length;
  updateText(this.text);
}