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) { if (parts.length > 2) {
return oldValue; return oldValue;
} }
final fractionDigits =
unit == null ? decimals : max(decimals - unit!.shift, 0);
if (newText.startsWith(decimalSeparator)) { if (newText.startsWith(decimalSeparator)) {
if (newText.length - 1 > fractionDigits) {
newText = newText.substring(0, fractionDigits + 1);
}
return TextEditingValue( return TextEditingValue(
text: newText, text: newText,
selection: TextSelection.collapsed( selection: TextSelection.collapsed(
@ -54,28 +62,24 @@ class AmountInputFormatter extends TextInputFormatter {
fraction = ""; fraction = "";
} }
final fractionDigits =
unit == null ? decimals : max(decimals - unit!.shift, 0);
if (fraction.length > fractionDigits) { if (fraction.length > fractionDigits) {
return oldValue; fraction = fraction.substring(0, fractionDigits);
} }
} }
if (newText.trim() == '' || newText.trim() == '0') { String newString;
return newValue.copyWith(text: ''); final val = BigInt.tryParse(newText);
} else if (BigInt.parse(newText) < BigInt.one) { if (val == null || val < BigInt.one) {
return newValue.copyWith(text: ''); 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) { if (fraction != null) {
newString += decimalSeparator; newString += decimalSeparator;
if (fraction.isNotEmpty) { if (fraction.isNotEmpty) {