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,
) {
  final trailingLength = _getTrailingLength();
  final leadingLength = _getLeadingLength();
  int oldCaretIndex = max(oldValue.selection.start, oldValue.selection.end);
  int newCaretIndex = max(newValue.selection.start, newValue.selection.end);
  var newText = newValue.text;
  final newAsNumeric = toNumericString(
    newText,
    allowPeriod: true,
    mantissaSeparator: _mantissaSeparator,
    mantissaLength: mantissaLength,
  );
  _updateValue(newAsNumeric);

  var oldText = oldValue.text;
  if (oldValue == newValue) {
    if (_printDebugInfo) {
      print('RETURN 0 ${oldValue.text}');
    }
    return newValue;
  }
  bool isErasing = newText.length < oldText.length;
  if (isErasing) {
    if (mantissaLength == 0 && oldCaretIndex == oldValue.text.length) {
      if (trailingLength > 0) {
        if (_printDebugInfo) {
          print('RETURN 1 ${oldValue.text}');
        }
        return oldValue.copyWith(
          selection: TextSelection.collapsed(
            offset: min(
              oldValue.text.length,
              oldCaretIndex - trailingLength,
            ),
          ),
        );
      }
    } else {
      if (thousandSeparator == ThousandSeparator.Space) {
        /// It's a dirty hack to try and fix this issue
        /// https://github.com/caseyryan/flutter_multi_formatter/issues/145
        /// The problem there is that after erasing just a white space
        /// the number from e.g. this 45 555 $ becomes this 45555 $ but
        /// after applying the format again in regains the lost space and
        /// this leads to a situation when nothing seems to be changed
        final differences = _findDifferentChars(
          longerString: oldText,
          shorterString: newText,
        );
        if (differences.length == 1 && differences.first == ' ') {
          if (newCaretIndex > 0) {
            newCaretIndex = newCaretIndex.subtractClamping(1);
            oldCaretIndex = oldCaretIndex.subtractClamping(1);
            newText = newText.removeCharAt(newCaretIndex);
          }
        }
      }
    }
    if (_hasErasedMantissaSeparator(
      shorterString: newText,
      longerString: oldText,
    )) {
      if (_printDebugInfo) {
        print('RETURN 2 ${oldValue.text}');
      }
      return oldValue.copyWith(
        selection: TextSelection.collapsed(
          offset: min(
            oldValue.text.length,
            oldCaretIndex - 1,
          ),
        ),
      );
    }
  } else {
    if (_containsIllegalChars(newText)) {
      if (_printDebugInfo) {
        print('RETURN 3 ${oldValue.text}');
      }
      return oldValue;
    }
  }

  int afterMantissaPosition = _countAfterMantissaPosition(
    oldText: oldText,
    oldCaretOffset: oldCaretIndex,
  );

  final newAsCurrency = toCurrencyString(
    newText,
    mantissaLength: mantissaLength,
    thousandSeparator: thousandSeparator,
    leadingSymbol: leadingSymbol,
    trailingSymbol: trailingSymbol,
    useSymbolPadding: useSymbolPadding,
  );

  if (_switchToRightInWholePart(
    newText: newText,
    oldText: oldText,
  )) {
    if (_printDebugInfo) {
      print('RETURN 4 ${oldValue.text.length} $oldCaretIndex');
    }
    return oldValue.copyWith(
      selection: TextSelection.collapsed(
        offset: min(
          oldValue.text.length,
          oldCaretIndex + 1,
        ),
      ),
    );
  }

  if (afterMantissaPosition > 0) {
    if (_switchToLeftInMantissa(
      newText: newText,
      oldText: oldText,
      caretPosition: newCaretIndex,
    )) {
      if (_printDebugInfo) {
        print('RETURN 5 $newAsCurrency');
      }
      return TextEditingValue(
        selection: TextSelection.collapsed(
          offset: newCaretIndex,
        ),
        text: newAsCurrency,
      );
    } else {
      if (_printDebugInfo) {
        print('RETURN 6 $newAsCurrency');
      }
      int offset = min(
        newCaretIndex,
        newAsCurrency.length - trailingLength,
      );
      return TextEditingValue(
        selection: TextSelection.collapsed(
          offset: offset,
        ),
        text: newAsCurrency,
      );
    }
  }

  var initialCaretOffset = leadingLength;
  if (_isZeroOrEmpty(newAsNumeric)) {
    if (_printDebugInfo) {
      print('RETURN 7 ${newValue.text}');
    }
    int offset = min(
      newValue.text.length,
      initialCaretOffset + 1,
    );
    if (newValue.text == '') {
      offset = 1;
    }
    return newValue.copyWith(
      text: newAsCurrency,
      selection: TextSelection.collapsed(
        offset: offset,
      ),
    );
  }
  final oldAsCurrency = toCurrencyString(
    oldText,
    mantissaLength: mantissaLength,
    thousandSeparator: thousandSeparator,
    leadingSymbol: leadingSymbol,
    trailingSymbol: trailingSymbol,
    useSymbolPadding: useSymbolPadding,
  );

  var lengthDiff = newAsCurrency.length - oldAsCurrency.length;

  initialCaretOffset = max(
    (oldCaretIndex + lengthDiff),
    leadingLength + 1,
  );
  if (initialCaretOffset < 1) {
    if (newAsCurrency.isNotEmpty) {
      initialCaretOffset += 1;
    }
  }

  if (_printDebugInfo) {
    print('RETURN 8 $newAsCurrency');
  }
  return TextEditingValue(
    selection: TextSelection.collapsed(
      offset: initialCaretOffset,
    ),
    text: newAsCurrency,
  );
}