formatEditUpdate method
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 FormattedValue oldFormattedValue = applyMask(
oldValue.text,
);
final FormattedValue newFormattedValue = applyMask(
newValue.text,
);
var numSeparatorsInNew = 0;
var numSeparatorsInOld = 0;
/// it's only used in CreditCardExpirationDateFormatter
/// when it adds a leading zero to the input
var addOffset = newFormattedValue._numLeadingSymbols;
numSeparatorsInOld = _countSeparators(
oldFormattedValue.text,
);
numSeparatorsInNew = _countSeparators(
newFormattedValue.text,
);
var separatorsDiff = (numSeparatorsInNew - numSeparatorsInOld);
if (newFormattedValue._isErasing) {
separatorsDiff = 0;
}
var selectionOffset = newValue.selection.end + separatorsDiff;
_maskedValue = newFormattedValue.text;
if (selectionOffset > _maskedValue.length) {
selectionOffset = _maskedValue.length;
}
return TextEditingValue(
text: _maskedValue,
selection: TextSelection.collapsed(
offset: selectionOffset + addOffset,
affinity: TextAffinity.upstream,
),
);
}