formatEditUpdate method

  1. @override
TextEditingValue formatEditUpdate(
  1. TextEditingValue oldValue,
  2. TextEditingValue newValue
)
override

Formats text input by filtering digits and managing cursor position

Called automatically by Flutter's text input system when user types.

Process:

  1. Extract only digits from new input
  2. Limit to 4 digits maximum
  3. Calculate appropriate cursor position
  4. Return formatted TextEditingValue

Cursor Logic:

  • Counts difference in digit count between old and new values
  • Adjusts cursor position based on this difference
  • Clamps position to valid range

oldValue - Previous TextEditingValue newValue - New TextEditingValue from user input Returns filtered and formatted TextEditingValue

Implementation

@override
TextEditingValue formatEditUpdate(
  TextEditingValue oldValue,
  TextEditingValue newValue,
) {
  // Keep only digits using cached regex - use local regex to avoid conflicts
  final digitsOnly = TimeInputControllers._removeNonDigits(newValue.text);

  // Limit to 4 digits max
  final limitedText =
      digitsOnly.length > 4 ? digitsOnly.substring(0, 4) : digitsOnly;

  // Improved cursor position calculation
  final newCursorPosition =
      _calculateCorrectCursorPosition(oldValue, newValue, limitedText);

  return TextEditingValue(
    text: limitedText,
    selection: TextSelection.collapsed(offset: newCursorPosition),
  );
}