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

Correção de bug na formatação de CPF + Criação de novo parâmetro #98

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 26 additions & 13 deletions lib/src/formatters/cpf_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,54 @@ import 'package:brasil_fields/src/interfaces/compoundable_formatter.dart';
import 'package:flutter/services.dart';

/// Formata o valor do campo com a mascara de CPF: `XXX.XXX.XXX-XX`
class CpfInputFormatter extends TextInputFormatter
implements CompoundableFormatter {
class CpfInputFormatter extends TextInputFormatter implements CompoundableFormatter {
final bool deleteSeparatorsAutomatically;
CpfInputFormatter({this.deleteSeparatorsAutomatically = true});

// Define o tamanho máximo do campo.
@override
int get maxLength => 11;
int get maxLength => 14;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
TextEditingValue oldValue, TextEditingValue newValue) {
// verifica o tamanho máximo do campo
if (newValue.text.length > maxLength) return oldValue;

var posicaoCursor = newValue.selection.end;
// valida o parâmetro que indica se deve remover os separadores automaticamente ao apagar um número
if (newValue.text.length < oldValue.text.length) {
if (deleteSeparatorsAutomatically == true) {
if (newValue.text.endsWith('.') || newValue.text.endsWith('-')) {
return newValue.copyWith(
text: newValue.text.substring(0, newValue.text.length - 1),
selection: TextSelection.collapsed(offset: newValue.selection.end - 1),
);
}
return newValue;
} else {
return newValue;
}
}

var substrIndex = 0;
final valorFinal = StringBuffer();

if (newValue.text.length >= 4) {
valorFinal.write('${newValue.text.substring(0, substrIndex = 3)}.');
if (newValue.selection.end >= 3) posicaoCursor++;
}
if (newValue.text.length >= 7) {
valorFinal.write('${newValue.text.substring(3, substrIndex = 6)}.');
if (newValue.selection.end >= 6) posicaoCursor++;
valorFinal.write('${newValue.text.substring(4, substrIndex = 7)}.');
}
if (newValue.text.length >= 10) {
valorFinal.write('${newValue.text.substring(6, substrIndex = 9)}-');
if (newValue.selection.end >= 9) posicaoCursor++;
if (newValue.text.length >= 11) {
valorFinal.write('${newValue.text.substring(8, substrIndex = 11)}-');
}
if (newValue.text.length >= substrIndex) {
valorFinal.write(newValue.text.substring(substrIndex));
valorFinal.write(newValue.text.substring(substrIndex).replaceAll('.', '').replaceAll('-', ''));
}

return TextEditingValue(
text: valorFinal.toString(),
selection: TextSelection.collapsed(offset: posicaoCursor),
selection: TextSelection.collapsed(offset: valorFinal.length),
);
}
}
15 changes: 15 additions & 0 deletions test/cpf_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:brasil_fields/src/formatters/cpf_input_formatter.dart';
import 'package:brasil_fields/src/validators/validators.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Expand Down Expand Up @@ -54,4 +56,17 @@ void main() {
test('Test CPF strip', () {
expect(CPFValidator.strip('334.616.710-02'), '33461671002');
});

test('Test CpfInputFormatter', () {
expect(
CpfInputFormatter().formatEditUpdate(
const TextEditingValue(text: '334.616.71'), // oldValue
const TextEditingValue(text: '334.616.710'), // newValue
),
const TextEditingValue(
text: '334.616.710-', // newValue formatted
selection: TextSelection.collapsed(offset: 12),
),
);
});
}
Loading