Skip to content

Commit

Permalink
CNSInputFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensdemelo committed Aug 12, 2023
1 parent 483d4e8 commit 4c8fe3f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.14.0
- Novo formatter: `CNSInputFormatter()`

## 1.13.1
- intl: 0.18.0.

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ Este package facilita o desenvolvimento de projetos que utilizam campos com os p
- Cartão bancário (0000 1111 2222 3333 4444)
- Centavos (R\$) (7,19) (até 3 casas decimais: 7,191)
- CEP (99.999-999)
- CNPJ (99.999.999/9999-99)
- CPF (999.999.99-99)
- CNPJ (99.999.999/9999-99)
- Cpf ou Cnpj (se adapta conforme os números são inseridos)
- CNS (111 2222 3333 4444)
- CEST (12.345.67)
- Data (01/01/1900)
- Hora (23:59)
- IOF (1,234567)
- KM (999.999)
- NCM (1234.56.78)
- Peso (111,1)
- Placa de veículo (AAA-1234)
- Real (R\$) (20.550)
- Telefone ( (99) 9999-9999)
- Validade de cartão bancário (12/24 ou 12/2024)
- Temperatura (27,1)
- IOF (1,234567)

### Padrões

Expand Down Expand Up @@ -59,18 +62,21 @@ TextFormField(
- `CartaoBancarioInputFormatter()`
- `CentavosInputFormatter()`
- `CepInputFormatter()`
- `CnpjInputFormatter()`
- `CpfInputFormatter()`
- `CnpjInputFormatter()`
- `CpfOuCnpjFormatter()`
- `CESTInputFormatter()`
- `CNSInputFormatter()`
- `DataInputFormatter()`
- `HoraInputFormatter()`
- `IOFInputFormatter()`
- `KmInputFormatter()`
- `NCMInputFormatter()`
- `PesoInputFormatter()`
- `PlacaVeiculoInputFormatter()` (**atenção**: nao utilizar `FilteringTextInputFormatter.digitsOnly`)
- `RealInputFormatter()`
- `TelefoneInputFormatter()`
- `ValidadeCartaoInputFormatter()`
- `IOFInputFormatter()`

Caso precise de um DropdownButton com algumas das classes de padrões:

Expand Down
4 changes: 2 additions & 2 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/rubensmelo/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/rubensmelo/git/brasil_fields/example"
export "FLUTTER_ROOT=/Users/rubens/git/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/rubens/git/brasil_fields/example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
Expand Down
4 changes: 4 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class MyApp extends StatelessWidget {
label: 'CEST',
formatter: CESTInputFormatter(),
),
RowFormatters(
label: 'CNS',
formatter: CNSInputFormatter(),
),
],
),
const Text('Em breve'),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.13.1"
version: "1.14.0"
characters:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions lib/brasil_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export 'src/formatters/centavos_input_formatter.dart';
export 'src/formatters/cep_input_formatter.dart';
export 'src/formatters/cest_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';
export 'src/formatters/cpf_input_formatter.dart';
export 'src/formatters/data_input_formatter.dart';
Expand Down
40 changes: 40 additions & 0 deletions lib/src/formatters/cns_formatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/services.dart';

/// Formata o valor do campo com a máscara `000 1111 2222 3333`.
class CNSInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
final newValueLength = newValue.text.length;

// Verifica o tamanho máximo do campo.
if (newValueLength > 15) {
return oldValue;
}

var selectionIndex = newValue.selection.end;
var substrIndex = 0;
final newText = StringBuffer();

if (newValueLength >= 3) {
newText.write('${newValue.text.substring(0, substrIndex = 3)} ');
if (newValue.selection.end >= 4) selectionIndex++;
}
if (newValueLength >= 7) {
newText.write('${newValue.text.substring(3, substrIndex = 7)} ');
if (newValue.selection.end >= 8) selectionIndex++;
}
if (newValueLength >= 11) {
newText.write('${newValue.text.substring(7, substrIndex = 11)} ');
if (newValue.selection.end >= 12) selectionIndex++;
}
if (newValueLength >= substrIndex) {
newText.write(newValue.text.substring(substrIndex));
}

return TextEditingValue(
text: newText.toString(),
selection: TextSelection.collapsed(offset: selectionIndex),
);
}
}
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: brasil_fields
description: O jeito mais fácil de utilizar padrões e formatos brasileiros em seu projeto Dart.
version: 1.13.1
version: 1.14.0
homepage: https://github.com/flutterbootcamp/brasil_fields
repository: https://github.com/flutterbootcamp/brasil_fields
issue_tracker: https://github.com/flutterbootcamp/brasil_fields/issues
Expand All @@ -13,7 +13,6 @@ dependencies:
sdk: flutter
intl: ^0.18.0


dev_dependencies:
flutter_lints: ^2.0.0
flutter_test:
Expand Down
8 changes: 8 additions & 0 deletions test/brasil_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,12 @@ void main() {
await tester.enterText(find.byType(TextField), '0101800');
expect(textController.text, '01.018.00');
});

testWidgets('CNSInputFormatter', (WidgetTester tester) async {
final textController = TextEditingController();

await tester.pumpWidget(boilerplate(CNSInputFormatter(), textController));
await tester.enterText(find.byType(TextField), '404012129898737');
expect(textController.text, '404 0121 2989 8737');
});
}

0 comments on commit 4c8fe3f

Please sign in to comment.