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'; 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..d011500 100644 --- a/test/brasil_fields_test.dart +++ b/test/brasil_fields_test.dart @@ -173,6 +173,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));