Skip to content
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
38 changes: 34 additions & 4 deletions app/lib/screens/market/buy_tft.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:stellar_client/models/exceptions.dart';
import 'package:threebotlogin/helpers/logger.dart';
Expand All @@ -11,6 +12,18 @@ import 'package:threebotlogin/screens/market/order.dart';
import 'package:threebotlogin/services/stellar_service.dart' as Stellar;
import 'package:threebotlogin/widgets/custom_dialog.dart';

/// TextInputFormatter that converts comma to dot for decimal separator
class CommaToDotFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.replaceAll(',', '.'),
selection: newValue.selection,
);
}
}

class BuyTFTWidget extends StatefulWidget {
final Wallet wallet;
final Offer? offer;
Expand Down Expand Up @@ -171,14 +184,22 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
});
return false;
}
if (Decimal.parse(price) <= Decimal.zero) {

try {
final decimalPrice = Decimal.parse(price);
if (decimalPrice <= Decimal.zero) {
setState(() {
priceError = 'Price should be positive';
});
return false;
}
return true;
} catch (e) {
setState(() {
priceError = 'Price should be positive';
priceError = 'Invalid price format';
});
return false;
}

return true;
}

calculateAmount(int percentage) {
Expand Down Expand Up @@ -456,6 +477,9 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
},
keyboardType: const TextInputType.numberWithOptions(
decimal: true, signed: false),
inputFormatters: [
CommaToDotFormatter(),
],
controller: amountController,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
Expand Down Expand Up @@ -562,6 +586,12 @@ class _BuyTFTWidgetState extends State<BuyTFTWidget> {
),
keyboardType: const TextInputType.numberWithOptions(
decimal: true, signed: false),
inputFormatters: [
CommaToDotFormatter(),
],
onChanged: (_) {
_validatePrice();
},
controller: priceController,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
Expand Down