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,
) {
  // ignore: parameter_assignments
  newValue = newValue.copyWith(
    text: newValue.text.replaceAll(RegExp(r'[^A-Z,a-z,0-9]'), ""),
  );

  if (bankCode == 001 || bankCode == 237) {
    if (bankCode == 001) {
      if (newValue.text.contains(RegExp('[A-W, Y-Z, a-w, y-z]'))) {
        return oldValue;
      }
    } else {
      if (newValue.text.contains(RegExp('[A-O, Q-Z, a-o, q-z]'))) {
        return oldValue;
      }
    }

    if (newValue.text.length > 1) {
      final formtedOldValue =
          oldValue.text.replaceAll(RegExp(r'[^A-Z,a-z,0-9]'), "");
      if (newValue.text.length > formtedOldValue.length) {
        // ignore: parameter_assignments
        newValue = newValue.copyWith(
          text: newValue.text.replaceFirst(RegExp('[0-9, A-Z, a-z]'), ""),
        );
      }
    }
    final List<String> accountTextList = newValue.text
        .replaceAll(" ", "")
        .replaceAll("-", "")
        .replaceAll(".", "")
        .split('')
        .reversed
        .toList();

    for (var i = accountTextList.length; i < _bankBranchLength; i++) {
      accountTextList.add('0');
    }

    accountTextList.insert(1, "-");
    return newValue.copyWith(
      text: accountTextList.reversed.join(),
      selection: TextSelection.fromPosition(
        TextPosition(offset: accountTextList.length),
      ),
    );
  } else {
    if (_bankBranchLength != 0) {
      final List<String> accountTextList = newValue.text
          .replaceAll(" ", "")
          .replaceAll("-", "")
          .replaceAll(".", "")
          .split('')
          .reversed
          .toList();

      if (newValue.text.length > 1) {
        accountTextList.removeLast();
      }

      for (var i = accountTextList.length; i < _bankBranchLength; i++) {
        accountTextList.add('0');
      }

      if (newValue.text.contains(RegExp('[A-Z, a-z]'))) {
        return oldValue;
      }

      return newValue.copyWith(
        text: accountTextList.reversed.join(),
        selection: TextSelection.fromPosition(
          TextPosition(offset: accountTextList.length),
        ),
      );
    }
    return newValue;
  }
}