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

Possibilidade de manter o valor zerado no campo, mais comum de ser vi… #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions lib/src/formatters/centavos_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import 'adiciona_separador.dart';
///
/// `casasDecimais` indica a quantidade de casas usadas.
class CentavosInputFormatter extends TextInputFormatter {
CentavosInputFormatter({this.moeda = false, this.casasDecimais = 2})
: assert(casasDecimais == 2 || casasDecimais == 3,
CentavosInputFormatter({
this.moeda = false,
this.casasDecimais = 2,
this.mostrarZerado = false,
}) : assert(casasDecimais == 2 || casasDecimais == 3,
'Quantidade de casas decimais deve ser 2 ou 3. Informado: $casasDecimais');

final bool moeda;
final int casasDecimais;
final bool mostrarZerado;

@override
TextEditingValue formatEditUpdate(
Expand Down Expand Up @@ -42,7 +46,7 @@ class CentavosInputFormatter extends TextInputFormatter {
}

// apaga o campo quando os valores foram zero.
if (numero == 0 && int.tryParse(centsValue) == 0) {
if (!mostrarZerado && numero == 0 && int.tryParse(centsValue) == 0) {
return const TextEditingValue(
text: "",
selection: TextSelection.collapsed(offset: 0),
Expand All @@ -63,8 +67,10 @@ class CentavosInputFormatter extends TextInputFormatter {
);
}

final minCentsValor = mostrarZerado ? 0 : 1;

// formata o número com 0, + centavos
if (numero > 0 && numero <= 9) {
if (numero >= minCentsValor && numero <= 9) {
if (casasDecimais == 3) {
centsValue = "00$numero";
} else {
Expand Down