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(oldValue, TextEditingValue newValue) {
var input = newValue.text;
if (newValue.selection.baseOffset == 0) {
return newValue;
}
var bufferString = StringBuffer();
// this is to buff the valid thru using / after first 2 characters
for (int i = 0; i < input.length; i++) {
bufferString.write(input[i]);
var nonZeroIndexValue = i + 1;
if (nonZeroIndexValue % 4 == 0 && nonZeroIndexValue != input.length) {
bufferString.write(" ");
}
}
// finally return the buffed valid thru
var string = bufferString.toString();
return newValue.copyWith(
text: string,
selection: TextSelection.collapsed(offset: string.length),
);
}