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 text = newValue.text;
// Validate input starts with `+` if required
if (mustStartWithPlus && !text.startsWith('+')) {
if(showToast??true){
pShowToast(message: messagePlus??'Phone number should be start with +');
}
return oldValue; // Revert to old value if it doesn't start with `+`
}
// Validate only numbers (and optional `+` if allowed)
final validPattern = mustStartWithPlus ? r'^\+[0-9]*$' : r'^\+?[0-9]*$';
final regex = RegExp(validPattern);
if (!regex.hasMatch(text)) {
if(showToast??true){
pShowToast(message: messageValidity??'Phone number should be a valid number');
}
return oldValue; // Revert if input doesn't match the pattern
}
// Enforce max length
if (text.length > maxLength) {
if(showToast??true){
pShowToast(message: messageLength??'Phone number length should be between $minLength to $maxLength');
}
return oldValue;
}
if (text.length < minLength) {
if(showToast??true){
pShowToast(message: messageLength??'Phone number length should be between $minLength to $maxLength');
}
return oldValue;
}
return newValue;
}