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) {
  // Short-circuit if the new value is empty
  if (newValue.text.isEmpty) {
    return newValue.copyWith(text: '');
  }

  // Handle "deletion" of separator character
  String oldValueText = oldValue.text.replaceAll(separator, '');
  String newValueText = newValue.text.replaceAll(separator, '');

  if (oldValue.text.endsWith(separator) &&
      oldValue.text.length == newValue.text.length + 1) {
    newValueText = newValueText.substring(0, newValueText.length - 1);
  }

  // Only process if the old value and new value are different
  if (oldValueText != newValueText) {
    // Split the string into its integer and decimal parts
    List<String> parts = newValueText.split('.');

    int selectionIndex = newValue.text.length -
        newValue.selection
            .extentOffset; // + (parts.length > 1 ? parts[1].length : 0);
    final chars = parts[0].split('');

    String newString = '';
    for (int i = chars.length - 1; i >= 0; i--) {
      if ((chars.length - 1 - i) % 3 == 0 && i != chars.length - 1) {
        newString = separator + newString;
      }
      newString = chars[i] + newString;
    }

    return TextEditingValue(
      text: newString.toString() + (parts.length > 1 ? '.${parts[1]}' : ''),
      selection: TextSelection.collapsed(
        offset: newString.length -
            selectionIndex +
            (parts.length > 1 ? parts[1].length + 1 : 0),
      ),
    );
  }

  // If the new value and old value are the same, just return as-is
  return newValue;
}