Skip to content

Commit 75f083b

Browse files
authored
Handle negative numbers validations && make validations onchange (#1067)
* handle negative numbers validations && make validations onchange * fix validations
1 parent bc63d62 commit 75f083b

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

app/lib/screens/market/buy_tft.dart

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
115115

116116
bool _validateAmount() {
117117
final amount = amountController.text.trim();
118-
amountError = null;
119118

120119
if (loadingBalance) {
121120
setState(() {
@@ -130,22 +129,36 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
130129
});
131130
return false;
132131
}
133-
final balance = roundAmount(availableUSDC ?? '0');
134132

135-
if (balance - Decimal.parse(amount) <= Decimal.zero) {
133+
try {
134+
final decimalAmount = Decimal.parse(amount);
135+
136+
if (decimalAmount <= Decimal.zero) {
137+
setState(() {
138+
amountError = 'Amount must be greater than zero';
139+
});
140+
return false;
141+
}
142+
143+
final balance = roundAmount(availableUSDC ?? '0');
144+
145+
if (balance - decimalAmount <= Decimal.zero) {
146+
setState(() {
147+
amountError = 'Balance is not enough';
148+
});
149+
return false;
150+
}
151+
136152
setState(() {
137-
amountError = 'Balance is not enough';
153+
amountError = null;
138154
});
139-
return false;
140-
}
141-
142-
if (Decimal.parse(amount) > Decimal.parse(availableUSDC ?? '0')) {
155+
return true;
156+
} catch (e) {
143157
setState(() {
144-
amountError = 'Not enough balance';
158+
amountError = 'Invalid amount format';
145159
});
146160
return false;
147161
}
148-
return true;
149162
}
150163

151164
bool _validatePrice() {
@@ -173,6 +186,7 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
173186
(Decimal.fromInt(percentage).shift(-2));
174187
amountController.text = roundAmount(amount.toString()).toString();
175188
_calculateTotal();
189+
_validateAmount();
176190
}
177191

178192
void _calculateTotal() {
@@ -437,6 +451,9 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
437451
color: Theme.of(context).colorScheme.onSurface,
438452
),
439453
focusNode: textFieldFocusNode,
454+
onChanged: (_) {
455+
_validateAmount();
456+
},
440457
keyboardType: const TextInputType.numberWithOptions(
441458
decimal: true, signed: false),
442459
controller: amountController,
@@ -638,7 +655,9 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
638655
child: SizedBox(
639656
width: double.infinity,
640657
child: ElevatedButton(
641-
onPressed: loading || loadingBalance
658+
onPressed: loading ||
659+
loadingBalance ||
660+
priceError != null
642661
? null
643662
: () async {
644663
if (_validateAmount() && _validatePrice()) {

0 commit comments

Comments
 (0)