Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Certidão de Nasicmento Modelo Novo + teste #99

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/brasil_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
55 changes: 55 additions & 0 deletions lib/src/formatters/cert_nascimento_input_formatter.dart
Original file line number Diff line number Diff line change
@@ -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),
);
}
}
8 changes: 8 additions & 0 deletions test/brasil_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading