Line data Source code
1 : import 'package:flutter/foundation.dart'; 2 : import 'package:flutter/services.dart'; 3 : import 'package:folly_fields/util/decimal.dart'; 4 : import 'package:folly_fields/util/decimal_text_formatter.dart'; 5 : import 'package:folly_fields/validators/abstract_validator.dart'; 6 : 7 : /// 8 : /// 9 : /// 10 : class NewDecimalValidator extends AbstractParserValidator<Decimal> { 11 : final int precision; 12 : final String decimalSeparator; 13 : final String thousandSeparator; 14 : 15 : /// 16 : /// 17 : /// 18 1 : NewDecimalValidator( 19 : this.precision, { 20 : this.decimalSeparator = ',', 21 : this.thousandSeparator = '.', 22 : }) : assert( 23 2 : precision >= 0, 24 : 'precision must be equals or greater than zero.', 25 : ), 26 2 : super(<TextInputFormatter>[ 27 1 : DecimalTextFormatter( 28 : precision: precision, 29 : decimalSeparator: decimalSeparator, 30 : thousandSeparator: thousandSeparator, 31 : ), 32 : ]); 33 : 34 : /// 35 : /// 36 : /// 37 0 : @override 38 : TextInputType get keyboard => TextInputType.number; 39 : 40 : /// 41 : /// 42 : /// 43 0 : @override 44 : String strip(String value) { 45 : if (kDebugMode) { 46 0 : print('Decimal validator - call strip method.'); 47 : } 48 : 49 : return value; 50 : } 51 : 52 : /// 53 : /// 54 : /// 55 2 : String _internalStrip(String value) => super.strip(value); 56 : 57 : /// 58 : /// 59 : /// 60 1 : @override 61 : String format(Decimal decimal) { 62 1 : List<String> parts = decimal.doubleValue 63 2 : .toStringAsFixed(precision) 64 1 : .replaceAll('.', '') 65 1 : .split('') 66 1 : .reversed 67 1 : .toList(); 68 : 69 2 : int start = precision + 4; 70 2 : if (precision > 0) { 71 3 : parts.insert(precision, decimalSeparator); 72 : } else { 73 : start = 3; 74 : } 75 : 76 3 : for (int pos = start; parts.length > pos; pos += 4) { 77 2 : parts.insert(pos, thousandSeparator); 78 : } 79 : 80 2 : return parts.reversed.join(); 81 : } 82 : 83 : /// 84 : /// 85 : /// 86 0 : @override 87 0 : bool isValid(String decimal) => valid(decimal) == null; 88 : 89 : /// 90 : /// 91 : /// 92 1 : @override 93 : Decimal? parse(String? value) { 94 2 : Decimal decimal = Decimal(precision: precision); 95 : 96 1 : if (value == null || value.isEmpty) { 97 : return decimal; 98 : } 99 : 100 2 : int sepPos = value.indexOf(decimalSeparator); 101 : 102 : String integerPart = '0'; 103 : String decimalPart = '0'; 104 : 105 1 : if (sepPos < 0) { 106 : integerPart = value; 107 1 : } else if (sepPos == 0) { 108 1 : decimalPart = _internalStrip(value); 109 : } else { 110 2 : integerPart = _internalStrip(value.substring(0, sepPos)); 111 2 : decimalPart = _internalStrip(value.substring(sepPos)); 112 : } 113 : 114 3 : if (decimalPart.length > precision) { 115 2 : decimalPart = decimalPart.substring(0, precision); 116 : } 117 : 118 1 : String str = '$integerPart.$decimalPart'; 119 : 120 1 : double? dbl = double.tryParse(str); 121 : 122 : if (dbl == null) { 123 : if (kDebugMode) { 124 0 : print('Error to parse $str'); 125 : } 126 : dbl = 0; 127 : } 128 : 129 1 : decimal.doubleValue = dbl; 130 : 131 : return decimal; 132 : } 133 : 134 : /// 135 : /// 136 : /// 137 0 : @override 138 : String? valid(String value) => null; 139 : }