Skip to content

Commit d92f160

Browse files
committed
refactor: rename internal variables and improve code clarity across formatters and validators.
1 parent faae206 commit d92f160

13 files changed

Lines changed: 123 additions & 123 deletions

lib/src/formatters/altura_input_formatter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AlturaInputFormatter extends TextInputFormatter {
2626
}
2727

2828
return TextEditingValue(
29-
text: valorFinal.toString(),
29+
text: valorFinal,
3030
selection: TextSelection.collapsed(offset: valorFinal.length),
3131
);
3232
}

lib/src/formatters/centavos_input_formatter.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CentavosInputFormatter extends TextInputFormatter {
1818
TextEditingValue oldValue, TextEditingValue newValue) {
1919
if (newValue.text.isEmpty || newValue.text.length > 12) return oldValue;
2020

21-
final newText = StringBuffer();
21+
final valorFinal = StringBuffer();
2222
var centavos = "";
2323
var textoFinal = newValue.text;
2424
var reais = int.parse(newValue.text);
@@ -46,11 +46,11 @@ class CentavosInputFormatter extends TextInputFormatter {
4646
if (moeda) {
4747
textoFinal = 'R\$ $textoFinal';
4848
}
49-
newText.write(textoFinal);
49+
valorFinal.write(textoFinal);
5050

5151
return TextEditingValue(
52-
text: newText.toString(),
53-
selection: TextSelection.collapsed(offset: newText.length),
52+
text: valorFinal.toString(),
53+
selection: TextSelection.collapsed(offset: valorFinal.length),
5454
);
5555
}
5656

@@ -86,11 +86,11 @@ class CentavosInputFormatter extends TextInputFormatter {
8686
if (moeda) {
8787
textoFinal = 'R\$ $textoFinal';
8888
}
89-
newText.write(textoFinal);
89+
valorFinal.write(textoFinal);
9090

9191
return TextEditingValue(
92-
text: newText.toString(),
93-
selection: TextSelection.collapsed(offset: newText.length),
92+
text: valorFinal.toString(),
93+
selection: TextSelection.collapsed(offset: valorFinal.length),
9494
);
9595
}
9696
}
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
import 'package:flutter/services.dart';
2-
3-
/// Formata o valor do campo com a máscara CEST `XX.XXX.XX`.
4-
class CESTInputFormatter extends TextInputFormatter {
5-
@override
6-
TextEditingValue formatEditUpdate(
7-
TextEditingValue oldValue, TextEditingValue newValue) {
8-
// verifica o tamanho máximo do campo
9-
if (newValue.text.length > 7) return oldValue;
10-
11-
var posicaoCursor = newValue.selection.end;
12-
var substrIndex = 0;
13-
final valorFinal = StringBuffer();
14-
15-
if (newValue.text.length >= 3) {
16-
valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}.');
17-
if (newValue.selection.end >= 2) posicaoCursor++;
18-
}
19-
20-
if (newValue.text.length >= 6) {
21-
valorFinal.write('${newValue.text.substring(2, substrIndex = 5)}.');
22-
if (newValue.selection.end >= 5) posicaoCursor++;
23-
}
24-
25-
if (newValue.text.length >= substrIndex) {
26-
valorFinal.write(newValue.text.substring(substrIndex));
27-
}
28-
29-
return TextEditingValue(
30-
text: valorFinal.toString(),
31-
selection: TextSelection.collapsed(offset: posicaoCursor),
32-
);
33-
}
34-
}
1+
import 'package:flutter/services.dart';
2+
3+
/// Formata o valor do campo com a máscara CEST `XX.XXX.XX`.
4+
class CESTInputFormatter extends TextInputFormatter {
5+
@override
6+
TextEditingValue formatEditUpdate(
7+
TextEditingValue oldValue, TextEditingValue newValue) {
8+
// verifica o tamanho máximo do campo
9+
if (newValue.text.length > 7) return oldValue;
10+
11+
var posicaoCursor = newValue.selection.end;
12+
var substrIndex = 0;
13+
final valorFinal = StringBuffer();
14+
15+
if (newValue.text.length >= 3) {
16+
valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}.');
17+
if (newValue.selection.end >= 2) posicaoCursor++;
18+
}
19+
20+
if (newValue.text.length >= 6) {
21+
valorFinal.write('${newValue.text.substring(2, substrIndex = 5)}.');
22+
if (newValue.selection.end >= 5) posicaoCursor++;
23+
}
24+
25+
if (newValue.text.length >= substrIndex) {
26+
valorFinal.write(newValue.text.substring(substrIndex));
27+
}
28+
29+
return TextEditingValue(
30+
text: valorFinal.toString(),
31+
selection: TextSelection.collapsed(offset: posicaoCursor),
32+
);
33+
}
34+
}

lib/src/formatters/cnpj_alfanumerico_input_formatter.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@ class CnpjAlfanumericoInputFormatter extends TextInputFormatter
2525

2626
if (newValueLength > maxLength) return oldValue;
2727

28-
var selectionIndex = newValue.selection.end;
28+
var posicaoCursor = newValue.selection.end;
2929
var substrIndex = 0;
30-
final newText = StringBuffer();
30+
final valorFinal = StringBuffer();
3131

