Reputation: 520
I have an exchange rate application in flutter, but I want to make the currency format the same as below, how can I do it?
My Code :
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import '../Utils.dart';
class CurrencyInputFormatter extends TextInputFormatter {
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.selection.baseOffset == 0) {
print(true);
return newValue;
}
double value = double.parse(newValue.text);
final formatter = NumberFormat("#,##0", "tr-TR");
String newText = formatter.format(value);
print(newText);
return newValue.copyWith(
text: newText,
selection: new TextSelection.collapsed(offset: newText.length));
}
}
I want to do
Upvotes: 2
Views: 2457
Reputation: 520
library currency_text_input_formatter;
import 'dart:math';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
/// The `symbol` argument is used to symbol of NumberFormat.
/// Put '\$' for symbol
///
/// The `locale` argument is used to locale of NumberFormat.
/// Put 'en' or 'es' for locale
///
/// The `decimalDigits` argument is used to decimalDigits of NumberFormat.
/// Defaults `decimalDigits` is 2.
class CurrencyTextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
if (newValue.text.isEmpty) {
return newValue.copyWith(text: '');
} else if (newValue.text == ".") {
return newValue.copyWith(
text: '0.00',
selection: TextSelection.collapsed(offset: 2),
);
} else {
NumberFormat f = NumberFormat("#,###,###.00", "en_US");
double newNumber = 0;
if ((!newValue.text.contains(".")) && oldValue.text.contains('.')) {
String tempString = newValue.text.replaceAll(f.symbols.GROUP_SEP, '');
tempString = tempString.substring(0, tempString.length - 2) +
"." +
tempString.substring(tempString.length - 2);
newNumber = double.parse(tempString);
} else {
newNumber = double.parse(newValue.text
.replaceAll(f.symbols.GROUP_SEP, '')
.replaceAll("..", '.'));
}
String newString = f.format(newNumber);
int cursorPosition = 0;
if (oldValue.text.length > newString.length) {
cursorPosition = -1;
} else if (oldValue.text.length < newString.length) {
cursorPosition = 1;
} else {
if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length >
newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
cursorPosition = -1;
if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
newString = "";
}
} else if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length <
newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
cursorPosition = 1;
} else if (oldValue.selection.extentOffset >
oldValue.selection.baseOffset) {
cursorPosition =
oldValue.selection.baseOffset - oldValue.selection.extentOffset;
newString =
newString.substring(0, oldValue.selection.baseOffset - 1) +
newString.substring(oldValue.selection.baseOffset + 1);
newNumber = double.parse(newString
.replaceAll(f.symbols.GROUP_SEP, '')
.replaceAll("..", '.'));
newString = f.format(newNumber);
if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
newString = "";
}
}
}
return TextEditingValue(
text: newString,
selection: TextSelection.collapsed(
offset: oldValue.selection.extent.offset +
cursorPosition +
(f.symbols.GROUP_SEP.allMatches(newString).length -
f.symbols.GROUP_SEP.allMatches(oldValue.text).length)),
);
}
}
}
Upvotes: 1
Reputation: 17746
Intl actually provides you a way to do so.
import 'package:intl/intl.dart';
final moneyText = 1000;
NumberFormat.simpleCurrency().format(moneyText);
If you are already using translation, you should have intl already.
Upvotes: 3
Reputation: 3136
You can achieve this with flutter_masked_text package it can do exactly the same.
Upvotes: 1