formatEditUpdate method
Formats text input by filtering digits and managing cursor position
Called automatically by Flutter's text input system when user types.
Process:
- Extract only digits from new input
- Limit to 4 digits maximum
- Calculate appropriate cursor position
- 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),
);
}