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,
) {
  // Regular expression that matches only alphanumeric characters
  final RegExp regExp = RegExp(r'^[a-zA-Z0-9]*$');
  // If the new value matches the alphanumeric pattern, return it
  return regExp.hasMatch(newValue.text) ? newValue : oldValue;
}