3232
if (newValueLength >= 3) {
33-
newText.write('${newValue.text.substring(0, substrIndex = 2)}.');
34-
if (newValue.selection.end >= 2) selectionIndex++;
33+
valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}.');
34+
if (newValue.selection.end >= 2) posicaoCursor++;
3535
}
3636
if (newValueLength >= 6) {
37-
newText.write('${newValue.text.substring(2, substrIndex = 5)}.');
38-
if (newValue.selection.end >= 5) selectionIndex++;
37+
valorFinal.write('${newValue.text.substring(2, substrIndex = 5)}.');
38+
if (newValue.selection.end >= 5) posicaoCursor++;
3939
}
4040
if (newValueLength >= 9) {
41-
newText.write('${newValue.text.substring(5, substrIndex = 8)}/');
42-
if (newValue.selection.end >= 8) selectionIndex++;
41+
valorFinal.write('${newValue.text.substring(5, substrIndex = 8)}/');
42+
if (newValue.selection.end >= 8) posicaoCursor++;
4343
}
4444
if (newValueLength >= 13) {
45-
newText.write('${newValue.text.substring(8, substrIndex = 12)}-');
46-
if (newValue.selection.end >= 12) selectionIndex++;
45+
valorFinal.write('${newValue.text.substring(8, substrIndex = 12)}-');
46+
if (newValue.selection.end >= 12) posicaoCursor++;
4747
}
4848
if (newValueLength >= substrIndex) {
49-
newText.write(newValue.text.substring(substrIndex));
49+
valorFinal.write(newValue.text.substring(substrIndex));
5050
}
5151

5252
return TextEditingValue(
53-
text: newText.toString().toUpperCase(),
54-
selection: TextSelection.collapsed(offset: selectionIndex),
53+
text: valorFinal.toString().toUpperCase(),
54+
selection: TextSelection.collapsed(offset: posicaoCursor),
5555
);
5656
}
5757
}

lib/src/formatters/cnpj_input_formatter.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@ class CnpjInputFormatter extends TextInputFormatter
1515

1616
if (newValueLength > maxLength) return oldValue;
1717

18-
var selectionIndex = newValue.selection.end;
18+
var posicaoCursor = newValue.selection.end;
1919
var substrIndex = 0;
20-
final newText = StringBuffer();
20+
final valorFinal = StringBuffer();
2121

2222
if (newValueLength >= 3) {
23-
newText.write('${newValue.text.substring(0, substrIndex = 2)}.');
24-
if (newValue.selection.end >= 2) selectionIndex++;
23+
valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}.');
24+
if (newValue.selection.end >= 2) posicaoCursor++;
2525
}
2626
if (newValueLength >= 6) {
27-
newText.write('${newValue.text.substring(2, substrIndex = 5)}.');
28-
if (newValue.selection.end >= 5) selectionIndex++;
27+
valorFinal.write('${newValue.text.substring(2, substrIndex = 5)}.');
28+
if (newValue.selection.end >= 5) posicaoCursor++;
2929
}
3030
if (newValueLength >= 9) {
31-
newText.write('${newValue.text.substring(5, substrIndex = 8)}/');
32-
if (newValue.selection.end >= 8) selectionIndex++;
31+
valorFinal.write('${newValue.text.substring(5, substrIndex = 8)}/');
32+
if (newValue.selection.end >= 8) posicaoCursor++;
3333
}
3434
if (newValueLength >= 13) {
35-
newText.write('${newValue.text.substring(8, substrIndex = 12)}-');
36-
if (newValue.selection.end >= 12) selectionIndex++;
35+
valorFinal.write('${newValue.text.substring(8, substrIndex = 12)}-');
36+
if (newValue.selection.end >= 12) posicaoCursor++;
3737
}
3838
if (newValueLength >= substrIndex) {
39-
newText.write(newValue.text.substring(substrIndex));
39+
valorFinal.write(newValue.text.substring(substrIndex));
4040
}
4141

4242
return TextEditingValue(
43-
text: newText.toString(),
44-
selection: TextSelection.collapsed(offset: selectionIndex),
43+
text: valorFinal.toString(),
44+
selection: TextSelection.collapsed(offset: posicaoCursor),
4545
);
4646
}
4747
}

lib/src/formatters/iof_input_formatter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class IOFInputFormatter extends TextInputFormatter {
2020
}
2121

