formatEditUpdate method
- TextEditingValue oldValue,
- 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,
) {
String newTextValue = newValue.text;
// Replace all invalid separators
newTextValue = replaceSeparator(newTextValue);
// If there is more than one separator, move to the new one
final MoveSeparatorResult separatorResult = moveSeparator(
oldValue.text,
newTextValue,
newValue.selection,
);
final int newTextLength = separatorResult.newText.length;
return newValue.copyWith(
text: separatorResult.newText,
selection: newValue.selection.copyWith(
baseOffset: math.min(separatorResult.newBasePosition, newTextLength),
extentOffset: math.min(
separatorResult.newExtentPosition,
newTextLength,
),
),
);
}