You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
887 B
Dart
28 lines
887 B
Dart
import 'package:flutter/services.dart';
|
|
|
|
/// Tippt „HHMM“ und formatiert live zu „HH:MM“ (nur Ziffern erlaubt).
|
|
class HHmmInputFormatter extends TextInputFormatter {
|
|
const HHmmInputFormatter();
|
|
|
|
@override
|
|
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
|
|
String digits = newValue.text.replaceAll(RegExp(r'[^0-9]'), '');
|
|
if (digits.isEmpty) {
|
|
return const TextEditingValue(text: '', selection: TextSelection.collapsed(offset: 0));
|
|
}
|
|
if (digits.length > 4) digits = digits.substring(0, 4);
|
|
|
|
String text;
|
|
if (digits.length <= 2) {
|
|
text = digits;
|
|
} else {
|
|
final hh = digits.substring(0, 2);
|
|
final mm = digits.substring(2);
|
|
text = '$hh:$mm';
|
|
}
|
|
|
|
final offset = text.length;
|
|
return TextEditingValue(text: text, selection: TextSelection.collapsed(offset: offset));
|
|
}
|
|
}
|