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,27 +62,23 @@ 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 // insert group separator
final regex = RegExp(r'\B(?=(\d{3})+(?!\d))'); final regex = RegExp(r'\B(?=(\d{3})+(?!\d))');
newString = newText.replaceAllMapped(
String newString = newText.replaceAllMapped(
regex, regex,
(m) => "${m.group(0)}${numberSymbols?.GROUP_SEP ?? ","}", (m) => "${m.group(0)}${numberSymbols?.GROUP_SEP ?? ","}",
); );
}
if (fraction != null) { if (fraction != null) {
newString += decimalSeparator; newString += decimalSeparator;