From 74e176d15c156de7984401a86d09464c248a441b Mon Sep 17 00:00:00 2001 From: Lucas Moraes Date: Mon, 10 Jun 2024 14:55:00 -0300 Subject: [PATCH 1/4] =?UTF-8?q?Adicionado=20inputFormatter=20para=20certid?= =?UTF-8?q?=C3=A3o=20de=20nascimento=20+=20teste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cert_nascimento_input_formatter.dart | 55 +++++++++++++++++++ test/brasil_fields_test.dart | 42 +++++++------- 2 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 lib/src/formatters/cert_nascimento_input_formatter.dart diff --git a/lib/src/formatters/cert_nascimento_input_formatter.dart b/lib/src/formatters/cert_nascimento_input_formatter.dart new file mode 100644 index 0000000..31dcf77 --- /dev/null +++ b/lib/src/formatters/cert_nascimento_input_formatter.dart @@ -0,0 +1,55 @@ +import 'package:flutter/services.dart'; + +/// Formata o valor do campo com a mascara de Certidão de Nascimento: `XXXXXX XX XX XXXX X XXXXX XXX XXXXXXX XX` +class CertNascimentoInputFormatter extends TextInputFormatter { + @override + TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) { + // verifica o tamanho máximo do campo + if (newValue.text.length > 32) return oldValue; + + var posicaoCursor = newValue.selection.end; + var substrIndex = 0; + final valorFinal = StringBuffer(); + + if (newValue.text.length >= 7) { + valorFinal.write('${newValue.text.substring(0, substrIndex = 6)} '); + if (newValue.selection.end >= 6) posicaoCursor++; + } + if (newValue.text.length >= 9) { + valorFinal.write('${newValue.text.substring(6, substrIndex = 8)} '); + if (newValue.selection.end >= 8) posicaoCursor++; + } + if (newValue.text.length >= 11) { + valorFinal.write('${newValue.text.substring(8, substrIndex = 10)} '); + if (newValue.selection.end >= 10) posicaoCursor++; + } + if (newValue.text.length >= 15) { + valorFinal.write('${newValue.text.substring(10, substrIndex = 14)} '); + if (newValue.selection.end >= 14) posicaoCursor++; + } + if (newValue.text.length >= 16) { + valorFinal.write('${newValue.text.substring(14, substrIndex = 15)} '); + if (newValue.selection.end >= 15) posicaoCursor++; + } + if (newValue.text.length >= 21) { + valorFinal.write('${newValue.text.substring(15, substrIndex = 20)} '); + if (newValue.selection.end >= 20) posicaoCursor++; + } + if (newValue.text.length >= 24) { + valorFinal.write('${newValue.text.substring(20, substrIndex = 23)} '); + if (newValue.selection.end >= 23) posicaoCursor++; + } + if (newValue.text.length >= 31) { + valorFinal.write('${newValue.text.substring(23, substrIndex = 30)} '); + if (newValue.selection.end >= 30) posicaoCursor++; + } + if (newValue.text.length >= substrIndex) { + valorFinal.write(newValue.text.substring(substrIndex)); + } + + return TextEditingValue( + text: valorFinal.toString(), + selection: TextSelection.collapsed(offset: posicaoCursor), + ); + } +} diff --git a/test/brasil_fields_test.dart b/test/brasil_fields_test.dart index 02d7725..2b6465a 100644 --- a/test/brasil_fields_test.dart +++ b/test/brasil_fields_test.dart @@ -1,4 +1,5 @@ import 'package:brasil_fields/brasil_fields.dart'; +import 'package:brasil_fields/src/formatters/cert_nascimento_input_formatter.dart'; import 'package:brasil_fields/src/formatters/compound_formatters/compound_formatter.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -46,14 +47,12 @@ void main() { testWidgets('TelefoneInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester - .pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); + await tester.pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '9912345678'); expect(textController.text, '(99) 1234-5678'); - await tester - .pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); + await tester.pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '00987654321'); expect(textController.text, '(00) 98765-4321'); @@ -69,8 +68,7 @@ void main() { await tester.enterText(find.byType(TextField), '12345678'); expect(textController.text, '12.345-678'); - await tester.pumpWidget( - boilerplate(CepInputFormatter(ponto: false), textController)); + await tester.pumpWidget(boilerplate(CepInputFormatter(ponto: false), textController)); await tester.enterText(find.byType(TextField), '12345678'); expect(textController.text, '12345-678'); @@ -103,8 +101,7 @@ void main() { testWidgets('RealInputFormatter + moeda', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget( - boilerplate(RealInputFormatter(moeda: true), textController)); + await tester.pumpWidget(boilerplate(RealInputFormatter(moeda: true), textController)); await tester.enterText(find.byType(TextField), '1256780'); expect(textController.text, 'R\$ 1.256.780'); @@ -127,8 +124,7 @@ void main() { testWidgets('CentavosInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester - .pumpWidget(boilerplate(CentavosInputFormatter(), textController)); + await tester.pumpWidget(boilerplate(CentavosInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '12567'); expect(textController.text, '125,67'); @@ -146,8 +142,7 @@ void main() { testWidgets('CentavosInputFormatter 3 decimais', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget( - boilerplate(CentavosInputFormatter(casasDecimais: 3), textController)); + await tester.pumpWidget(boilerplate(CentavosInputFormatter(casasDecimais: 3), textController)); await tester.enterText(find.byType(TextField), '125678'); expect(textController.text, '125,678'); @@ -173,6 +168,14 @@ void main() { expect(textController.text, '01/01/1900'); }); + testWidgets('CertNascimentoFormatter', (WidgetTester tester) async { + final textController = TextEditingController(); + await tester.pumpWidget(boilerplate(CertNascimentoInputFormatter(), textController)); + + await tester.enterText(find.byType(TextField), '11111122334444566666777888888899'); + expect(textController.text, '111111 22 33 4444 5 66666 777 8888888 99'); + }); + testWidgets('HoraInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); await tester.pumpWidget(boilerplate(HoraInputFormatter(), textController)); @@ -183,16 +186,14 @@ void main() { testWidgets('CartaoBancarioInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget( - boilerplate(CartaoBancarioInputFormatter(), textController)); + await tester.pumpWidget(boilerplate(CartaoBancarioInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '4040121298987373'); expect(textController.text, '4040 1212 9898 7373'); }); testWidgets('ValidadeCartaoInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget( - boilerplate(ValidadeCartaoInputFormatter(), textController)); + await tester.pumpWidget(boilerplate(ValidadeCartaoInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '1223'); expect(textController.text, '12/23'); @@ -200,8 +201,7 @@ void main() { testWidgets('TemperaturaInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester - .pumpWidget(boilerplate(TemperaturaInputFormatter(), textController)); + await tester.pumpWidget(boilerplate(TemperaturaInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '176'); expect(textController.text, '17,6'); @@ -223,13 +223,11 @@ void main() { final textController = TextEditingController(); // testa toUpperCase - await tester.pumpWidget( - boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); + await tester.pumpWidget(boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); await tester.enterText(find.byType(TextField), 'abc'); expect(textController.text, 'ABC'); - await tester.pumpWidget( - boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); + await tester.pumpWidget(boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); await tester.enterText(find.byType(TextField), 'abc-1234'); expect(textController.text, 'ABC-1234'); }); From 1a7ef8c9def62bcb857aa2b05491b614fd7b0d8e Mon Sep 17 00:00:00 2001 From: Lucas Mariano <120429072+lucas-marianno@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:32:17 -0300 Subject: [PATCH 2/4] Update brasil_fields_test.dart --- test/brasil_fields_test.dart | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/test/brasil_fields_test.dart b/test/brasil_fields_test.dart index 2b6465a..8cc8ff2 100644 --- a/test/brasil_fields_test.dart +++ b/test/brasil_fields_test.dart @@ -47,12 +47,14 @@ void main() { testWidgets('TelefoneInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); + await tester + .pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '9912345678'); expect(textController.text, '(99) 1234-5678'); - await tester.pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); + await tester + .pumpWidget(boilerplate(TelefoneInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '00987654321'); expect(textController.text, '(00) 98765-4321'); @@ -68,7 +70,8 @@ void main() { await tester.enterText(find.byType(TextField), '12345678'); expect(textController.text, '12.345-678'); - await tester.pumpWidget(boilerplate(CepInputFormatter(ponto: false), textController)); + await tester.pumpWidget( + boilerplate(CepInputFormatter(ponto: false), textController)); await tester.enterText(find.byType(TextField), '12345678'); expect(textController.text, '12345-678'); @@ -101,7 +104,8 @@ void main() { testWidgets('RealInputFormatter + moeda', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(RealInputFormatter(moeda: true), textController)); + await tester.pumpWidget( + boilerplate(RealInputFormatter(moeda: true), textController)); await tester.enterText(find.byType(TextField), '1256780'); expect(textController.text, 'R\$ 1.256.780'); @@ -124,7 +128,8 @@ void main() { testWidgets('CentavosInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(CentavosInputFormatter(), textController)); + await tester + .pumpWidget(boilerplate(CentavosInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '12567'); expect(textController.text, '125,67'); @@ -142,7 +147,8 @@ void main() { testWidgets('CentavosInputFormatter 3 decimais', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(CentavosInputFormatter(casasDecimais: 3), textController)); + await tester.pumpWidget( + boilerplate(CentavosInputFormatter(casasDecimais: 3), textController)); await tester.enterText(find.byType(TextField), '125678'); expect(textController.text, '125,678'); @@ -175,7 +181,7 @@ void main() { await tester.enterText(find.byType(TextField), '11111122334444566666777888888899'); expect(textController.text, '111111 22 33 4444 5 66666 777 8888888 99'); }); - + testWidgets('HoraInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); await tester.pumpWidget(boilerplate(HoraInputFormatter(), textController)); @@ -186,14 +192,16 @@ void main() { testWidgets('CartaoBancarioInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(CartaoBancarioInputFormatter(), textController)); + await tester.pumpWidget( + boilerplate(CartaoBancarioInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '4040121298987373'); expect(textController.text, '4040 1212 9898 7373'); }); testWidgets('ValidadeCartaoInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(ValidadeCartaoInputFormatter(), textController)); + await tester.pumpWidget( + boilerplate(ValidadeCartaoInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '1223'); expect(textController.text, '12/23'); @@ -201,7 +209,8 @@ void main() { testWidgets('TemperaturaInputFormatter', (WidgetTester tester) async { final textController = TextEditingController(); - await tester.pumpWidget(boilerplate(TemperaturaInputFormatter(), textController)); + await tester + .pumpWidget(boilerplate(TemperaturaInputFormatter(), textController)); await tester.enterText(find.byType(TextField), '176'); expect(textController.text, '17,6'); @@ -223,11 +232,13 @@ void main() { final textController = TextEditingController(); // testa toUpperCase - await tester.pumpWidget(boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); + await tester.pumpWidget( + boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); await tester.enterText(find.byType(TextField), 'abc'); expect(textController.text, 'ABC'); - await tester.pumpWidget(boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); + await tester.pumpWidget( + boilerplatePlacaVeiculo(PlacaVeiculoInputFormatter(), textController)); await tester.enterText(find.byType(TextField), 'abc-1234'); expect(textController.text, 'ABC-1234'); }); From e217e2f5ea5a6cfc8cf4b5f52ed3e33a32ddc3e9 Mon Sep 17 00:00:00 2001 From: Lucas Mariano <120429072+lucas-marianno@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:33:07 -0300 Subject: [PATCH 3/4] Update brasil_fields_test.dart --- test/brasil_fields_test.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/test/brasil_fields_test.dart b/test/brasil_fields_test.dart index 8cc8ff2..d011500 100644 --- a/test/brasil_fields_test.dart +++ b/test/brasil_fields_test.dart @@ -1,5 +1,4 @@ import 'package:brasil_fields/brasil_fields.dart'; -import 'package:brasil_fields/src/formatters/cert_nascimento_input_formatter.dart'; import 'package:brasil_fields/src/formatters/compound_formatters/compound_formatter.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; From 80982a1f945e1d8d42441335db279a66682bf5a6 Mon Sep 17 00:00:00 2001 From: Lucas Mariano <120429072+lucas-marianno@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:36:06 -0300 Subject: [PATCH 4/4] Update brasil_fields.dart --- lib/brasil_fields.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/brasil_fields.dart b/lib/brasil_fields.dart index c83c4fc..9fb42dc 100644 --- a/lib/brasil_fields.dart +++ b/lib/brasil_fields.dart @@ -5,6 +5,7 @@ export 'src/formatters/cartao_bancario_input_formatter.dart'; export 'src/formatters/centavos_input_formatter.dart'; export 'src/formatters/cep_input_formatter.dart'; export 'src/formatters/cest_input_formatter.dart'; +export 'src/formatters/cert_nascimento_input_formatter.dart'; export 'src/formatters/cnpj_input_formatter.dart'; export 'src/formatters/cns_formatter.dart'; export 'src/formatters/compound_formatters/cpf_ou_cpnj_formatter.dart';