Created
December 4, 2021 23:44
-
-
Save jacobaraujo7/b212b640a8553ad7916ebcc4b1532e56 to your computer and use it in GitHub Desktop.
// Teste de construção de um CNPJ e CPF Format sem view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CNPJAndCPFInputFormat extends TextInputFormatter { | |
@override | |
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) { | |
var value = _prepareValue(newValue); | |
if (value.isNotEmpty && value.length <= 11) { | |
value = _appendMask(value, '###.###.###-##'); | |
} else if (value.isNotEmpty && value.length <= 14) { | |
value = _appendMask(value, '##.###.###/####-##'); | |
} else if (value.length > 14) { | |
return oldValue; | |
} | |
return newValue.copyWith( | |
text: value, | |
selection: TextSelection.fromPosition( | |
TextPosition(offset: value.length), | |
), | |
); | |
} | |
String _prepareValue(TextEditingValue newValue) { | |
var value = newValue.text; | |
value = value.replaceAll(RegExp(r'[^\d]'), ''); | |
return value; | |
} | |
String _appendMask(String value, String mask) { | |
for (var i = 0; i < value.length; i++) { | |
mask = mask.replaceFirst('#', value[i]); | |
} | |
var lastHash = mask.indexOf('#'); | |
if (lastHash != -1) { | |
mask = mask.characters.getRange(0, lastHash).join(); | |
if (RegExp(r'[^\d]').hasMatch(mask.characters.last)) { | |
mask = mask.characters.getRange(0, mask.length - 1).join(); | |
} | |
} | |
return mask; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_playground/src/cnpj_and_cpf_input_format.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
void main() { | |
final formatter = CNPJAndCPFInputFormat(); | |
test('Should return empty when type letter', () { | |
var newValue3 = const TextEditingValue(text: 'jac'); | |
newValue3 = formatter.formatEditUpdate(newValue3, newValue3); | |
expect(newValue3.text, ''); | |
}); | |
group('CPF', () { | |
test('Should format CPF when has 3 digit', () { | |
var newValue3 = const TextEditingValue(text: '111'); | |
newValue3 = formatter.formatEditUpdate(newValue3, newValue3); | |
expect(newValue3.text, '111'); | |
}); | |
test('Should format CPF when has 4 digit', () { | |
const newValue4 = TextEditingValue(text: '1111'); | |
expect(formatter.formatEditUpdate(newValue4, newValue4).text, '111.1'); | |
}); | |
test('Should format CPF when has 6 digit', () { | |
const newValue7 = TextEditingValue(text: '111111'); | |
expect(formatter.formatEditUpdate(newValue7, newValue7).text, '111.111'); | |
}); | |
test('Should format CPF when has 7 digit', () { | |
const newValue7 = TextEditingValue(text: '1111111'); | |
expect(formatter.formatEditUpdate(newValue7, newValue7).text, '111.111.1'); | |
}); | |
test('Should format CPF when has 10 digit', () { | |
const newValue10 = TextEditingValue(text: '111.111.1112'); | |
expect(formatter.formatEditUpdate(newValue10, newValue10).text, '111.111.111-2'); | |
}); | |
test('Should format CPF when has 11 digit', () { | |
const newValue11 = TextEditingValue(text: '111.111.11122'); | |
expect(formatter.formatEditUpdate(newValue11, newValue11).text, '111.111.111-22'); | |
}); | |
}); | |
group('CNPJ', () { | |
test('Should format CNPJ when has 12 digit', () { | |
const newValue11 = TextEditingValue(text: '111.111.110-001'); | |
expect(formatter.formatEditUpdate(newValue11, newValue11).text, '11.111.111/0001'); | |
}); | |
test('Should format CNPJ when has 14 digit', () { | |
const newValue11 = TextEditingValue(text: '11.111.111/000111'); | |
expect(formatter.formatEditUpdate(newValue11, newValue11).text, '11.111.111/0001-11'); | |
}); | |
}); | |
test('Should ignore if typed more less 14', () { | |
const oldValue11 = TextEditingValue(text: '11.111.111/0001-11'); | |
const newValue11 = TextEditingValue(text: '11.111.111/0001-111'); | |
expect(formatter.formatEditUpdate(oldValue11, newValue11).text, '11.111.111/0001-11'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment