first commit
This commit is contained in:
23
lib/utils/text_field_4_space_formatter.dart
Normal file
23
lib/utils/text_field_4_space_formatter.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class TextField4SpaceFormatter extends TextInputFormatter {
|
||||
@override
|
||||
TextEditingValue formatEditUpdate(
|
||||
TextEditingValue oldValue,
|
||||
TextEditingValue newValue,
|
||||
) {
|
||||
final text = newValue.text.replaceAll(' ', '');
|
||||
final buffer = StringBuffer();
|
||||
for (int i = 0; i < text.length; i++) {
|
||||
buffer.write(text[i]);
|
||||
if ((i + 1) % 4 == 0 && i + 1 != text.length) {
|
||||
buffer.write(' ');
|
||||
}
|
||||
}
|
||||
final formattedText = buffer.toString();
|
||||
return newValue.copyWith(
|
||||
text: formattedText,
|
||||
selection: TextSelection.collapsed(offset: formattedText.length),
|
||||
);
|
||||
}
|
||||
}
|
23
lib/utils/text_field_date_formatter.dart
Normal file
23
lib/utils/text_field_date_formatter.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class TextFieldDateFormatter extends TextInputFormatter {
|
||||
@override
|
||||
TextEditingValue formatEditUpdate(
|
||||
TextEditingValue oldValue,
|
||||
TextEditingValue newValue,
|
||||
) {
|
||||
final text = newValue.text.replaceAll('/', '');
|
||||
final buffer = StringBuffer();
|
||||
for (int i = 0; i < text.length; i++) {
|
||||
buffer.write(text[i]);
|
||||
if (i == 1 && i + 1 != text.length) {
|
||||
buffer.write('/');
|
||||
}
|
||||
}
|
||||
final formattedText = buffer.toString();
|
||||
return newValue.copyWith(
|
||||
text: formattedText,
|
||||
selection: TextSelection.collapsed(offset: formattedText.length),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user