2222
return TextEditingValue(
23-
text: valorFinal.toString(),
23+
text: valorFinal,
2424
selection: TextSelection.collapsed(offset: valorFinal.length),
2525
);
2626
}
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import 'package:flutter/services.dart';
2-
3-
/// Formata o valor do campo com a máscara de NCM: `XXXX.XX.XX`.
4-
class NCMInputFormatter extends TextInputFormatter {
5-
@override
6-
TextEditingValue formatEditUpdate(
7-
TextEditingValue oldValue, TextEditingValue newValue) {
8-
// verifica o tamanho máximo do campo
9-
if (newValue.text.length > 8) return oldValue;
10-
11-
var posicaoCursor = newValue.selection.end;
12-
var substrIndex = 0;
13-
final valorFinal = StringBuffer();
14-
15-
if (newValue.text.length >= 5) {
16-
valorFinal.write('${newValue.text.substring(0, substrIndex = 4)}.');
17-
if (newValue.selection.end >= 4) posicaoCursor++;
18-
}
19-
if (newValue.text.length >= 7) {
20-
valorFinal.write('${newValue.text.substring(4, substrIndex = 6)}.');
21-
if (newValue.selection.end >= 6) posicaoCursor++;
22-
}
23-
24-
if (newValue.text.length >= substrIndex) {
25-
valorFinal.write(newValue.text.substring(substrIndex));
26-
}
27-
28-
return TextEditingValue(
29-
text: valorFinal.toString(),
30-
selection: TextSelection.collapsed(offset: posicaoCursor),
31-
);
32-
}
33-
}
1+
import 'package:flutter/services.dart';
2+
3+
/// Formata o valor do campo com a máscara de NCM: `XXXX.XX.XX`.
4+
class NCMInputFormatter extends TextInputFormatter {
5+
@override
6+
TextEditingValue formatEditUpdate(
7+
TextEditingValue oldValue, TextEditingValue newValue) {
8+
// verifica o tamanho máximo do campo
9+
if (newValue.text.length > 8) return oldValue;
10+
11+
var posicaoCursor = newValue.selection.end;
12+
var substrIndex = 0;
13+
final valorFinal = StringBuffer();
14+
15+
if (newValue.text.length >= 5) {
16+
valorFinal.write('${newValue.text.substring(0, substrIndex = 4)}.');
17+
if (newValue.selection.end >= 4) posicaoCursor++;
18+
}
19+
if (newValue.text.length >= 7) {
20+
valorFinal.write('${newValue.text.substring(4, substrIndex = 6)}.');
21+
if (newValue.selection.end >= 6) posicaoCursor++;
22+
}
23+
24+
if (newValue.text.length >= substrIndex) {
25+
valorFinal.write(newValue.text.substring(substrIndex));
26+
}
27+
28+
return TextEditingValue(
29+
text: valorFinal.toString(),
30+
selection: TextSelection.collapsed(offset: posicaoCursor),
31+
);
32+
}
33+
}

lib/src/formatters/placa_veiculo_formatter.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ class PlacaVeiculoInputFormatter extends TextInputFormatter {
1313
// verifica o tamanho máximo do campo (7 caracteres alfanuméricos + 1 traço)
1414
if (newValue.text.replaceAll('-', '').length > 7) return oldValue;
1515

16-
var selectionIndex = newValue.selection.end;
16+
var posicaoCursor = newValue.selection.end;
1717
var substrIndex = 0;
18-
final newText = StringBuffer();
18+
final valorFinal = StringBuffer();
1919

2020
if (newValue.text.length > 3) {
2121
if (newValue.text.contains("-")) {
22-
newText.write(newValue.text.substring(0, substrIndex = 3));
22+
valorFinal.write(newValue.text.substring(0, substrIndex = 3));
2323
} else {
24-
newText.write(
24+
valorFinal.write(
2525
'${newValue.text.substring(0, substrIndex = 3)}-${newValue.text.substring(3, substrIndex = newValue.text.length)}',
2626
);
27-
selectionIndex++;
27+
posicaoCursor++;
2828
}
2929
}
3030

3131
if (newValue.text.length >= substrIndex) {
32-
newText.write(newValue.text.substring(substrIndex));
32+
valorFinal.write(newValue.text.substring(substrIndex));
3333
}
3434

3535
return TextEditingValue(
36-
text: newText.toString().toUpperCase(),
37-
selection: TextSelection.collapsed(offset: selectionIndex),
36+
text: valorFinal.toString().toUpperCase(),
37+
selection: TextSelection.collapsed(offset: posicaoCursor),
3838
);
3939
}
4040
}

lib/src/formatters/telefone_input_formatter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TelefoneInputFormatter extends TextInputFormatter {
1212

1313
final newValueLength = newValue.text.length;
1414
if (newValueLength == 11) {
15-
if (newValue.text.toString()[2] != '9') {
15+
if (newValue.text[2] != '9') {
1616
return oldValue;
1717
}
1818
}

lib/src/validators/cnpj_alfanumerico_validator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//Credits: CPF/CNPJ Validators
2-
//https://github.com/leonardocaldas/flutter-cpf-cnpj-validator
1+
// Credits: CPF/CNPJ Validators
2+
// https://github.com/leonardocaldas/flutter-cpf-cnpj-validator
33

44
import 'dart:math';
55

@@ -56,10 +56,10 @@ class CnpjAlfanumericoValidator {
5656
}
5757

5858
static String strip(String? cnpj) {
59-
final regex = RegExp(stipRegex);
59+
final regExp = RegExp(stipRegex);
6060
cnpj = cnpj ?? '';
6161

62-
return cnpj.replaceAll(regex, '');
62+
return cnpj.replaceAll(regExp, '');
6363
}
6464

6565
static bool isValid(String? cnpj, {bool stripBeforeValidation = true}) {

0 commit comments

Comments
 (0)