Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DecimalFormatter extends TextInputFormatter {
- final int decimalDigits;
- DecimalFormatter({this.decimalDigits = 2}) : assert(decimalDigits >= 0);
- @override
- TextEditingValue formatEditUpdate(
- TextEditingValue oldValue,
- TextEditingValue newValue,
- ) {
- String newText;
- if (decimalDigits == 0) {
- newText = newValue.text.replaceAll(RegExp('[^0-9]'), '');
- } else {
- newText = newValue.text.replaceAll(RegExp('[^0-9\.]'), '');
- }
- if (newText.contains('.')) {
- //in case if user's first input is "."
- if (newText.trim() == '.') {
- return newValue.copyWith(
- text: '0.',
- selection: const TextSelection.collapsed(offset: 2),
- );
- }
- //in case if user tries to input multiple "."s or tries to input
- //more than the decimal place
- else if ((newText.split(".").length > 2) ||
- (newText.split(".")[1].length > decimalDigits)) {
- return oldValue;
- } else {
- return newValue;
- }
- }
- //in case if input is empty or zero
- if (newText.trim() == '' || newText.trim() == '0') {
- return newValue.copyWith(text: '');
- } else if (int.parse(newText) < 1) {
- return newValue.copyWith(text: '');
- }
- double newDouble = double.parse(newText);
- var selectionIndexFromTheRight =
- newValue.text.length - newValue.selection.end;
- String newString = NumberFormat("#,##0.##").format(newDouble);
- // String formattedString = "$newString.00";
- return TextEditingValue(
- text: newString,
- selection: TextSelection.collapsed(
- offset: newString.length - selectionIndexFromTheRight,
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement