fix: amount input formatter case when trying to input an amount less than one starting with a zero instead of the decimal separator, edge case allowing more decimal places than the coin has, and pasting certain amounts

This commit is contained in:
julian 2023-06-23 12:59:43 -06:00
parent 2b228f736e
commit 3a9f567150

View file

@ -38,7 +38,15 @@ class AmountInputFormatter extends TextInputFormatter {
if (parts.length > 2) {
return oldValue;
}
final fractionDigits =
unit == null ? decimals : max(decimals - unit!.shift, 0);
if (newText.startsWith(decimalSeparator)) {
if (newText.length - 1 > fractionDigits) {
newText = newText.substring(0, fractionDigits + 1);
}
return TextEditingValue(
text: newText,
selection: TextSelection.collapsed(
@ -54,28 +62,24 @@ class AmountInputFormatter extends TextInputFormatter {
fraction = "";
}
final fractionDigits =
unit == null ? decimals : max(decimals - unit!.shift, 0);
if (fraction.length > fractionDigits) {
return oldValue;
fraction = fraction.substring(0, fractionDigits);
}
}
if (newText.trim() == '' || newText.trim() == '0') {
return newValue.copyWith(text: '');
} else if (BigInt.parse(newText) < BigInt.one) {
return newValue.copyWith(text: '');
String newString;
final val = BigInt.tryParse(newText);
if (val == null || val < BigInt.one) {
newString = newText;
} else {
// insert group separator
final regex = RegExp(r'\B(?=(\d{3})+(?!\d))');
newString = newText.replaceAllMapped(
regex,
(m) => "${m.group(0)}${numberSymbols?.GROUP_SEP ?? ","}",
);
}
// insert group separator
final regex = RegExp(r'\B(?=(\d{3})+(?!\d))');
String newString = newText.replaceAllMapped(
regex,
(m) => "${m.group(0)}${numberSymbols?.GROUP_SEP ?? ","}",
);
if (fraction != null) {
newString += decimalSeparator;
if (fraction.isNotEmpty) {