formatEditUpdate method

  1. @override
TextEditingValue formatEditUpdate(
  1. TextEditingValue oldValue,
  2. TextEditingValue newValue
)
override

Called when text is being typed or cut/copy/pasted in the EditableText.

You can override the resulting text based on the previous text value and the incoming new text value.

When formatters are chained, oldValue reflects the initial value of TextEditingValue at the beginning of the chain.

Implementation

@override
TextEditingValue formatEditUpdate(
    TextEditingValue oldValue, TextEditingValue newValue) {
  if (newValue.text == '') {
    return TextEditingValue(
        text: '',
        selection: TextSelection.fromPosition(TextPosition(offset: 0)));
  } else {
    int decimalDigits = _formatter.decimalDigits!;
    NumberFormat format = _formatter.format;
    String groupSep = format.symbols.GROUP_SEP;
    String decimalSep = format.symbols.DECIMAL_SEP;

    String newText = newValue.text;
    try {
      formatter.stringFromString(newText);
    } on FormatException {
      return oldValue;
    }

    if (newText.length < oldValue.text.length) {
      for (int i = 0; i < newText.length; i++) {
        if (newText[i] != oldValue.text[i] &&
            (oldValue.text[i] == groupSep ||
                oldValue.text[i] == decimalSep)) {
          newText = newText.substring(0, i - 1) + newText.substring(i);
          break;
        }
      }
    }

    newText = newText.replaceAll(groupSep, '').replaceAll(decimalSep, '');
    if (decimalDigits > 0) {
      if (newText.length > decimalDigits) {
        newText = newText.substring(0, newText.length - decimalDigits) +
            decimalSep +
            newText.substring(newText.length - decimalDigits, newText.length);
      } else {
        String newTextWithZeroes = '';
        while (newTextWithZeroes.length + newText.length < decimalDigits) {
          newTextWithZeroes += '0';
        }
        newText = '0' + decimalSep + newTextWithZeroes + newText;
      }
    }
    num parsed = format.parse(newText);
    Decimal value = Decimal.parse(parsed.toStringAsFixed(decimalDigits));
    if (zeroIsEmpty) {
      if (value == Decimal.zero) {
        return TextEditingValue(
            text: '',
            selection: TextSelection.fromPosition(TextPosition(offset: 0)));
      }
    }
    newText = format.format(parsed);

    int baseOffset = oldValue.selection.baseOffset;
    int extentOffset = oldValue.selection.extentOffset;

    if (oldValue.text.length < newText.length) {
      int diff = newText.length - oldValue.text.length;
      if (textDirection == TextDirection.ltr) {
        baseOffset += diff;
      } else {
        baseOffset -= diff;
      }
    } else if (oldValue.text.length > newText.length) {
      int diff = oldValue.text.length - newText.length;
      if (baseOffset == extentOffset) {
        if (textDirection == TextDirection.ltr) {
          baseOffset -= diff;
        } else {
          baseOffset += diff;
        }
      }
    }
    if (baseOffset > newText.length) {
      baseOffset = newText.length;
    } else if (baseOffset < 0) {
      baseOffset = 0;
    }
    return TextEditingValue(
        text: newText,
        selection:
            TextSelection.fromPosition(TextPosition(offset: baseOffset)));
  }
}