Skip to content

Handle negative numbers validations && make validations onchange #1067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions app/lib/screens/market/buy_tft.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {

bool _validateAmount() {
final amount = amountController.text.trim();
amountError = null;

if (loadingBalance) {
setState(() {
Expand All @@ -130,22 +129,36 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
});
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 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;
}

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() {
Expand Down Expand Up @@ -173,6 +186,7 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
(Decimal.fromInt(percentage).shift(-2));
amountController.text = roundAmount(amount.toString()).toString();
_calculateTotal();
_validateAmount();
}

void _calculateTotal() {
Expand Down Expand Up @@ -437,6 +451,9 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
color: Theme.of(context).colorScheme.onSurface,
),
focusNode: textFieldFocusNode,
onChanged: (_) {
_validateAmount();
},
keyboardType: const TextInputType.numberWithOptions(
decimal: true, signed: false),
controller: amountController,
Expand Down Expand Up @@ -638,7 +655,9 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: loading || loadingBalance
onPressed: loading ||
loadingBalance ||
priceError != null
? null
: () async {
if (_validateAmount() && _validatePrice()) {
Expand Down