From 0773391a2d39f6dc30f55e01d62e6439e9385ce3 Mon Sep 17 00:00:00 2001 From: Alaa Elattar Date: Wed, 28 May 2025 17:17:26 +0300 Subject: [PATCH 1/2] handle negative numbers validations && make validations onchange --- app/lib/screens/market/buy_tft.dart | 58 +++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/app/lib/screens/market/buy_tft.dart b/app/lib/screens/market/buy_tft.dart index 5e2a555b..7b80b3c1 100644 --- a/app/lib/screens/market/buy_tft.dart +++ b/app/lib/screens/market/buy_tft.dart @@ -115,7 +115,6 @@ class _BuyTFTWidgetState extends State { bool _validateAmount() { final amount = amountController.text.trim(); - amountError = null; if (loadingBalance) { setState(() { @@ -130,22 +129,50 @@ class _BuyTFTWidgetState extends State { }); return false; } - final balance = roundAmount(availableUSDC ?? '0'); - if (balance - Decimal.parse(amount) <= Decimal.zero) { + try { + final decimalAmount = Decimal.parse(amount); + + if (decimalAmount < Decimal.zero) { + setState(() { + amountError = 'Amount cannot be negative'; + }); + return false; + } + + if (decimalAmount == Decimal.zero) { + setState(() { + amountError = 'Amount must be greater than zero'; + }); + return false; + } + + final balance = roundAmount(availableUSDC ?? '0'); + + if (balance - decimalAmount <= Decimal.zero) { + setState(() { + amountError = 'Balance is not enough'; + }); + return false; + } + + if (decimalAmount > Decimal.parse(availableUSDC ?? '0')) { + setState(() { + amountError = 'Not enough balance'; + }); + return false; + } + setState(() { - amountError = 'Balance is not enough'; + amountError = null; }); - return false; - } - - if (Decimal.parse(amount) > Decimal.parse(availableUSDC ?? '0')) { + return true; + } catch (e) { setState(() { - amountError = 'Not enough balance'; + amountError = 'Invalid amount format'; }); return false; } - return true; } bool _validatePrice() { @@ -173,6 +200,7 @@ class _BuyTFTWidgetState extends State { (Decimal.fromInt(percentage).shift(-2)); amountController.text = roundAmount(amount.toString()).toString(); _calculateTotal(); + _validateAmount(); } void _calculateTotal() { @@ -437,6 +465,12 @@ class _BuyTFTWidgetState extends State { color: Theme.of(context).colorScheme.onSurface, ), focusNode: textFieldFocusNode, + onChanged: (_) { + setState(() { + amountError = null; + }); + _validateAmount(); + }, keyboardType: const TextInputType.numberWithOptions( decimal: true, signed: false), controller: amountController, @@ -638,7 +672,9 @@ class _BuyTFTWidgetState extends State { child: SizedBox( width: double.infinity, child: ElevatedButton( - onPressed: loading || loadingBalance + onPressed: loading || + loadingBalance || + priceError != null ? null : () async { if (_validateAmount() && _validatePrice()) { From de6ae65b85ecdfbeb66f954bf3c1c717c3bef953 Mon Sep 17 00:00:00 2001 From: Alaa Elattar Date: Mon, 2 Jun 2025 08:53:30 +0300 Subject: [PATCH 2/2] fix validations --- app/lib/screens/market/buy_tft.dart | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/app/lib/screens/market/buy_tft.dart b/app/lib/screens/market/buy_tft.dart index 7b80b3c1..a5376c5e 100644 --- a/app/lib/screens/market/buy_tft.dart +++ b/app/lib/screens/market/buy_tft.dart @@ -133,14 +133,7 @@ class _BuyTFTWidgetState extends State { try { final decimalAmount = Decimal.parse(amount); - if (decimalAmount < Decimal.zero) { - setState(() { - amountError = 'Amount cannot be negative'; - }); - return false; - } - - if (decimalAmount == Decimal.zero) { + if (decimalAmount <= Decimal.zero) { setState(() { amountError = 'Amount must be greater than zero'; }); @@ -156,13 +149,6 @@ class _BuyTFTWidgetState extends State { return false; } - if (decimalAmount > Decimal.parse(availableUSDC ?? '0')) { - setState(() { - amountError = 'Not enough balance'; - }); - return false; - } - setState(() { amountError = null; }); @@ -466,9 +452,6 @@ class _BuyTFTWidgetState extends State { ), focusNode: textFieldFocusNode, onChanged: (_) { - setState(() { - amountError = null; - }); _validateAmount(); }, keyboardType: const TextInputType.numberWithOptions(