forked from bhrott/flutter-masked-text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflutter_masked_text.dart
More file actions
278 lines (222 loc) · 6.12 KB
/
flutter_masked_text.dart
File metadata and controls
278 lines (222 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
library custom_flutter_masked_text;
import 'package:flutter/material.dart';
class MaskedTextController extends TextEditingController {
MaskedTextController({
required String text,
required this.mask,
Map<String, RegExp>? translator,
}) : super(text: text) {
translator = translator ?? MaskedTextController.getDefaultTranslator();
addListener(() {
var previous = _lastUpdatedText;
if (beforeChange(previous, text)) {
updateText(text);
afterChange(previous, text);
} else {
updateText(_lastUpdatedText);
}
});
updateText(this.text);
}
String mask;
Map<String, RegExp>? translator;
void afterChange(
String? previous,
String next,
) {}
bool beforeChange(
String? previous,
String next,
) {
return true;
}
String get rawText {
String localMask = mask;
String localText = text;
for (int i = 0; i < localMask.length; i++) {
var aux = localMask.substring(i, i + 1);
if ((localMask.substring(i, i + 1) != '0') &&
(localMask.substring(i, i + 1) != 'A') &&
(localMask.substring(i, i + 1) != '@') &&
(localMask.substring(i, i + 1) != '*')) {
localText = localText.replaceFirst(aux, '|');
}
}
return localText.replaceAll('|', '');
}
String get rawNumberValue {
RegExp exp = RegExp(r'[^0-9]');
return text.replaceAll(exp, '');
}
String get numberValue {
String localMask = mask;
for (int i = 0; i < localMask.length; i++) {
var aux = localMask.substring(i, i + 1);
if ((localMask.substring(i, i + 1) != '0') &&
(localMask.substring(i, i + 1) != 'A') &&
(localMask.substring(i, i + 1) != '@') &&
(localMask.substring(i, i + 1) != '*')) {
localMask = localMask.replaceFirst(aux, '0');
}
}
return _applyMask(localMask, text);
}
String? _lastUpdatedText = '';
void updateText(String? text) {
if (text != null) {
text = _applyMask(mask, text);
} else {
text = '';
}
_lastUpdatedText = text;
}
void updateMask(
String mask, {
bool moveCursorToEnd = true,
}) {
mask = mask;
updateText(text);
if (moveCursorToEnd) {
this.moveCursorToEnd();
}
}
void moveCursorToEnd() {
String? text = _lastUpdatedText;
selection = TextSelection.fromPosition(
TextPosition(offset: (text ?? '').length),
);
}
@override
set text(String text) {
if (super.text != text) {
super.text = text;
moveCursorToEnd();
}
}
static Map<String, RegExp> getDefaultTranslator() {
return {
'A': RegExp(r'[A-Za-z]'),
'0': RegExp(r'[0-9]'),
'@': RegExp(r'[A-Za-z0-9]'),
'*': RegExp(r'.*'),
};
}
String _applyMask(String mask, String value) {
String result = '';
var maskCharIndex = 0;
var valueCharIndex = 0;
while (true) {
// if mask is ended, break.
if (maskCharIndex == mask.length) {
break;
}
// if value is ended, break.
if (valueCharIndex == value.length) {
break;
}
var maskChar = mask[maskCharIndex];
var valueChar = value[valueCharIndex];
// value equals mask, just set
if (maskChar == valueChar) {
result += maskChar;
valueCharIndex += 1;
maskCharIndex += 1;
continue;
}
// apply translator if match
if (translator!.containsKey(maskChar)) {
if (translator![maskChar]!.hasMatch(valueChar)) {
result += valueChar;
maskCharIndex += 1;
}
valueCharIndex += 1;
continue;
}
// not masked value, fixed char on mask
result += maskChar;
maskCharIndex += 1;
continue;
}
return result;
}
}
//
// Mask for monetary values.
//
class CustomMoneyMaskedTextController extends TextEditingController {
CustomMoneyMaskedTextController({
double initialValue = 0.0,
this.decimalSeparator = ',',
this.thousandSeparator = '.',
this.rightSymbol = '',
this.leftSymbol = '',
this.precision = 2,
}) {
_validateConfig();
addListener(() {
updateValue(numberValue);
afterChange(text, numberValue);
});
updateValue(initialValue);
}
final String decimalSeparator;
final String thousandSeparator;
final String rightSymbol;
final String leftSymbol;
final int precision;
void afterChange(
String maskedValue,
double rawValue,
) {}
double _lastValue = 0.0;
void updateValue(double value) {
double valueToUse = value;
if (value.toStringAsFixed(0).length > 12) {
valueToUse = _lastValue;
} else {
_lastValue = value;
}
String masked = _applyMask(valueToUse);
if (rightSymbol.isNotEmpty) {
masked += rightSymbol;
}
if (leftSymbol.isNotEmpty) {
masked = leftSymbol + masked;
}
if (masked != text) {
text = masked;
var cursorPosition = super.text.length - rightSymbol.length;
selection = TextSelection.fromPosition(TextPosition(offset: cursorPosition));
}
}
double get numberValue {
List<String> parts = _getOnlyNumbers(text).split('').toList(growable: true);
parts.insert(parts.length - precision, '.');
return double.parse(parts.join());
}
_validateConfig() {
bool rightSymbolHasNumbers = _getOnlyNumbers(rightSymbol).isNotEmpty;
if (rightSymbolHasNumbers) {
throw ArgumentError('rightSymbol must not have numbers.');
}
}
String _getOnlyNumbers(String text) {
String cleanedText = text;
var onlyNumbersRegex = RegExp(r'[^\d]');
cleanedText = cleanedText.replaceAll(onlyNumbersRegex, '');
return cleanedText;
}
String _applyMask(double value) {
List<String> textRepresentation =
value.toStringAsFixed(precision).replaceAll('.', '').split('').reversed.toList(growable: true);
textRepresentation.insert(precision, decimalSeparator);
for (var i = precision + 4; true; i = i + 4) {
if (textRepresentation.length > i) {
textRepresentation.insert(i, thousandSeparator);
} else {
break;
}
}
return textRepresentation.reversed.join('');
}
}