@@ -31,6 +31,8 @@ import '../../services/buy/buy_response.dart';
3131import '../../services/buy/simplex/simplex_api.dart' ;
3232import '../../themes/stack_colors.dart' ;
3333import '../../utilities/address_utils.dart' ;
34+ import '../../utilities/amount/amount.dart' ;
35+ import '../../utilities/amount/amount_formatter.dart' ;
3436import '../../utilities/assets.dart' ;
3537import '../../utilities/barcode_scanner_interface.dart' ;
3638import '../../utilities/clipboard_interface.dart' ;
@@ -124,6 +126,30 @@ class _BuyFormState extends ConsumerState<BuyForm> {
124126 // static Decimal maxCrypto = Decimal.parse((10000.00000000).toString());
125127 // static String boundedCryptoTicker = '';
126128
129+ /// Parse a buy amount string using the active locale's decimal and group
130+ /// separators. Fiat amounts are parsed via [Amount.tryParseFiatString] ;
131+ /// crypto amounts reuse the locale-aware [pAmountFormatter] parser so that
132+ /// full crypto precision is preserved.
133+ Decimal ? _tryParseBuyAmount (String value) {
134+ if (buyWithFiat) {
135+ return Amount .tryParseFiatString (
136+ value,
137+ locale: ref.read (localeServiceChangeNotifierProvider).locale,
138+ )? .decimal;
139+ }
140+
141+ final cc = coin;
142+ if (cc != null ) {
143+ return ref.read (pAmountFormatter (cc)).tryParse (value)? .decimal;
144+ }
145+
146+ // Fall back to fiat-style locale parsing when no coin is available.
147+ return Amount .tryParseFiatString (
148+ value,
149+ locale: ref.read (localeServiceChangeNotifierProvider).locale,
150+ )? .decimal;
151+ }
152+
127153 String _amountOutOfRangeErrorString = "" ;
128154 void validateAmount () {
129155 if (_buyAmountController.text.isEmpty) {
@@ -133,7 +159,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
133159 return ;
134160 }
135161
136- final value = Decimal . tryParse (_buyAmountController.text);
162+ final value = _tryParseBuyAmount (_buyAmountController.text);
137163 if (value == null ) {
138164 setState (() {
139165 _amountOutOfRangeErrorString = "Invalid amount" ;
@@ -414,11 +440,12 @@ class _BuyFormState extends ConsumerState<BuyForm> {
414440 crypto: selectedCrypto! ,
415441 fiat: selectedFiat! ,
416442 youPayFiatPrice: buyWithFiat
417- ? Decimal . parse ( _buyAmountController.text)
443+ ? ( _tryParseBuyAmount ( _buyAmountController.text) ?? Decimal .zero )
418444 : Decimal .parse ("100" ), // dummy value
419445 youReceiveCryptoAmount: buyWithFiat
420446 ? Decimal .parse ("0.000420282" ) // dummy value
421- : Decimal .parse (_buyAmountController.text), // Ternary for this
447+ : (_tryParseBuyAmount (_buyAmountController.text) ??
448+ Decimal .zero), // Ternary for this
422449 id: "id" , // anything; we get an ID back
423450 receivingAddress: _receiveAddressController.text,
424451 buyWithFiat: buyWithFiat,
@@ -1123,12 +1150,23 @@ class _BuyFormState extends ConsumerState<BuyForm> {
11231150 final ClipboardData ? data = await clipboard
11241151 .getData (Clipboard .kTextPlain);
11251152
1126- final amountString = Decimal . tryParse (
1153+ final amountString = _tryParseBuyAmount (
11271154 data? .text ?? "" ,
11281155 );
11291156 if (amountString != null ) {
1157+ final locale = ref
1158+ .read (
1159+ localeServiceChangeNotifierProvider,
1160+ )
1161+ .locale;
1162+ final decimalSeparator =
1163+ Util .getSymbolsFor (
1164+ locale: locale,
1165+ )? .DECIMAL_SEP ??
1166+ "." ;
11301167 _buyAmountController.text = amountString
1131- .toString ();
1168+ .toString ()
1169+ .replaceFirst ("." , decimalSeparator);
11321170
11331171 validateAmount ();
11341172 }
0 commit comments