30 lines
809 B
Dart
30 lines
809 B
Dart
import 'package:flutter/services.dart';
|
|
|
|
class FrequencyInputFormatter extends TextInputFormatter {
|
|
static const int maxDigits = 5;
|
|
|
|
@override
|
|
TextEditingValue formatEditUpdate(
|
|
TextEditingValue oldValue, TextEditingValue newValue) {
|
|
String digitsOnly = newValue.text.replaceAll(RegExp(r'[^0-9]'), '');
|
|
|
|
if (digitsOnly.length > maxDigits) {
|
|
return oldValue;
|
|
}
|
|
|
|
String formatted = digitsOnly;
|
|
if (digitsOnly.length > 3) {
|
|
formatted = '${digitsOnly.substring(0, 3)}.${digitsOnly.substring(3)}';
|
|
}
|
|
|
|
if (formatted.endsWith('.') && digitsOnly.length <= 3) {
|
|
formatted = formatted.substring(0, formatted.length - 1);
|
|
}
|
|
|
|
return TextEditingValue(
|
|
text: formatted,
|
|
selection: TextSelection.collapsed(offset: formatted.length),
|
|
);
|
|
}
|
|
}
|