pass TextEditingValue with selection parameter

This commit is contained in:
sneurlax 2023-01-27 11:12:05 -06:00
parent 2a6b46f761
commit 052135a32f

View file

@ -1357,30 +1357,29 @@ class NumericalRangeFormatter extends TextInputFormatter {
TextEditingValue oldValue, TextEditingValue oldValue,
TextEditingValue newValue, TextEditingValue newValue,
) { ) {
final TextSelection newSelection = newValue.selection;
String newVal = newValue.text;
if (newValue.text == '') { if (newValue.text == '') {
return newValue; return newValue;
} else { } else {
if (_BuyFormState.buyWithFiat) { if (_BuyFormState.buyWithFiat) {
if (Decimal.parse(newValue.text) < _BuyFormState.minFiat) { if (Decimal.parse(newValue.text) < _BuyFormState.minFiat) {
newValue = const TextEditingValue() newVal = _BuyFormState.minFiat.toStringAsFixed(2);
.copyWith(text: _BuyFormState.minFiat.toStringAsFixed(2)); // _BuyFormState._buyAmountController.selection =
} else { // TextSelection.collapsed(
newValue = Decimal.parse(newValue.text) > _BuyFormState.maxFiat // offset: _BuyFormState.buyWithFiat
? const TextEditingValue() // ? _BuyFormState._buyAmountController.text.length - 2
.copyWith(text: _BuyFormState.maxFiat.toStringAsFixed(2)) // : _BuyFormState._buyAmountController.text.length - 8);
: newValue; } else if (Decimal.parse(newValue.text) > _BuyFormState.maxFiat) {
newVal = _BuyFormState.maxFiat.toStringAsFixed(2);
} }
} else if (!_BuyFormState.buyWithFiat && } else if (!_BuyFormState.buyWithFiat &&
_BuyFormState.selectedCrypto?.ticker == _BuyFormState.selectedCrypto?.ticker ==
_BuyFormState.boundedCryptoTicker) { _BuyFormState.boundedCryptoTicker) {
if (Decimal.parse(newValue.text) < _BuyFormState.maxCrypto) { if (Decimal.parse(newValue.text) < _BuyFormState.minCrypto) {
newValue = const TextEditingValue() newVal = _BuyFormState.minCrypto.toStringAsFixed(8);
.copyWith(text: _BuyFormState.minCrypto.toStringAsFixed(8)); } else if (Decimal.parse(newValue.text) > _BuyFormState.maxCrypto) {
} else { newVal = _BuyFormState.maxCrypto.toStringAsFixed(8);
newValue = Decimal.parse(newValue.text) > _BuyFormState.maxCrypto
? const TextEditingValue()
.copyWith(text: _BuyFormState.maxCrypto.toStringAsFixed(8))
: newValue;
} }
} }
} }
@ -1390,6 +1389,8 @@ class NumericalRangeFormatter extends TextInputFormatter {
: r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$'; : r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$';
// return RegExp(r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$') // return RegExp(r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$')
return RegExp(regexString).hasMatch(newValue.text) ? newValue : oldValue; return RegExp(regexString).hasMatch(newValue.text)
? TextEditingValue(text: newVal, selection: newSelection)
: oldValue;
} }
} }