Skip to content

Commit

Permalink
* Updated AmountFormFieldController to support custom currency format
Browse files Browse the repository at this point in the history
* Updated intl version to ^0.18.1
* Added MaxLength props in NumberFormFieldController
* AmountFormFieldController Added Support for decimal digits
  • Loading branch information
santhoshvgts committed Feb 1, 2024
1 parent 32ee119 commit c68a88c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/form/utils/form_field_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ class AmountFormFieldController extends FormFieldController {
symbol: currencyFormat!.symbol,
);

print(currencyFormat);
print(textEditingController.text);
formatter.minimumFractionDigits = 0;
formatter.maximumFractionDigits = currencyFormat?.decimalDigits ?? 0;

String value = textEditingController.text.replaceAll(" ", "");
if (value.trim() == currencyFormat!.symbol) {
Expand All @@ -293,10 +293,8 @@ class AmountFormFieldController extends FormFieldController {
symbol: currencyFormat!.symbol,
);

print(currencyFormat.toString());
print(formatter.currencySymbol);
print(formatter.toString());
print(double.parse(value));
formatter.minimumFractionDigits = 0;
formatter.maximumFractionDigits = currencyFormat?.decimalDigits ?? 0;

try {
textEditingController.text = formatter.format(double.parse(value));
Expand Down
24 changes: 24 additions & 0 deletions lib/form/utils/input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ class CurrencyInputFormatter extends TextInputFormatter {
return oldValue;
}

print("New Value: $newValue");

final oldValueText = oldValue.text.replaceAll(RegExp(r'[^0-9]'), '');
// final oldValueText = oldValue.text.replaceAll(RegExp(r'[0-9-]{1}[0-9]*\.?[0-9]*$'), '');

print("oldValueText: $oldValueText");
print(oldValueText);

NumberFormat formatter = NumberFormat.currency(
name: currencyFormat?.name ?? "INR",
Expand All @@ -113,9 +119,13 @@ class CurrencyInputFormatter extends TextInputFormatter {
symbol: currencyFormat?.symbol ?? '₹',
);

formatter.minimumFractionDigits = 0;
formatter.maximumFractionDigits = currencyFormat?.decimalDigits ?? 0;

String newValueText = '';
try {
newValueText = formatter.parse(newValue.text).toString();
print("newValueText $newValueText");
} catch(ex) {
}

Expand All @@ -127,8 +137,22 @@ class CurrencyInputFormatter extends TextInputFormatter {
if (newValueText.isNotEmpty) {
double value = double.parse(newValueText);
newText = formatter.format(value);
if (newValue.text.endsWith('.')) {
newText = newText + '.';
} else if (newValue.text.endsWith(".0")) {
newText = newText + '.0';
}
// RegExp regex = RegExp(r'([.]*0)(?!.*\d)');
// newText = newText.replaceAll(regex, '');

}





print("newText $newText");

return newValue.copyWith(
text: newText,
selection: TextSelection.collapsed(offset: newText.length));
Expand Down

0 comments on commit c68a88c

Please sign in to comment.