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
  • Loading branch information
santhoshvgts committed Jan 27, 2024
1 parent ae3a9d2 commit 5d8f41e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
12 changes: 11 additions & 1 deletion lib/form/utils/form_field_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ class AmountFormFieldController extends FormFieldController {
decimalDigits: currencyFormat!.decimalDigits,
symbol: currencyFormat!.symbol,
);

print(currencyFormat);
print(textEditingController.text);

String value = textEditingController.text.replaceAll(" ", "");
if (value.trim() == currencyFormat!.symbol) {
return "";
Expand All @@ -288,6 +292,12 @@ class AmountFormFieldController extends FormFieldController {
decimalDigits: currencyFormat!.decimalDigits,
symbol: currencyFormat!.symbol,
);

print(currencyFormat.toString());
print(formatter.currencySymbol);
print(formatter.toString());
print(double.parse(value));

try {
textEditingController.text = formatter.format(double.parse(value));
} catch (ex) {
Expand All @@ -304,7 +314,7 @@ class AmountFormFieldController extends FormFieldController {
TextInputType get textInputType => TextInputType.numberWithOptions(decimal: true);

@override
List<TextInputFormatter> get inputFormatter => [ CurrencyInputFormatter(maxDigits: 50) ];
List<TextInputFormatter> get inputFormatter => [ CurrencyInputFormatter(maxDigits: 50, currencyFormat: currencyFormat) ];

@override
bool get allowPaste => false;
Expand Down
13 changes: 8 additions & 5 deletions lib/form/utils/input_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:vgts_plugin/form/utils/number_currency_format.dart';

class InputFormatter {

Expand Down Expand Up @@ -88,8 +89,10 @@ class MaskedTextInputFormatter extends TextInputFormatter {

class CurrencyInputFormatter extends TextInputFormatter {

CurrencyInputFormatter({this.maxDigits = 10});
CurrencyInputFormatter({this.maxDigits = 10, this.currencyFormat});

final int maxDigits;
final NumberCurrencyFormat? currencyFormat;

@override
TextEditingValue formatEditUpdate(
Expand All @@ -104,10 +107,10 @@ class CurrencyInputFormatter extends TextInputFormatter {
final oldValueText = oldValue.text.replaceAll(RegExp(r'[^0-9]'), '');

NumberFormat formatter = NumberFormat.currency(
name: "INR",
locale: 'en_IN',
decimalDigits: 0,
symbol: '₹',
name: currencyFormat?.name ?? "INR",
locale: currencyFormat?.locale ?? 'en_IN',
decimalDigits: currencyFormat?.decimalDigits ?? 0,
symbol: currencyFormat?.symbol ?? '₹',
);

String newValueText = '';
Expand Down
4 changes: 4 additions & 0 deletions lib/form/utils/number_currency_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ class NumberCurrencyFormat {
return NumberCurrencyFormat("USD", "en-US", r"$", 0);
}

@override
String toString() {
return 'NumberCurrencyFormat{name: $name, locale: $locale, symbol: $symbol, decimalDigits: $decimalDigits}';
}
}

0 comments on commit 5d8f41e

Please sign in to comment.