diff --git a/coverage/lcov.info b/coverage/lcov.info new file mode 100644 index 0000000..eab2ddd --- /dev/null +++ b/coverage/lcov.info @@ -0,0 +1,673 @@ +SF:lib/src/formatters/altura_input_formatter.dart +DA:5,1 +DA:9,3 +DA:12,2 +DA:13,3 +DA:14,1 +DA:19,1 +DA:22,3 +DA:23,7 +DA:24,3 +DA:25,5 +DA:28,1 +DA:29,1 +DA:30,2 +LF:13 +LH:13 +end_of_record +SF:lib/src/formatters/cartao_bancario_input_formatter.dart +DA:5,2 +DA:9,6 +DA:11,4 +DA:12,2 +DA:13,8 +DA:15,6 +DA:16,2 +DA:18,3 +DA:21,6 +DA:24,2 +DA:25,2 +DA:26,2 +LF:12 +LH:12 +end_of_record +SF:lib/src/formatters/centavos_input_formatter.dart +DA:9,1 +DA:10,3 +DA:11,0 +DA:16,1 +DA:19,5 +DA:21,1 +DA:23,1 +DA:24,2 +DA:26,2 +DA:27,6 +DA:28,3 +DA:29,1 +DA:30,4 +DA:31,4 +DA:34,1 +DA:39,1 +DA:44,3 +DA:45,1 +DA:46,1 +DA:47,0 +DA:49,1 +DA:51,1 +DA:52,1 +DA:53,2 +DA:58,2 +DA:59,2 +DA:60,1 +DA:62,1 +DA:66,2 +DA:67,2 +DA:68,1 +DA:70,0 +DA:74,1 +DA:75,1 +DA:80,1 +DA:81,0 +DA:83,1 +DA:86,1 +DA:87,0 +DA:89,1 +DA:91,1 +DA:92,1 +DA:93,2 +LF:43 +LH:38 +end_of_record +SF:lib/src/formatters/cep_input_formatter.dart +DA:9,2 +DA:11,2 +DA:15,6 +DA:17,2 +DA:18,4 +DA:20,8 +DA:21,4 +DA:22,2 +DA:23,3 +DA:25,2 +DA:26,2 +DA:27,3 +DA:29,6 +DA:32,2 +DA:33,2 +DA:34,2 +LF:16 +LH:16 +end_of_record +SF:lib/src/formatters/cest_input_formatter.dart +DA:5,2 +DA:9,6 +DA:11,4 +DA:13,2 +DA:15,6 +DA:16,8 +DA:17,7 +DA:20,6 +DA:21,8 +DA:22,7 +DA:25,6 +DA:26,6 +DA:29,2 +DA:30,2 +DA:31,2 +LF:15 +LH:15 +end_of_record +SF:lib/src/formatters/cnpj_input_formatter.dart +DA:8,2 +DA:11,2 +DA:14,4 +DA:16,4 +DA:18,4 +DA:20,2 +DA:22,2 +DA:23,8 +DA:24,7 +DA:26,2 +DA:27,8 +DA:28,7 +DA:30,2 +DA:31,8 +DA:32,7 +DA:34,2 +DA:35,8 +DA:36,7 +DA:38,2 +DA:39,6 +DA:42,2 +DA:43,2 +DA:44,2 +LF:23 +LH:23 +end_of_record +SF:lib/src/formatters/cns_formatter.dart +DA:5,2 +DA:9,6 +DA:11,4 +DA:13,2 +DA:15,6 +DA:16,8 +DA:17,7 +DA:19,6 +DA:20,8 +DA:21,7 +DA:23,6 +DA:24,8 +DA:25,7 +DA:27,6 +DA:28,6 +DA:31,2 +DA:32,2 +DA:33,2 +LF:18 +LH:18 +end_of_record +SF:lib/src/formatters/compound_formatters/cpf_ou_cpnj_formatter.dart +DA:6,5 +LF:1 +LH:1 +end_of_record +SF:lib/src/formatters/cpf_input_formatter.dart +DA:8,2 +DA:11,2 +DA:15,8 +DA:17,4 +DA:19,2 +DA:21,6 +DA:22,8 +DA:23,7 +DA:25,6 +DA:26,8 +DA:27,7 +DA:29,6 +DA:30,8 +DA:31,7 +DA:33,6 +DA:34,6 +DA:37,2 +DA:38,2 +DA:39,2 +LF:19 +LH:19 +end_of_record +SF:lib/src/formatters/data_input_formatter.dart +DA:5,1 +DA:9,3 +DA:11,2 +DA:13,1 +DA:15,3 +DA:16,4 +DA:17,4 +DA:19,3 +DA:20,4 +DA:21,4 +DA:23,3 +DA:24,3 +DA:27,1 +DA:28,1 +DA:29,1 +LF:15 +LH:15 +end_of_record +SF:lib/src/formatters/hora_input_formatter.dart +DA:5,1 +DA:9,3 +DA:11,2 +DA:13,1 +DA:15,2 +DA:16,1 +DA:17,0 +DA:18,0 +DA:20,1 +DA:21,0 +DA:22,0 +DA:24,1 +DA:25,0 +DA:26,0 +DA:27,0 +DA:28,0 +DA:30,1 +DA:31,3 +DA:32,1 +DA:33,4 +DA:34,4 +DA:38,3 +DA:39,3 +DA:42,1 +DA:43,1 +DA:44,1 +LF:26 +LH:18 +end_of_record +SF:lib/src/formatters/iof_input_formatter.dart +DA:5,2 +DA:8,4 +DA:11,2 +DA:13,2 +DA:15,2 +DA:17,2 +DA:19,10 +DA:22,2 +DA:23,2 +DA:24,4 +LF:10 +LH:10 +end_of_record +SF:lib/src/formatters/km_input_formatter.dart +DA:5,1 +DA:9,3 +DA:11,2 +DA:13,1 +DA:15,2 +DA:16,1 +DA:17,1 +DA:18,5 +DA:19,1 +DA:21,1 +DA:22,1 +DA:23,5 +DA:24,1 +DA:26,1 +DA:27,1 +DA:28,5 +DA:29,1 +DA:34,3 +DA:35,3 +DA:38,1 +DA:39,1 +DA:40,1 +LF:22 +LH:22 +end_of_record +SF:lib/src/formatters/ncm_input_formatter.dart +DA:5,1 +DA:9,3 +DA:11,2 +DA:13,1 +DA:15,3 +DA:16,4 +DA:17,4 +DA:19,3 +DA:20,4 +DA:21,4 +DA:24,3 +DA:25,3 +DA:28,1 +DA:29,1 +DA:30,1 +LF:15 +LH:15 +end_of_record +SF:lib/src/formatters/peso_input_formatter.dart +DA:5,1 +DA:9,3 +DA:11,2 +DA:13,1 +DA:15,2 +DA:16,1 +DA:17,4 +DA:18,4 +DA:20,1 +DA:21,4 +DA:22,4 +DA:26,3 +DA:27,3 +DA:30,1 +DA:31,1 +DA:32,1 +LF:16 +LH:16 +end_of_record +SF:lib/src/formatters/placa_veiculo_formatter.dart +DA:8,1 +DA:12,3 +DA:14,2 +DA:16,1 +DA:18,3 +DA:19,0 +DA:20,0 +DA:22,0 +DA:23,0 +DA:24,0 +DA:28,3 +DA:29,3 +DA:32,1 +DA:33,2 +DA:34,1 +LF:15 +LH:10 +end_of_record +SF:lib/src/formatters/real_input_formatter.dart +DA:8,2 +DA:12,2 +DA:16,6 +DA:18,4 +DA:20,2 +DA:22,2 +DA:23,4 +DA:25,6 +DA:28,2 +DA:29,2 +DA:30,4 +LF:11 +LH:11 +end_of_record +SF:lib/src/formatters/telefone_input_formatter.dart +DA:7,1 +DA:10,2 +DA:13,1 +DA:16,1 +DA:17,4 +DA:22,2 +DA:24,1 +DA:26,1 +DA:27,1 +DA:28,4 +DA:31,1 +DA:32,4 +DA:33,4 +DA:36,3 +DA:37,1 +DA:38,4 +DA:39,4 +DA:42,1 +DA:43,4 +DA:44,4 +DA:48,1 +DA:49,3 +DA:52,1 +DA:53,1 +DA:54,1 +LF:25 +LH:25 +end_of_record +SF:lib/src/formatters/temperatura_input_formatter.dart +DA:5,1 +DA:10,2 +DA:12,1 +DA:15,2 +DA:17,1 +DA:20,1 +DA:21,0 +DA:22,0 +DA:24,1 +DA:25,4 +DA:26,4 +DA:30,1 +DA:31,3 +DA:34,1 +DA:35,1 +DA:36,1 +LF:16 +LH:14 +end_of_record +SF:lib/src/formatters/validade_cartao_input_formatter.dart +DA:11,1 +DA:12,2 +DA:13,0 +DA:15,1 +DA:18,2 +DA:20,2 +DA:24,2 +DA:26,1 +DA:28,1 +DA:29,4 +DA:30,4 +DA:33,1 +DA:34,3 +DA:37,1 +DA:38,1 +DA:39,1 +LF:16 +LH:15 +end_of_record +SF:lib/src/modelos/meses.dart +DA:6,0 +DA:21,0 +LF:2 +LH:0 +end_of_record +SF:lib/src/modelos/semana.dart +DA:19,0 +DA:27,0 +DA:35,0 +DA:62,0 +DA:72,0 +DA:82,0 +LF:6 +LH:0 +end_of_record +SF:lib/src/util/extensores.dart +DA:5,1 +DA:6,6 +DA:10,3 +DA:15,2 +DA:17,1 +DA:22,1 +DA:23,2 +DA:28,1 +DA:29,6 +DA:33,3 +DA:38,2 +DA:40,1 +DA:45,1 +DA:46,2 +LF:14 +LH:14 +end_of_record +SF:lib/src/util/util_brasil_fields.dart +DA:6,1 +DA:7,2 +DA:8,2 +DA:12,1 +DA:13,2 +DA:14,1 +DA:18,1 +DA:19,2 +DA:20,1 +DA:21,3 +DA:29,1 +DA:31,3 +DA:34,3 +DA:35,5 +DA:43,1 +DA:45,3 +DA:47,1 +DA:50,5 +DA:53,2 +DA:54,0 +DA:55,4 +DA:57,5 +DA:60,2 +DA:61,0 +DA:62,3 +DA:66,1 +DA:67,3 +DA:70,1 +DA:74,2 +DA:77,2 +DA:80,0 +DA:81,0 +DA:84,0 +DA:85,0 +DA:88,1 +DA:89,1 +DA:90,1 +DA:94,1 +DA:95,1 +DA:96,1 +DA:100,1 +DA:103,1 +DA:105,2 +DA:108,1 +DA:109,1 +DA:111,3 +DA:112,1 +DA:115,1 +DA:119,1 +DA:126,1 +DA:128,2 +DA:129,1 +DA:130,1 +DA:131,1 +DA:132,5 +DA:133,1 +DA:134,6 +DA:135,1 +DA:136,7 +DA:138,1 +LF:60 +LH:54 +end_of_record +SF:lib/src/util/util_data.dart +DA:7,0 +DA:8,0 +DA:12,0 +DA:13,0 +DA:15,0 +DA:16,0 +DA:18,0 +DA:22,0 +DA:26,1 +DA:27,1 +DA:28,2 +DA:32,1 +DA:33,1 +DA:34,2 +DA:38,1 +DA:39,1 +DA:40,2 +DA:44,1 +DA:45,1 +DA:46,2 +DA:50,1 +DA:51,1 +DA:52,2 +DA:56,0 +DA:57,0 +DA:59,0 +DA:60,0 +DA:61,0 +DA:62,0 +DA:63,0 +DA:67,0 +DA:70,0 +DA:75,0 +DA:76,0 +DA:77,0 +DA:78,0 +DA:79,0 +DA:80,0 +DA:82,0 +DA:87,0 +DA:90,0 +DA:97,1 +DA:98,1 +DA:99,2 +DA:105,1 +DA:106,1 +DA:107,3 +DA:113,1 +DA:114,1 +DA:115,2 +LF:50 +LH:24 +end_of_record +SF:lib/src/formatters/adiciona_separador.dart +DA:1,3 +DA:4,15 +DA:5,3 +DA:6,3 +DA:9,3 +DA:10,6 +LF:6 +LH:6 +end_of_record +SF:lib/src/formatters/compound_formatters/compound_formatter.dart +DA:11,1 +DA:12,2 +DA:13,3 +DA:15,1 +DA:18,3 +DA:19,2 +DA:20,1 +DA:21,1 +DA:22,0 +DA:23,0 +DA:25,1 +LF:11 +LH:9 +end_of_record +SF:lib/src/validators/cnpj_validator.dart +DA:24,2 +DA:28,14 +DA:32,4 +DA:33,4 +DA:34,4 +DA:37,2 +DA:39,4 +DA:42,2 +DA:43,2 +DA:45,4 +DA:46,14 +DA:49,2 +DA:50,2 +DA:53,2 +DA:56,2 +DA:58,2 +DA:62,2 +DA:67,4 +DA:72,2 +DA:76,2 +DA:77,6 +DA:78,6 +DA:80,8 +DA:81,6 +DA:84,1 +DA:87,2 +DA:88,4 +DA:91,3 +DA:92,3 +DA:94,1 +LF:30 +LH:30 +end_of_record +SF:lib/src/validators/cpf_validator.dart +DA:25,2 +DA:27,10 +DA:29,4 +DA:31,2 +DA:33,6 +DA:34,8 +DA:37,8 +DA:39,4 +DA:42,2 +DA:43,2 +DA:45,4 +DA:46,12 +DA:49,2 +DA:50,2 +DA:53,2 +DA:56,2 +DA:58,2 +DA:62,2 +DA:67,4 +DA:72,2 +DA:76,2 +DA:77,6 +DA:78,6 +DA:80,8 +DA:81,6 +DA:84,1 +DA:87,2 +DA:88,4 +DA:91,3 +DA:92,3 +DA:94,1 +LF:31 +LH:31 +end_of_record diff --git a/example/lib/main.dart b/example/lib/main.dart index 6f4d938..a8bc09a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -68,6 +68,10 @@ class MyApp extends StatelessWidget { label: 'Hora', formatter: HoraInputFormatter(), ), + RowFormatters( + label: 'KM', + formatter: KmInputFormatter(), + ), RowFormatters( label: 'Centavos', formatter: CentavosInputFormatter()), RowFormatters( diff --git a/example/macos/Runner.xcodeproj/project.pbxproj b/example/macos/Runner.xcodeproj/project.pbxproj index d9333e4..c5fd548 100644 --- a/example/macos/Runner.xcodeproj/project.pbxproj +++ b/example/macos/Runner.xcodeproj/project.pbxproj @@ -182,7 +182,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0920; - LastUpgradeCheck = 1300; + LastUpgradeCheck = 1430; ORGANIZATIONNAME = ""; TargetAttributes = { 33CC10EC2044A3C60003C045 = { diff --git a/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index fb7259e..83d8872 100644 --- a/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ =3.0.0 <4.0.0" + dart: ">=3.1.0-185.0.dev <4.0.0" diff --git a/lib/src/formatters/altura_input_formatter.dart b/lib/src/formatters/altura_input_formatter.dart index 9be389b..42aa8f4 100644 --- a/lib/src/formatters/altura_input_formatter.dart +++ b/lib/src/formatters/altura_input_formatter.dart @@ -5,38 +5,29 @@ class AlturaInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 3) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 3) { - return oldValue; - } - - if (newValueLength > 0) { - final numNovo = int.tryParse(newValue.text.substring(0, 1)); - if (numNovo != null) { - if (numNovo > 2) { - return oldValue; - } + // evita que o primeiro digito seja > 2 + if (newValue.text.isNotEmpty) { + final primeiroDigito = int.tryParse(newValue.text[0]); + if (primeiroDigito == null || primeiroDigito > 2) { + return oldValue; } } - var selectionIndex = newValue.selection.end; - var substrIndex = 0; - final newText = StringBuffer(); - - if (newValueLength > 2) { - newText.write('${newValue.text.substring(0, substrIndex = 1)},'); - if (newValue.selection.end > 2) selectionIndex++; - } + String valorFinal = newValue.text; - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + // adiciona "," + if (newValue.text.length == 3) { + valorFinal = '${newValue.text[0]},${newValue.text[1]}${newValue.text[2]}'; + } else if (newValue.text.length == 2) { + valorFinal = '${newValue.text[0]},${newValue.text[1]}'; } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: valorFinal.length), ); } } diff --git a/lib/src/formatters/cartao_bancario_input_formatter.dart b/lib/src/formatters/cartao_bancario_input_formatter.dart index e91f505..38a1e7d 100644 --- a/lib/src/formatters/cartao_bancario_input_formatter.dart +++ b/lib/src/formatters/cartao_bancario_input_formatter.dart @@ -5,36 +5,25 @@ class CartaoBancarioInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 16) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 16) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; - var substrIndex = 0; - final newText = StringBuffer(); + int posicaoCursor = newValue.selection.end; + final valorFinal = StringBuffer(); + for (int i = 0; i < newValue.text.length; i++) { + // adiciona espaco em branco a cada 4 digitos + if (i % 4 == 0 && i != 0) { + valorFinal.write(' '); + // incrementa a posicao do cursor + if (posicaoCursor >= i) posicaoCursor++; + } - if (newValueLength >= 4) { - newText.write('${newValue.text.substring(0, substrIndex = 4)} '); - if (newValue.selection.end >= 5) selectionIndex++; - } - if (newValueLength >= 8) { - newText.write('${newValue.text.substring(4, substrIndex = 8)} '); - if (newValue.selection.end >= 9) selectionIndex++; - } - if (newValueLength >= 12) { - newText.write('${newValue.text.substring(8, substrIndex = 12)} '); - if (newValue.selection.end >= 13) selectionIndex++; - } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + valorFinal.write(newValue.text[i]); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/centavos_input_formatter.dart b/lib/src/formatters/centavos_input_formatter.dart index 11143d4..d123180 100644 --- a/lib/src/formatters/centavos_input_formatter.dart +++ b/lib/src/formatters/centavos_input_formatter.dart @@ -16,46 +16,37 @@ class CentavosInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + if (newValue.text.isEmpty || newValue.text.length > 12) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 12) { - return oldValue; - } - - if (newValueLength == 0) { - return newValue; - } - - const simbolo = 'R\$ '; final newText = StringBuffer(); - var centsValue = ""; - var valorFinal = newValue.text; - var numero = int.parse(newValue.text); + var centavos = ""; + var textoFinal = newValue.text; + var reais = int.parse(newValue.text); var textValue = newValue.text.padLeft( newValue.text.length == 1 ? casasDecimais + 1 : casasDecimais, ""); if (textValue.length >= casasDecimais) { - centsValue = textValue.substring( + centavos = textValue.substring( textValue.length - casasDecimais, textValue.length); - valorFinal = textValue.substring(0, textValue.length - casasDecimais); + textoFinal = textValue.substring(0, textValue.length - casasDecimais); + } + // apaga o campo quando os valores forem zero (inteiro e decimais). + if (reais == 0 && int.tryParse(centavos) == 0) { + return TextEditingValue.empty; } - // apaga o campo quando os valores foram zero. - if (numero == 0 && int.tryParse(centsValue) == 0) { - return const TextEditingValue( - text: "", - selection: TextSelection.collapsed(offset: 0), - ); + // apaga o campo quando novo valor for 0 e o valor anterior tambem era 0. + if (reais == 0 && (oldValue.text == '0,' || oldValue.text == 'R\$ 0,')) { + return TextEditingValue.empty; } // retorna apenas o valor decimal, após o 0 if (textValue.length == casasDecimais) { - valorFinal = "0,$centsValue"; + textoFinal = "0,$centavos"; if (moeda) { - valorFinal = simbolo + valorFinal; + textoFinal = 'R\$ $textoFinal'; } - newText.write(valorFinal); + newText.write(textoFinal); return TextEditingValue( text: newText.toString(), @@ -64,38 +55,38 @@ class CentavosInputFormatter extends TextInputFormatter { } // formata o número com 0, + centavos - if (numero > 0 && numero <= 9) { + if (reais > 0 && reais <= 9) { if (casasDecimais == 3) { - centsValue = "00$numero"; + centavos = "00$reais"; } else { - centsValue = "0$numero"; + centavos = "0$reais"; } - numero = 0; - } else if (numero >= 10 && numero < 100) { + reais = 0; + } else if (reais >= 10 && reais < 100) { if (casasDecimais == 3) { - centsValue = "0$numero"; + centavos = "0$reais"; } else { - centsValue = numero.toString(); + centavos = reais.toString(); } - numero = 0; - } else if (valorFinal.isNotEmpty) { - numero = int.parse(valorFinal); + reais = 0; + } else if (textoFinal.isNotEmpty) { + reais = int.parse(textoFinal); } // adiciona - if (numero > 999) { - valorFinal = "${adicionarSeparador(numero.toString())},$centsValue"; + if (reais > 999) { + textoFinal = "${adicionarSeparador(reais.toString())},$centavos"; } else { - valorFinal = "$numero,$centsValue"; + textoFinal = "$reais,$centavos"; } if (moeda) { - valorFinal = simbolo + valorFinal; + textoFinal = 'R\$ $textoFinal'; } - newText.write(valorFinal); + newText.write(textoFinal); return TextEditingValue( text: newText.toString(), diff --git a/lib/src/formatters/cep_input_formatter.dart b/lib/src/formatters/cep_input_formatter.dart index bdcce14..cddde62 100644 --- a/lib/src/formatters/cep_input_formatter.dart +++ b/lib/src/formatters/cep_input_formatter.dart @@ -11,38 +11,27 @@ class CepInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; - - // Define o tamanho máximo do campo. - if (newValueLength > 8) { - return oldValue; - } - var substrInicio = 2; - if (!ponto) { - substrInicio = 0; - } - - var selectionIndex = newValue.selection.end; - var substrIndex = 0; - final newText = StringBuffer(); - - if (newValueLength >= 3 && ponto) { - newText.write('${newValue.text.substring(0, substrIndex = 2)}.'); - if (newValue.selection.end >= 2) selectionIndex++; - } - if (newValueLength >= 6) { - newText - .write('${newValue.text.substring(substrInicio, substrIndex = 5)}-'); - if (newValue.selection.end >= 5) selectionIndex++; - } - - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + // verifica o tamanho máximo do campo + if (newValue.text.length > 8) return oldValue; + + final valorFinal = StringBuffer(); + int posicaoCursor = newValue.selection.end; + + for (int i = 0; i < newValue.text.length; i++) { + if (i == 2 && ponto) { + valorFinal.write('.'); + if (posicaoCursor > i) posicaoCursor++; + } + if (i == 5) { + valorFinal.write('-'); + if (posicaoCursor > i) posicaoCursor++; + } + valorFinal.write(newValue.text[i]); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/cest_input_formatter.dart b/lib/src/formatters/cest_input_formatter.dart index 171264f..d69c16d 100644 --- a/lib/src/formatters/cest_input_formatter.dart +++ b/lib/src/formatters/cest_input_formatter.dart @@ -1,42 +1,34 @@ -import 'package:brasil_fields/src/interfaces/compoundable_formatter.dart'; import 'package:flutter/services.dart'; /// Formata o valor do campo com a máscara CEST `XX.XXX.XX`. -class CESTInputFormatter extends TextInputFormatter - implements CompoundableFormatter { - // Define o tamanho máximo do campo. - @override - int get maxLength => 7; - +class CESTInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 7) return oldValue; - if (newValueLength > maxLength) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - if (newValueLength >= 3) { - newText.write('${newValue.text.substring(0, substrIndex = 2)}.'); - if (newValue.selection.end >= 2) selectionIndex++; + if (newValue.text.length >= 3) { + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}.'); + if (newValue.selection.end >= 2) posicaoCursor++; } - if (newValueLength >= 6) { - newText.write('${newValue.text.substring(2, substrIndex = 5)}.'); - if (newValue.selection.end >= 5) selectionIndex++; + + if (newValue.text.length >= 6) { + valorFinal.write('${newValue.text.substring(2, substrIndex = 5)}.'); + if (newValue.selection.end >= 5) posicaoCursor++; } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/cnpj_input_formatter.dart b/lib/src/formatters/cnpj_input_formatter.dart index 65e6321..7344ad7 100644 --- a/lib/src/formatters/cnpj_input_formatter.dart +++ b/lib/src/formatters/cnpj_input_formatter.dart @@ -13,9 +13,7 @@ class CnpjInputFormatter extends TextInputFormatter TextEditingValue oldValue, TextEditingValue newValue) { final newValueLength = newValue.text.length; - if (newValueLength > maxLength) { - return oldValue; - } + if (newValueLength > maxLength) return oldValue; var selectionIndex = newValue.selection.end; var substrIndex = 0; diff --git a/lib/src/formatters/cns_formatter.dart b/lib/src/formatters/cns_formatter.dart index 792bc95..2384907 100644 --- a/lib/src/formatters/cns_formatter.dart +++ b/lib/src/formatters/cns_formatter.dart @@ -5,36 +5,32 @@ class CNSInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo. + if (newValue.text.length > 15) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 15) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - if (newValueLength >= 3) { - newText.write('${newValue.text.substring(0, substrIndex = 3)} '); - if (newValue.selection.end >= 4) selectionIndex++; + if (newValue.text.length >= 3) { + valorFinal.write('${newValue.text.substring(0, substrIndex = 3)} '); + if (newValue.selection.end >= 4) posicaoCursor++; } - if (newValueLength >= 7) { - newText.write('${newValue.text.substring(3, substrIndex = 7)} '); - if (newValue.selection.end >= 8) selectionIndex++; + if (newValue.text.length >= 7) { + valorFinal.write('${newValue.text.substring(3, substrIndex = 7)} '); + if (newValue.selection.end >= 8) posicaoCursor++; } - if (newValueLength >= 11) { - newText.write('${newValue.text.substring(7, substrIndex = 11)} '); - if (newValue.selection.end >= 12) selectionIndex++; + if (newValue.text.length >= 11) { + valorFinal.write('${newValue.text.substring(7, substrIndex = 11)} '); + if (newValue.selection.end >= 12) posicaoCursor++; } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/cpf_input_formatter.dart b/lib/src/formatters/cpf_input_formatter.dart index 27969fc..2b3baa6 100644 --- a/lib/src/formatters/cpf_input_formatter.dart +++ b/lib/src/formatters/cpf_input_formatter.dart @@ -11,35 +11,32 @@ class CpfInputFormatter extends TextInputFormatter @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > maxLength) return oldValue; - if (newValueLength > maxLength) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - if (newValueLength >= 4) { - newText.write('${newValue.text.substring(0, substrIndex = 3)}.'); - if (newValue.selection.end >= 3) selectionIndex++; + if (newValue.text.length >= 4) { + valorFinal.write('${newValue.text.substring(0, substrIndex = 3)}.'); + if (newValue.selection.end >= 3) posicaoCursor++; } - if (newValueLength >= 7) { - newText.write('${newValue.text.substring(3, substrIndex = 6)}.'); - if (newValue.selection.end >= 6) selectionIndex++; + if (newValue.text.length >= 7) { + valorFinal.write('${newValue.text.substring(3, substrIndex = 6)}.'); + if (newValue.selection.end >= 6) posicaoCursor++; } - if (newValueLength >= 10) { - newText.write('${newValue.text.substring(6, substrIndex = 9)}-'); - if (newValue.selection.end >= 9) selectionIndex++; + if (newValue.text.length >= 10) { + valorFinal.write('${newValue.text.substring(6, substrIndex = 9)}-'); + if (newValue.selection.end >= 9) posicaoCursor++; } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/data_input_formatter.dart b/lib/src/formatters/data_input_formatter.dart index 7ea7340..876adb5 100644 --- a/lib/src/formatters/data_input_formatter.dart +++ b/lib/src/formatters/data_input_formatter.dart @@ -5,32 +5,28 @@ class DataInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 8) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 8) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - if (newValueLength >= 3) { - newText.write('${newValue.text.substring(0, substrIndex = 2)}/'); - if (newValue.selection.end >= 2) selectionIndex++; + if (newValue.text.length >= 3) { + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}/'); + if (newValue.selection.end >= 2) posicaoCursor++; } - if (newValueLength >= 5) { - newText.write('${newValue.text.substring(2, substrIndex = 4)}/'); - if (newValue.selection.end >= 4) selectionIndex++; + if (newValue.text.length >= 5) { + valorFinal.write('${newValue.text.substring(2, substrIndex = 4)}/'); + if (newValue.selection.end >= 4) posicaoCursor++; } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/hora_input_formatter.dart b/lib/src/formatters/hora_input_formatter.dart index aa63fd7..5531643 100644 --- a/lib/src/formatters/hora_input_formatter.dart +++ b/lib/src/formatters/hora_input_formatter.dart @@ -1,51 +1,52 @@ import 'package:flutter/services.dart'; /// Formata o valor do campo com a máscara de hora `HH:mm` +/// +/// Nao aceita [hora > 24] e [minuto > 59] class HoraInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 4) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 4) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); + + obterHora() => int.parse(newValue.text[0] + newValue.text[1]); - switch (newValueLength) { + switch (newValue.text.length) { case 1: - final hora = int.parse(newValue.text.substring(0, 1)); + final hora = int.parse(newValue.text[0]); if (hora >= 3) return oldValue; break; case 2: - final hora = int.parse(newValue.text.substring(0, 2)); - if (hora >= 24) return oldValue; + if (obterHora() >= 24) return oldValue; break; case 3: - final minuto = int.parse(newValue.text.substring(2, 3)); + if (obterHora() >= 24) return oldValue; + final minuto = int.parse(newValue.text[2]); if (minuto >= 6) return oldValue; - newText.write('${newValue.text.substring(0, substrIndex = 2)}:'); - if (newValue.selection.end >= 2) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}:'); + if (newValue.selection.end >= 2) posicaoCursor++; break; case 4: + if (obterHora() >= 24) return oldValue; final minuto = int.parse(newValue.text.substring(2, 4)); if (minuto >= 60) return oldValue; - newText.write('${newValue.text.substring(0, substrIndex = 2)}:'); - if (newValue.selection.end >= 2) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}:'); + if (newValue.selection.end >= 2) posicaoCursor++; break; } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/iof_input_formatter.dart b/lib/src/formatters/iof_input_formatter.dart index 1826607..ab7f4f0 100644 --- a/lib/src/formatters/iof_input_formatter.dart +++ b/lib/src/formatters/iof_input_formatter.dart @@ -1,33 +1,27 @@ import 'package:flutter/services.dart'; -/// Formata o valor do campo com a máscara `9,99999` +/// Formata o valor do campo com a máscara `1,234567` class IOFInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { final newValueLength = newValue.text.length; - // Verifica o tamanho máximo do campo. - if (newValueLength > 7) { - return oldValue; - } + // verifica o tamanho máximo do campo + if (newValueLength > 7) return oldValue; - if (newValueLength == 0) { - return newValue; - } + if (newValueLength == 0) return newValue; - final newText = StringBuffer(); + String valorFinal = newValue.text; if (newValueLength >= 2) { - newText.write( - '${newValue.text.substring(0, 1)},${newValue.text.substring(1, newValueLength)}'); - } else { - newText.write(newValue.text); + valorFinal = + '${newValue.text.substring(0, 1)},${newValue.text.substring(1, newValueLength)}'; } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: newText.length), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: valorFinal.length), ); } } diff --git a/lib/src/formatters/km_input_formatter.dart b/lib/src/formatters/km_input_formatter.dart index 045182d..abe44c2 100644 --- a/lib/src/formatters/km_input_formatter.dart +++ b/lib/src/formatters/km_input_formatter.dart @@ -5,42 +5,39 @@ class KmInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 6) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 6) { - return oldValue; - } - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - switch (newValueLength) { + switch (newValue.text.length) { case 4: - newText.write( + valorFinal.write( '${newValue.text.substring(0, substrIndex = 1)}.${newValue.text.substring(1, substrIndex = 3)}'); - selectionIndex += 1; + posicaoCursor += 1; break; case 5: - newText.write( + valorFinal.write( '${newValue.text.substring(0, substrIndex = 2)}.${newValue.text.substring(2, substrIndex = 4)}'); - selectionIndex += 1; + posicaoCursor += 1; break; case 6: - newText.write( + valorFinal.write( '${newValue.text.substring(0, substrIndex = 3)}.${newValue.text.substring(3, substrIndex = 5)}'); - selectionIndex += 1; + posicaoCursor += 1; break; default: } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/ncm_input_formatter.dart b/lib/src/formatters/ncm_input_formatter.dart index c62c9a3..f78f14a 100644 --- a/lib/src/formatters/ncm_input_formatter.dart +++ b/lib/src/formatters/ncm_input_formatter.dart @@ -1,41 +1,33 @@ -import 'package:brasil_fields/src/interfaces/compoundable_formatter.dart'; import 'package:flutter/services.dart'; /// Formata o valor do campo com a máscara de NCM: `XXXX.XX.XX` -class NCMInputFormatter extends TextInputFormatter - implements CompoundableFormatter { - @override - int get maxLength => 8; - +class NCMInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; - - if (newValueLength > maxLength) { - return oldValue; - } + // verifica o tamanho máximo do campo + if (newValue.text.length > 8) return oldValue; - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - if (newValueLength >= 5) { - newText.write('${newValue.text.substring(0, substrIndex = 4)}.'); - if (newValue.selection.end >= 4) selectionIndex++; + if (newValue.text.length >= 5) { + valorFinal.write('${newValue.text.substring(0, substrIndex = 4)}.'); + if (newValue.selection.end >= 4) posicaoCursor++; } - if (newValueLength >= 7) { - newText.write('${newValue.text.substring(4, substrIndex = 6)}.'); - if (newValue.selection.end >= 6) selectionIndex++; + if (newValue.text.length >= 7) { + valorFinal.write('${newValue.text.substring(4, substrIndex = 6)}.'); + if (newValue.selection.end >= 6) posicaoCursor++; } - if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/peso_input_formatter.dart b/lib/src/formatters/peso_input_formatter.dart index 3e45d3f..25f0d37 100644 --- a/lib/src/formatters/peso_input_formatter.dart +++ b/lib/src/formatters/peso_input_formatter.dart @@ -5,35 +5,31 @@ class PesoInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newTextLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 4) return oldValue; - // Verifica o tamanho máximo do campo. - if (newTextLength > 4) { - return oldValue; - } - - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); - switch (newTextLength) { + switch (newValue.text.length) { case 3: - newText.write('${newValue.text.substring(0, substrIndex = 2)},'); - if (newValue.selection.end >= 3) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)},'); + if (newValue.selection.end >= 3) posicaoCursor++; break; case 4: - newText.write('${newValue.text.substring(0, substrIndex = 3)},'); - if (newValue.selection.end >= 4) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 3)},'); + if (newValue.selection.end >= 4) posicaoCursor++; break; } - if (newTextLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/placa_veiculo_formatter.dart b/lib/src/formatters/placa_veiculo_formatter.dart index 4df10c3..7529a50 100644 --- a/lib/src/formatters/placa_veiculo_formatter.dart +++ b/lib/src/formatters/placa_veiculo_formatter.dart @@ -8,27 +8,25 @@ class PlacaVeiculoInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; - // Verifica o tamanho máximo do campo. - if (newValueLength > 8) { - return oldValue; - } + // verifica o tamanho máximo do campo + // TODO remover replace + if (newValue.text.replaceAll('-', '').length > 7) return oldValue; var selectionIndex = newValue.selection.end; var substrIndex = 0; final newText = StringBuffer(); - if (newValueLength == 4) { + if (newValue.text.length > 3) { if (newValue.text.contains("-")) { newText.write(newValue.text.substring(0, substrIndex = 3)); } else { newText.write( - '${newValue.text.substring(0, substrIndex = 3)}-${newValue.text.substring(3, substrIndex = newValueLength)}'); + '${newValue.text.substring(0, substrIndex = 3)}-${newValue.text.substring(3, substrIndex = newValue.text.length)}'); selectionIndex++; } } - if (newValueLength >= substrIndex) { + if (newValue.text.length >= substrIndex) { newText.write(newValue.text.substring(substrIndex)); } diff --git a/lib/src/formatters/real_input_formatter.dart b/lib/src/formatters/real_input_formatter.dart index 9451246..c864d5a 100644 --- a/lib/src/formatters/real_input_formatter.dart +++ b/lib/src/formatters/real_input_formatter.dart @@ -12,29 +12,22 @@ class RealInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; + // verifica o tamanho máximo do campo + if (newValue.text.length > 12) return oldValue; - // Verifica o tamanho máximo do campo. - if (newValueLength > 12) { - return oldValue; - } - - if (newValueLength == 0) { - return newValue; - } + if (newValue.text.isEmpty) return newValue; - final newText = StringBuffer(); - var valorFinal = newValue.text; + final valorFinal = StringBuffer(); if (moeda) { - newText.write('R\$ ${adicionarSeparador(valorFinal)}'); + valorFinal.write('R\$ ${adicionarSeparador(newValue.text)}'); } else { - newText.write(adicionarSeparador(valorFinal)); + valorFinal.write(adicionarSeparador(newValue.text)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: newText.length), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: valorFinal.length), ); } } diff --git a/lib/src/formatters/telefone_input_formatter.dart b/lib/src/formatters/telefone_input_formatter.dart index 99f9a08..372f92f 100644 --- a/lib/src/formatters/telefone_input_formatter.dart +++ b/lib/src/formatters/telefone_input_formatter.dart @@ -7,51 +7,49 @@ class TelefoneInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { - final newValueLength = newValue.text.length; - // Verifica o tamanho máximo do campo. - if (newValueLength > 11) { - return oldValue; - } + if (newValue.text.length > 11) return oldValue; + + final newValueLength = newValue.text.length; if (newValueLength == 11) { if (newValue.text.toString()[2] != '9') { return oldValue; } } - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); if (newValueLength >= 1) { - newText.write('('); - if (newValue.selection.end >= 1) selectionIndex++; + valorFinal.write('('); + if (newValue.selection.end >= 1) posicaoCursor++; } if (newValueLength >= 3) { - newText.write('${newValue.text.substring(0, substrIndex = 2)}) '); - if (newValue.selection.end >= 2) selectionIndex += 2; + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}) '); + if (newValue.selection.end >= 2) posicaoCursor += 2; } if (newValue.text.length == 11) { if (newValueLength >= 8) { - newText.write('${newValue.text.substring(2, substrIndex = 7)}-'); - if (newValue.selection.end >= 7) selectionIndex++; + valorFinal.write('${newValue.text.substring(2, substrIndex = 7)}-'); + if (newValue.selection.end >= 7) posicaoCursor++; } } else { if (newValueLength >= 7) { - newText.write('${newValue.text.substring(2, substrIndex = 6)}-'); - if (newValue.selection.end >= 6) selectionIndex++; + valorFinal.write('${newValue.text.substring(2, substrIndex = 6)}-'); + if (newValue.selection.end >= 6) posicaoCursor++; } } if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/temperatura_input_formatter.dart b/lib/src/formatters/temperatura_input_formatter.dart index 997d7d6..7593fde 100644 --- a/lib/src/formatters/temperatura_input_formatter.dart +++ b/lib/src/formatters/temperatura_input_formatter.dart @@ -4,36 +4,32 @@ import 'package:flutter/services.dart'; class TemperaturaInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( - TextEditingValue oldValue, - TextEditingValue newValue, - ) { - final newTextLength = newValue.text.length; - // Verifica o tamanho máximo do campo. - if (newTextLength > 3) { - return oldValue; - } - var selectionIndex = newValue.selection.end; - var usedSubstringIndex = 0; - final newText = StringBuffer(); + TextEditingValue oldValue, TextEditingValue newValue) { + // verifica o tamanho máximo do campo + if (newValue.text.length > 3) return oldValue; + + var posicaoCursor = newValue.selection.end; + var substrIndex = 0; + final valorFinal = StringBuffer(); - switch (newTextLength) { + switch (newValue.text.length) { case 2: - newText.write('${newValue.text.substring(0, usedSubstringIndex = 1)},'); - if (newValue.selection.end >= 2) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 1)},'); + if (newValue.selection.end >= 2) posicaoCursor++; break; case 3: - newText.write('${newValue.text.substring(0, usedSubstringIndex = 2)},'); - if (newValue.selection.end >= 3) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)},'); + if (newValue.selection.end >= 3) posicaoCursor++; break; } - if (newTextLength >= usedSubstringIndex) { - newText.write(newValue.text.substring(usedSubstringIndex)); + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/lib/src/formatters/validade_cartao_input_formatter.dart b/lib/src/formatters/validade_cartao_input_formatter.dart index 49c5bed..11231ca 100644 --- a/lib/src/formatters/validade_cartao_input_formatter.dart +++ b/lib/src/formatters/validade_cartao_input_formatter.dart @@ -17,26 +17,24 @@ class ValidadeCartaoInputFormatter extends TextInputFormatter { TextEditingValue oldValue, TextEditingValue newValue) { final newValueLength = newValue.text.length; - if (newValueLength > maxLength) { - return oldValue; - } + if (newValueLength > maxLength) return oldValue; - var selectionIndex = newValue.selection.end; + var posicaoCursor = newValue.selection.end; var substrIndex = 0; - final newText = StringBuffer(); + final valorFinal = StringBuffer(); if (newValueLength >= 3) { - newText.write('${newValue.text.substring(0, substrIndex = 2)}/'); - if (newValue.selection.end >= 2) selectionIndex++; + valorFinal.write('${newValue.text.substring(0, substrIndex = 2)}/'); + if (newValue.selection.end >= 2) posicaoCursor++; } if (newValueLength >= substrIndex) { - newText.write(newValue.text.substring(substrIndex)); + valorFinal.write(newValue.text.substring(substrIndex)); } return TextEditingValue( - text: newText.toString(), - selection: TextSelection.collapsed(offset: selectionIndex), + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), ); } } diff --git a/pubspec.lock b/pubspec.lock index 702266b..bd354e5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" + sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 url: "https://pub.dev" source: hosted - version: "1.17.1" + version: "1.17.2" fake_async: dependency: transitive description: @@ -58,10 +58,10 @@ packages: dependency: "direct dev" description: name: flutter_lints - sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c + sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04 url: "https://pub.dev" source: hosted - version: "2.0.1" + version: "2.0.3" flutter_test: dependency: "direct dev" description: flutter @@ -75,38 +75,30 @@ packages: url: "https://pub.dev" source: hosted version: "0.18.1" - js: - dependency: transitive - description: - name: js - sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 - url: "https://pub.dev" - source: hosted - version: "0.6.7" lints: dependency: transitive description: name: lints - sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593" + sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" url: "https://pub.dev" source: hosted - version: "2.0.1" + version: "2.1.1" matcher: dependency: transitive description: name: matcher - sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" url: "https://pub.dev" source: hosted - version: "0.12.15" + version: "0.12.16" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 + sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" url: "https://pub.dev" source: hosted - version: "0.2.0" + version: "0.5.0" meta: dependency: transitive description: @@ -132,10 +124,10 @@ packages: dependency: transitive description: name: source_span - sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: @@ -172,10 +164,10 @@ packages: dependency: transitive description: name: test_api - sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb + sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" url: "https://pub.dev" source: hosted - version: "0.5.1" + version: "0.6.0" vector_math: dependency: transitive description: @@ -184,5 +176,13 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + web: + dependency: transitive + description: + name: web + sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + url: "https://pub.dev" + source: hosted + version: "0.1.4-beta" sdks: - dart: ">=3.0.0 <4.0.0" + dart: ">=3.1.0-185.0.dev <4.0.0" diff --git a/test/boilerplate.dart b/test/boilerplate.dart new file mode 100644 index 0000000..d53cb0f --- /dev/null +++ b/test/boilerplate.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +Widget boilerplate( + TextInputFormatter inputFormatter, TextEditingController textController) { + return MaterialApp( + home: MediaQuery( + data: const MediaQueryData(size: Size(320, 480)), + child: Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: TextField( + controller: textController, + inputFormatters: [ + FilteringTextInputFormatter.digitsOnly, + inputFormatter, + ], + ), + ), + ), + ), + ); +} diff --git a/test/brasil_fields_test.dart b/test/brasil_fields_test.dart index 2a3b8ba..02d7725 100644 --- a/test/brasil_fields_test.dart +++ b/test/brasil_fields_test.dart @@ -4,26 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -Widget boilerplate( - TextInputFormatter inputFormatter, TextEditingController textController) { - return MaterialApp( - home: MediaQuery( - data: const MediaQueryData(size: Size(320, 480)), - child: Directionality( - textDirection: TextDirection.ltr, - child: Material( - child: TextField( - controller: textController, - inputFormatters: [ - FilteringTextInputFormatter.digitsOnly, - inputFormatter, - ], - ), - ), - ), - ), - ); -} +import 'boilerplate.dart'; Widget boilerplatePlacaVeiculo( TextInputFormatter inputFormatter, TextEditingController textController) { @@ -217,15 +198,6 @@ void main() { expect(textController.text, '12/23'); }); - testWidgets('AlturaInputFormatter', (WidgetTester tester) async { - final textController = TextEditingController(); - await tester - .pumpWidget(boilerplate(AlturaInputFormatter(), textController)); - - await tester.enterText(find.byType(TextField), '176'); - expect(textController.text, '1,76'); - }); - testWidgets('TemperaturaInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); await tester diff --git a/test/src/formatters/altura_input_formatter_test.dart b/test/src/formatters/altura_input_formatter_test.dart new file mode 100644 index 0000000..e1d0566 --- /dev/null +++ b/test/src/formatters/altura_input_formatter_test.dart @@ -0,0 +1,25 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return AlturaInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('AlturaInputFormatter', () { + test('padrao', () => expect(evaluate('', '175'), '1,75')); + test('limite 3 digitos', () => expect(evaluate('', '1759'), '')); + test('valor > 3', () => expect(evaluate('', '3'), '')); + test('backspace', () { + expect(evaluate('1,75', '17'), '1,7'); + expect(evaluate('1,7', '1'), '1'); + expect(evaluate('1', ''), ''); + expect(evaluate('', ''), ''); + }); + }); +} diff --git a/test/src/formatters/cartao_bancario_input_formatter_test.dart b/test/src/formatters/cartao_bancario_input_formatter_test.dart new file mode 100644 index 0000000..a53e2a8 --- /dev/null +++ b/test/src/formatters/cartao_bancario_input_formatter_test.dart @@ -0,0 +1,41 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return CartaoBancarioInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('CartaoBancarioInputFormatter', () { + test('padrao', + () => expect(evaluate('', '1111222233334444'), '1111 2222 3333 4444')); + test('limite 16 digitos', + () => expect(evaluate('', '11112223334445555'), '')); + test('backspace', () { + expect(evaluate('', '111122223333444'), '1111 2222 3333 444'); + expect(evaluate('', '11112222333344'), '1111 2222 3333 44'); + expect( + evaluate('', '1111222233334'), + '1111 2222 3333 4', + ); + expect(evaluate('', '111122223333'), '1111 2222 3333'); + expect(evaluate('', '11112222333'), '1111 2222 333'); + expect(evaluate('', '1111222233'), '1111 2222 33'); + expect(evaluate('', '111122223'), '1111 2222 3'); + expect(evaluate('', '11112222'), '1111 2222'); + expect(evaluate('', '1111222'), '1111 222'); + expect(evaluate('', '111122'), '1111 22'); + expect(evaluate('', '11112'), '1111 2'); + expect(evaluate('', '1111'), '1111'); + expect(evaluate('', '111'), '111'); + expect(evaluate('', '11'), '11'); + expect(evaluate('', '1'), '1'); + expect(evaluate('', ''), ''); + }); + }); +} diff --git a/test/src/formatters/cep_input_formatter_test.dart b/test/src/formatters/cep_input_formatter_test.dart new file mode 100644 index 0000000..33fb21c --- /dev/null +++ b/test/src/formatters/cep_input_formatter_test.dart @@ -0,0 +1,45 @@ +import 'package:brasil_fields/src/formatters/cep_input_formatter.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue, [bool ponto = true]) { + return CepInputFormatter(ponto: ponto) + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('CepInputFormatter', () { + test('padrao', () => expect(evaluate('', '12345678'), '12.345-678')); + test('limite 8 digitos', () => expect(evaluate('', '912345678'), '')); + + test('padrao [ponto: false]', + () => expect(evaluate('', '12345678', false), '12345-678')); + test('limite 8 digitos [ponto: false]', + () => expect(evaluate('', '912345678', false), '')); + + test('backspace', () { + expect(evaluate('12.345-678', '1234567'), '12.345-67'); + expect(evaluate('12.345-67', '123456'), '12.345-6'); + expect(evaluate('12.345-6', '12345'), '12.345'); + expect(evaluate('12.345', '1234'), '12.34'); + expect(evaluate('12.34', '123'), '12.3'); + expect(evaluate('12.3', '12'), '12'); + expect(evaluate('12', '1'), '1'); + expect(evaluate('1', ''), ''); + }); + + test('backspace [ponto: false]', () { + expect(evaluate('12345-678', '1234567', false), '12345-67'); + expect(evaluate('12345-67', '123456', false), '12345-6'); + expect(evaluate('12345-6', '12345', false), '12345'); + expect(evaluate('12345', '1234', false), '1234'); + expect(evaluate('1234', '123', false), '123'); + expect(evaluate('123', '12', false), '12'); + expect(evaluate('12', '1', false), '1'); + expect(evaluate('1', '', false), ''); + }); + }); +} diff --git a/test/src/formatters/cest_input_formatter_test.dart b/test/src/formatters/cest_input_formatter_test.dart new file mode 100644 index 0000000..061cecd --- /dev/null +++ b/test/src/formatters/cest_input_formatter_test.dart @@ -0,0 +1,28 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return CESTInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('CESTInputFormatter', () { + test('padrao', () => expect(evaluate('', '1234567'), '12.345.67')); + test('limite 7 digitos', () => expect(evaluate('', '12345678'), '')); + + test('backspace', () { + expect(evaluate('12.345.67', '123456'), '12.345.6'); + expect(evaluate('12.345.6', '12345'), '12.345'); + expect(evaluate('12.345', '1234'), '12.34'); + expect(evaluate('12.34', '123'), '12.3'); + expect(evaluate('12.3', '12'), '12'); + expect(evaluate('12', '1'), '1'); + expect(evaluate('1', ''), ''); + }); + }); +} diff --git a/test/src/formatters/cnpj_input_formatter_test.dart b/test/src/formatters/cnpj_input_formatter_test.dart new file mode 100644 index 0000000..c3880bd --- /dev/null +++ b/test/src/formatters/cnpj_input_formatter_test.dart @@ -0,0 +1,37 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return CnpjInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('CnpjInputFormatter', () { + test('padrao', + () => expect(evaluate('', '99999999999999'), '99.999.999/9999-99')); + test( + 'limite 14 digitos', () => expect(evaluate('', '199999999999999'), '')); + test('backspace', () { + expect( + evaluate('99.999.999/9999-99', '9999999999999'), '99.999.999/9999-9'); + expect(evaluate('99.999.999/9999-9', '999999999999'), '99.999.999/9999'); + expect(evaluate('99.999.999/9999', '99999999999'), '99.999.999/999'); + expect(evaluate('99.999.999/999', '9999999999'), '99.999.999/99'); + expect(evaluate('99.999.999/99', '999999999'), '99.999.999/9'); + expect(evaluate('99.999.999/9', '99999999'), '99.999.999'); + expect(evaluate('99.999.999', '9999999'), '99.999.99'); + expect(evaluate('99.999.99', '999999'), '99.999.9'); + expect(evaluate('99.999.9', '99999'), '99.999'); + expect(evaluate('99.999', '9999'), '99.99'); + expect(evaluate('99.99', '999'), '99.9'); + expect(evaluate('99.9', '99'), '99'); + expect(evaluate('99', '9'), '9'); + expect(evaluate('9', ''), ''); + }); + }); +} diff --git a/test/src/formatters/cns_formatter_test.dart b/test/src/formatters/cns_formatter_test.dart new file mode 100644 index 0000000..526c5dd --- /dev/null +++ b/test/src/formatters/cns_formatter_test.dart @@ -0,0 +1,42 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return CNSInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('CNSInputFormatter', () { + test('padrao', + () => expect(evaluate('', '000111122223333'), '000 1111 2222 3333')); + + test('limite 15 digitos', + () => expect(evaluate('', '9000111122223333'), '')); + + test('backspace', () { + expect(evaluate('000 1111 2222 3333', '00011112222333'), + '000 1111 2222 333'); + expect( + evaluate('000 1111 2222 333', '0001111222233'), '000 1111 2222 33'); + expect(evaluate('000 1111 2222 33', '000111122223'), '000 1111 2222 3'); + // TODO: verificar o espaco em branco + expect(evaluate('000 1111 2222 3', '00011112222'), '000 1111 2222 '); + expect(evaluate('000 1111 2222', '0001111222'), '000 1111 222'); + expect(evaluate('000 1111 222', '000111122'), '000 1111 22'); + expect(evaluate('000 1111 22', '00011112'), '000 1111 2'); + expect(evaluate('000 1111 2', '0001111'), '000 1111 '); + expect(evaluate('000 1111', '000111'), '000 111'); + expect(evaluate('000 111', '00011'), '000 11'); + expect(evaluate('000 11', '0001'), '000 1'); + expect(evaluate('000 1', '000'), '000 '); + expect(evaluate('000', '00'), '00'); + expect(evaluate('00', '0'), '0'); + expect(evaluate('0', ''), ''); + }); + }); +} diff --git a/test/src/formatters/cpf_input_formatter_test.dart b/test/src/formatters/cpf_input_formatter_test.dart new file mode 100644 index 0000000..31d026e --- /dev/null +++ b/test/src/formatters/cpf_input_formatter_test.dart @@ -0,0 +1,31 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return CpfInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('CpfInputFormatter', () { + test('padrao', () => expect(evaluate('', '11122233344'), '111.222.333-44')); + test('limite 11 digitos', () => expect(evaluate('', '911122233344'), '')); + test('backspace', () { + expect(evaluate('111.222.333-44', '1112223334'), '111.222.333-4'); + expect(evaluate('111.222.333-4', '111222333'), '111.222.333'); + expect(evaluate('111.222.333', '11122233'), '111.222.33'); + expect(evaluate('111.222.33', '1112223'), '111.222.3'); + expect(evaluate('111.222.3', '111222'), '111.222'); + expect(evaluate('111.222', '11122'), '111.22'); + expect(evaluate('111.22', '1112'), '111.2'); + expect(evaluate('111.2', '111'), '111'); + expect(evaluate('111', '11'), '11'); + expect(evaluate('11', '1'), '1'); + expect(evaluate('1', ''), ''); + }); + }); +} diff --git a/test/src/formatters/hora_input_formatter_test.dart b/test/src/formatters/hora_input_formatter_test.dart new file mode 100644 index 0000000..c6ada69 --- /dev/null +++ b/test/src/formatters/hora_input_formatter_test.dart @@ -0,0 +1,32 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return HoraInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('HoraInputFormatter', () { + test('padrao', () => expect(evaluate('', '1234'), '12:34')); + test('limite 4 digitos', () => expect(evaluate('', '91234'), '')); + test('hora > 24', () => expect(evaluate('', '2559'), '')); + test('backspace', () { + expect(evaluate('1234', '123'), '12:3'); + expect(evaluate('12:3', '12'), '12'); + expect(evaluate('12', '1'), '1'); + expect(evaluate('1', ''), ''); + }); + + test('testa digitacao', () { + expect(evaluate('', '1'), '1'); + expect(evaluate('1', '12'), '12'); + expect(evaluate('12', '123'), '12:3'); + expect(evaluate('12:3', '1234'), '12:34'); + }); + }); +} diff --git a/test/src/formatters/iof_input_formatter_test.dart b/test/src/formatters/iof_input_formatter_test.dart new file mode 100644 index 0000000..51d8de0 --- /dev/null +++ b/test/src/formatters/iof_input_formatter_test.dart @@ -0,0 +1,27 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return IOFInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('IOFInputFormatter', () { + test('padrao', () => expect(evaluate('', '1234567'), '1,234567')); + test('limite 7 digitos', () => expect(evaluate('', '12345678'), '')); + test('backspace', () { + expect(evaluate('1,234567', '123456'), '1,23456'); + expect(evaluate('1,23456', '12345'), '1,2345'); + expect(evaluate('1,2345', '1234'), '1,234'); + expect(evaluate('1,234', '123'), '1,23'); + expect(evaluate('1,23', '12'), '1,2'); + expect(evaluate('1,2', '1'), '1'); + expect(evaluate('1', ''), ''); + }); + }); +} diff --git a/test/src/formatters/km_input_formatter_test.dart b/test/src/formatters/km_input_formatter_test.dart new file mode 100644 index 0000000..d1e7788 --- /dev/null +++ b/test/src/formatters/km_input_formatter_test.dart @@ -0,0 +1,35 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return KmInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('KmInputFormatter', () { + test('padrao', () => expect(evaluate('', '123456'), '123.456')); + test('testa limite 6 digitos', () => expect(evaluate('', '9123456'), '')); + + test('backspace', () { + expect(evaluate('', '12345'), '12.345'); + expect(evaluate('', '1234'), '1.234'); + expect(evaluate('', '123'), '123'); + expect(evaluate('', '12'), '12'); + expect(evaluate('', '1'), '1'); + }); + + test('digitacao', () { + expect(evaluate('', '1'), '1'); + expect(evaluate('', '12'), '12'); + expect(evaluate('', '123'), '123'); + expect(evaluate('', '1234'), '1.234'); + expect(evaluate('', '12345'), '12.345'); + expect(evaluate('', '123456'), '123.456'); + }); + }); +} diff --git a/test/src/formatters/ncm_input_formatter_test.dart b/test/src/formatters/ncm_input_formatter_test.dart new file mode 100644 index 0000000..b14f126 --- /dev/null +++ b/test/src/formatters/ncm_input_formatter_test.dart @@ -0,0 +1,37 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return NCMInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('NCMInputFormatter', () { + test('padrao', () => expect(evaluate('', '12345678'), '1234.56.78')); + test('limite 8 digitos', () => expect(evaluate('', '912345678'), '')); + test('backspace', () { + expect(evaluate('', '1234567'), '1234.56.7'); + expect(evaluate('', '123456'), '1234.56'); + expect(evaluate('', '12345'), '1234.5'); + expect(evaluate('', '1234'), '1234'); + expect(evaluate('', '123'), '123'); + expect(evaluate('', '12'), '12'); + expect(evaluate('', '1'), '1'); + }); + + test('digitacao', () { + expect(evaluate('', '1'), '1'); + expect(evaluate('', '12'), '12'); + expect(evaluate('', '123'), '123'); + expect(evaluate('', '1234'), '1234'); + expect(evaluate('', '12345'), '1234.5'); + expect(evaluate('', '123456'), '1234.56'); + expect(evaluate('', '1234567'), '1234.56.7'); + }); + }); +} diff --git a/test/src/formatters/placa_veiculo_formatter_test.dart b/test/src/formatters/placa_veiculo_formatter_test.dart new file mode 100644 index 0000000..018fcf7 --- /dev/null +++ b/test/src/formatters/placa_veiculo_formatter_test.dart @@ -0,0 +1,38 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return PlacaVeiculoInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('PlacaVeiculoInputFormatter', () { + test('padrao', () => expect(evaluate('', 'ABC1234'), 'ABC-1234')); + test('limite 8 digitos', () => expect(evaluate('', '9ABC1234'), '')); + + test('backspace', () { + expect(evaluate('', 'abc1234'), 'ABC-1234'); + expect(evaluate('', 'abc123'), 'ABC-123'); + expect(evaluate('', 'abc12'), 'ABC-12'); + + expect(evaluate('', 'abc1'), 'ABC-1'); + expect(evaluate('', 'abc'), 'ABC'); + expect(evaluate('', 'ab'), 'AB'); + expect(evaluate('', 'a'), 'A'); + }); + test('digitacao', () { + expect(evaluate('', 'a'), 'A'); + expect(evaluate('', 'ab'), 'AB'); + expect(evaluate('', 'abc'), 'ABC'); + expect(evaluate('', 'abc1'), 'ABC-1'); + expect(evaluate('', 'abc12'), 'ABC-12'); + expect(evaluate('', 'abc123'), 'ABC-123'); + expect(evaluate('', 'abc1234'), 'ABC-1234'); + }); + }); +} diff --git a/test/src/formatters/real_input_formatter_test.dart b/test/src/formatters/real_input_formatter_test.dart new file mode 100644 index 0000000..835bb73 --- /dev/null +++ b/test/src/formatters/real_input_formatter_test.dart @@ -0,0 +1,85 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue, [bool moeda = false]) { + return RealInputFormatter(moeda: moeda) + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('RealInputFormatter', () { + test('padrao', + () => expect(evaluate('', '111222333444'), '111.222.333.444')); + test( + 'padrao [moeda: true]', + () => expect(evaluate('', '111222333444', true), 'R\$ 111.222.333.444'), + ); + test('limite 12 digitos', () => expect(evaluate('', '9111222333444'), '')); + test( + 'limite 12 digitos [moeda: true]', + () => expect(evaluate('', '9111222333444', true), ''), + ); + + test('backspace', () { + expect(evaluate('', '12345678900'), '12.345.678.900'); + expect(evaluate('', '1234567890'), '1.234.567.890'); + expect(evaluate('', '123456789'), '123.456.789'); + expect(evaluate('', '12345678'), '12.345.678'); + expect(evaluate('', '1234567'), '1.234.567'); + expect(evaluate('', '123456'), '123.456'); + expect(evaluate('', '12345'), '12.345'); + expect(evaluate('', '1234'), '1.234'); + expect(evaluate('', '123'), '123'); + expect(evaluate('', '12'), '12'); + expect(evaluate('', '1'), '1'); + expect(evaluate('', ''), ''); + }); + + test('backspace [moeda: true]', () { + expect(evaluate('', '12345678900', true), 'R\$ 12.345.678.900'); + expect(evaluate('', '1234567890', true), 'R\$ 1.234.567.890'); + expect(evaluate('', '123456789', true), 'R\$ 123.456.789'); + expect(evaluate('', '12345678', true), 'R\$ 12.345.678'); + expect(evaluate('', '1234567', true), 'R\$ 1.234.567'); + expect(evaluate('', '123456', true), 'R\$ 123.456'); + expect(evaluate('', '12345', true), 'R\$ 12.345'); + expect(evaluate('', '1234', true), 'R\$ 1.234'); + expect(evaluate('', '123', true), 'R\$ 123'); + expect(evaluate('', '12', true), 'R\$ 12'); + expect(evaluate('', '1', true), 'R\$ 1'); + expect(evaluate('', '', true), ''); + }); + + test('digitacao', () { + expect(evaluate('', '1'), '1'); + expect(evaluate('', '12'), '12'); + expect(evaluate('', '123'), '123'); + expect(evaluate('', '1234'), '1.234'); + expect(evaluate('', '12345'), '12.345'); + expect(evaluate('', '123456'), '123.456'); + expect(evaluate('', '1234567'), '1.234.567'); + expect(evaluate('', '12345678'), '12.345.678'); + expect(evaluate('', '123456789'), '123.456.789'); + expect(evaluate('', '1234567890'), '1.234.567.890'); + expect(evaluate('', '12345678900'), '12.345.678.900'); + }); + + test('digitacao [moeda: true]', () { + expect(evaluate('', '1', true), 'R\$ 1'); + expect(evaluate('', '12', true), 'R\$ 12'); + expect(evaluate('', '123', true), 'R\$ 123'); + expect(evaluate('', '1234', true), 'R\$ 1.234'); + expect(evaluate('', '12345', true), 'R\$ 12.345'); + expect(evaluate('', '123456', true), 'R\$ 123.456'); + expect(evaluate('', '1234567', true), 'R\$ 1.234.567'); + expect(evaluate('', '12345678', true), 'R\$ 12.345.678'); + expect(evaluate('', '123456789', true), 'R\$ 123.456.789'); + expect(evaluate('', '1234567890', true), 'R\$ 1.234.567.890'); + expect(evaluate('', '12345678900', true), 'R\$ 12.345.678.900'); + }); + }); +} diff --git a/test/src/formatters/telefone_input_formatter_test.dart b/test/src/formatters/telefone_input_formatter_test.dart new file mode 100644 index 0000000..cb8d7e6 --- /dev/null +++ b/test/src/formatters/telefone_input_formatter_test.dart @@ -0,0 +1,48 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return TelefoneInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('TelefoneInputFormatter', () { + test('padrao [9 digito]', + () => expect(evaluate('', '61987654321'), '(61) 98765-4321')); + test('padrao', () => expect(evaluate('', '6187654321'), '(61) 8765-4321')); + + test('limite 11 digitos', () => expect(evaluate('', '961987654321'), '')); + test('backspace', () { + expect(evaluate('', '6198765432'), '(61) 9876-5432'); + expect(evaluate('', '619876543'), '(61) 9876-543'); + expect(evaluate('', '61987654'), '(61) 9876-54'); + expect(evaluate('', '6198765'), '(61) 9876-5'); + expect(evaluate('', '619876'), '(61) 9876'); + expect(evaluate('', '61987'), '(61) 987'); + expect(evaluate('', '6198'), '(61) 98'); + expect(evaluate('', '619'), '(61) 9'); + expect(evaluate('', '61'), '(61'); + expect(evaluate('', '6'), '(6'); + expect(evaluate('', ''), ''); + }); + + test('digitacao', () { + expect(evaluate('', '6'), '(6'); + expect(evaluate('', '61'), '(61'); + expect(evaluate('', '619'), '(61) 9'); + expect(evaluate('', '6198'), '(61) 98'); + expect(evaluate('', '61987'), '(61) 987'); + expect(evaluate('', '619876'), '(61) 9876'); + expect(evaluate('', '6198765'), '(61) 9876-5'); + expect(evaluate('', '61987654'), '(61) 9876-54'); + expect(evaluate('', '619876543'), '(61) 9876-543'); + expect(evaluate('', '6198765432'), '(61) 9876-5432'); + expect(evaluate('', '61987654321'), '(61) 98765-4321'); + }); + }); +} diff --git a/test/src/formatters/temperatura_input_formatter_test.dart b/test/src/formatters/temperatura_input_formatter_test.dart new file mode 100644 index 0000000..73982b4 --- /dev/null +++ b/test/src/formatters/temperatura_input_formatter_test.dart @@ -0,0 +1,29 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue) { + return TemperaturaInputFormatter() + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('description', () { + test('padrao', () => expect(evaluate('', '246'), '24,6')); + test('limite 3 digitos', () => expect(evaluate('', '9246'), '')); + test('backspace', () { + expect(evaluate('', '24'), '2,4'); + expect(evaluate('', '2'), '2'); + expect(evaluate('', ''), ''); + }); + + test('digitacao', () { + expect(evaluate('', '2'), '2'); + expect(evaluate('', '24'), '2,4'); + expect(evaluate('', '246'), '24,6'); + }); + }); +} diff --git a/test/src/formatters/validade_cartao_input_formatter_test.dart b/test/src/formatters/validade_cartao_input_formatter_test.dart new file mode 100644 index 0000000..e27a163 --- /dev/null +++ b/test/src/formatters/validade_cartao_input_formatter_test.dart @@ -0,0 +1,55 @@ +import 'package:brasil_fields/brasil_fields.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + evaluate(String oldValue, String newValue, [int maxLength = 6]) { + return ValidadeCartaoInputFormatter(maxLength: maxLength) + .formatEditUpdate( + TextEditingValue(text: oldValue), + TextEditingValue(text: newValue), + ) + .text; + } + + group('ValidadeCartaoInputFormatter', () { + test('padrao', () => expect(evaluate('', '0928'), '09/28')); + test('padrao [maxLength: 6]', + () => expect(evaluate('', '092028', 6), '09/2028')); + + test('maxLength invalido', () { + expect(() => evaluate('', '', 7), throwsAssertionError); + }); + + test('backspace', () { + expect(evaluate('', '092'), '09/2'); + expect(evaluate('', '09'), '09'); + expect(evaluate('', '0'), '0'); + expect(evaluate('', ''), ''); + }); + + test('backspace [maxLength: 6]', () { + expect(evaluate('', '09202', 6), '09/202'); + expect(evaluate('', '0920', 6), '09/20'); + expect(evaluate('', '092', 6), '09/2'); + expect(evaluate('', '09', 6), '09'); + expect(evaluate('', '0', 6), '0'); + expect(evaluate('', '', 6), ''); + }); + + test('digitacao', () { + expect(evaluate('', '0'), '0'); + expect(evaluate('', '09'), '09'); + expect(evaluate('', '092'), '09/2'); + expect(evaluate('', '0929'), '09/29'); + }); + + test('digitacao [maxLength: 6]', () { + expect(evaluate('', '0', 6), '0'); + expect(evaluate('', '09', 6), '09'); + expect(evaluate('', '092', 6), '09/2'); + expect(evaluate('', '0920', 6), '09/20'); + expect(evaluate('', '09202', 6), '09/202'); + expect(evaluate('', '092029', 6), '09/2029'); + }); + }); +}