Skip to content

Commit

Permalink
ncm e cest formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensdemelo committed Apr 19, 2023
1 parent 57c81dd commit 3bc8fa8
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.12.0
- Novos formatters: `CESTInputFormatter()` e `NCMInputFormatter()`, por [juliogyn](https://github.com/juliogyn).

## 1.11.0
- Novo formatter: `IOFInputFormatter()`.

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ Para inicializar um `TextEditingController` com o texto já formatado, basta esc
final dataController = TextEditingController(text: UtilData.obterDataDDMMAAAA(DateTime(2020, 12, 31)));
final cnpjController = TextEditingController(text: UtilBrasilFields.obterCnpj('11222333444455'));
```

---

<a href="https://github.com/flutterbootcamp/brasil_fields/graphs/contributors">
<img src="https://contrib.rocks/image?repo=flutterbootcamp/brasil_fields" />
</a>

Made with [contrib.rocks](https://contrib.rocks).
8 changes: 8 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ class MyApp extends StatelessWidget {
label: 'IOF',
formatter: IOFInputFormatter(),
),
RowFormatters(
label: 'NCM',
formatter: NCMInputFormatter(),
),
RowFormatters(
label: 'CEST',
formatter: CESTInputFormatter(),
),
],
),
const Text('Em breve'),
Expand Down
9 changes: 5 additions & 4 deletions example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXAggregateTarget section */
Expand Down Expand Up @@ -235,6 +235,7 @@
/* Begin PBXShellScriptBuildPhase section */
3399D490228B24CF009A79C7 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -344,7 +345,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down Expand Up @@ -423,7 +424,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -470,7 +471,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
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.11.0"
version: "1.12.0"
characters:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions lib/brasil_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ export 'src/formatters/altura_input_formatter.dart';
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/cnpj_input_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';
export 'src/formatters/hora_input_formatter.dart';
export 'src/formatters/iof_input_formatter.dart';
export 'src/formatters/km_input_formatter.dart';
export 'src/formatters/ncm_input_formatter.dart';
export 'src/formatters/peso_input_formatter.dart';
export 'src/formatters/placa_veiculo_formatter.dart';
export 'src/formatters/real_input_formatter.dart';
Expand Down
42 changes: 42 additions & 0 deletions lib/src/formatters/cest_input_formatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:brasil_fields/src/interfaces/compoundable_formatter.dart';
import 'package:flutter/services.dart';

/// Formata o valor do campo com a máscara CEST `XX.XXX.XX`.
class CESTInputFormatter extends TextInputFormatter
implements CompoundableFormatter {
// Define o tamanho máximo do campo.
@override
int get maxLength => 7;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
final newValueLength = newValue.text.length;

if (newValueLength > maxLength) {
return oldValue;
}

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

if (newValueLength >= 3) {
newText.write('${newValue.text.substring(0, substrIndex = 2)}.');
if (newValue.selection.end >= 2) selectionIndex++;
}
if (newValueLength >= 6) {
newText.write('${newValue.text.substring(2, substrIndex = 5)}.');
if (newValue.selection.end >= 5) 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 lib/src/formatters/iof_input_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/services.dart';

/// Formata o valor do campo com a máscara `9,99999`.
/// Formata o valor do campo com a máscara `9,99999`
class IOFInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
Expand Down
41 changes: 41 additions & 0 deletions lib/src/formatters/ncm_input_formatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:brasil_fields/src/interfaces/compoundable_formatter.dart';
import 'package:flutter/services.dart';

/// Formata o valor do campo com a máscara de NCM: `XXXX.XX.XX`
class NCMInputFormatter extends TextInputFormatter
implements CompoundableFormatter {
@override
int get maxLength => 8;

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
final newValueLength = newValue.text.length;

if (newValueLength > maxLength) {
return oldValue;
}

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

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

if (newValueLength >= substrIndex) {
newText.write(newValue.text.substring(substrIndex));
}

return TextEditingValue(
text: newText.toString(),
selection: TextSelection.collapsed(offset: selectionIndex),
);
}
}
2 changes: 1 addition & 1 deletion 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.11.0
version: 1.12.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 Down
16 changes: 16 additions & 0 deletions test/brasil_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,20 @@ void main() {
await tester.enterText(find.byType(TextField), '12345678');
expect(textController.text, '1,234567');
});

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

await tester.pumpWidget(boilerplate(NCMInputFormatter(), textController));
await tester.enterText(find.byType(TextField), '03099000');
expect(textController.text, '0309.90.00');
});

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

await tester.pumpWidget(boilerplate(CESTInputFormatter(), textController));
await tester.enterText(find.byType(TextField), '0101800');
expect(textController.text, '01.018.00');
});
}

0 comments on commit 3bc8fa8

Please sign in to comment.