@@ -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,50 @@ 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 cannot be negative' ;
139+ });
140+ return false ;
141+ }
142+
143+ if (decimalAmount == Decimal .zero) {
144+ setState (() {
145+ amountError = 'Amount must be greater than zero' ;
146+ });
147+ return false ;
148+ }
149+
150+ final balance = roundAmount (availableUSDC ?? '0' );
151+
152+ if (balance - decimalAmount <= Decimal .zero) {
153+ setState (() {
154+ amountError = 'Balance is not enough' ;
155+ });
156+ return false ;
157+ }
158+
159+ if (decimalAmount > Decimal .parse (availableUSDC ?? '0' )) {
160+ setState (() {
161+ amountError = 'Not enough balance' ;
162+ });
163+ return false ;
164+ }
165+
136166 setState (() {
137- amountError = 'Balance is not enough' ;
167+ amountError = null ;
138168 });
139- return false ;
140- }
141-
142- if (Decimal .parse (amount) > Decimal .parse (availableUSDC ?? '0' )) {
169+ return true ;
170+ } catch (e) {
143171 setState (() {
144- amountError = 'Not enough balance ' ;
172+ amountError = 'Invalid amount format ' ;
145173 });
146174 return false ;
147175 }
148- return true ;
149176 }
150177
151178 bool _validatePrice () {
@@ -173,6 +200,7 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
173200 (Decimal .fromInt (percentage).shift (- 2 ));
174201 amountController.text = roundAmount (amount.toString ()).toString ();
175202 _calculateTotal ();
203+ _validateAmount ();
176204 }
177205
178206 void _calculateTotal () {
@@ -437,6 +465,12 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
437465 color: Theme .of (context).colorScheme.onSurface,
438466 ),
439467 focusNode: textFieldFocusNode,
468+ onChanged: (_) {
469+ setState (() {
470+ amountError = null ;
471+ });
472+ _validateAmount ();
473+ },
440474 keyboardType: const TextInputType .numberWithOptions (
441475 decimal: true , signed: false ),
442476 controller: amountController,
@@ -638,7 +672,9 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
638672 child: SizedBox (
639673 width: double .infinity,
640674 child: ElevatedButton (
641- onPressed: loading || loadingBalance
675+ onPressed: loading ||
676+ loadingBalance ||
677+ priceError != null
642678 ? null
643679 : () async {
644680 if (_validateAmount () && _validatePrice ()) {
0 commit comments