stack_wallet/lib/pages/send_view/send_view.dart

2415 lines
106 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:async';
import 'dart:io';
import 'package:cs_monero/cs_monero.dart' as lib_monero;
2022-08-26 08:11:35 +00:00
import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
2024-05-27 23:56:22 +00:00
import 'package:tuple/tuple.dart';
import '../../models/isar/models/isar_models.dart';
import '../../models/paynym/paynym_account_lite.dart';
import '../../models/send_view_auto_fill_data.dart';
import '../../providers/providers.dart';
import '../../providers/ui/fee_rate_type_state_provider.dart';
import '../../providers/ui/preview_tx_button_state_provider.dart';
import '../../providers/wallet/public_private_balance_state_provider.dart';
import '../../route_generator.dart';
import '../../themes/coin_icon_provider.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/address_utils.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/amount/amount_formatter.dart';
import '../../utilities/amount/amount_input_formatter.dart';
import '../../utilities/amount/amount_unit.dart';
import '../../utilities/assets.dart';
import '../../utilities/barcode_scanner_interface.dart';
import '../../utilities/clipboard_interface.dart';
import '../../utilities/constants.dart';
import '../../utilities/enums/fee_rate_type_enum.dart';
import '../../utilities/extensions/extensions.dart';
import '../../utilities/logger.dart';
import '../../utilities/prefs.dart';
import '../../utilities/text_styles.dart';
import '../../utilities/util.dart';
import '../../wallets/crypto_currency/crypto_currency.dart';
import '../../wallets/crypto_currency/intermediate/nano_currency.dart';
import '../../wallets/isar/providers/wallet_info_provider.dart';
import '../../wallets/models/tx_data.dart';
import '../../wallets/wallet/impl/firo_wallet.dart';
import '../../wallets/wallet/wallet_mixin_interfaces/coin_control_interface.dart';
import '../../wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart';
import '../../wallets/wallet/wallet_mixin_interfaces/spark_interface.dart';
import '../../widgets/animated_text.dart';
import '../../widgets/background.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/custom_buttons/blue_text_button.dart';
2024-06-25 20:46:36 +00:00
import '../../widgets/dialogs/firo_exchange_address_dialog.dart';
import '../../widgets/fee_slider.dart';
import '../../widgets/icon_widgets/addressbook_icon.dart';
import '../../widgets/icon_widgets/clipboard_icon.dart';
import '../../widgets/icon_widgets/qrcode_icon.dart';
import '../../widgets/icon_widgets/x_icon.dart';
import '../../widgets/rounded_white_container.dart';
import '../../widgets/stack_dialog.dart';
import '../../widgets/stack_text_field.dart';
import '../../widgets/textfield_icon_button.dart';
2024-05-27 23:56:22 +00:00
import '../address_book_views/address_book_view.dart';
import '../coin_control/coin_control_view.dart';
import 'confirm_transaction_view.dart';
import 'sub_widgets/building_transaction_dialog.dart';
import 'sub_widgets/firo_balance_selection_sheet.dart';
import 'sub_widgets/transaction_fee_selection_sheet.dart';
2022-08-26 08:11:35 +00:00
class SendView extends ConsumerStatefulWidget {
const SendView({
2024-05-15 21:20:45 +00:00
super.key,
2022-08-26 08:11:35 +00:00
required this.walletId,
required this.coin,
this.autoFillData,
this.clipboard = const ClipboardWrapper(),
this.barcodeScanner = const BarcodeScannerWrapper(),
2023-01-26 17:46:16 +00:00
this.accountLite,
2024-05-15 21:20:45 +00:00
});
2022-08-26 08:11:35 +00:00
static const String routeName = "/sendView";
final String walletId;
2024-05-15 21:20:45 +00:00
final CryptoCurrency coin;
2022-08-26 08:11:35 +00:00
final SendViewAutoFillData? autoFillData;
final ClipboardInterface clipboard;
final BarcodeScannerInterface barcodeScanner;
2023-01-26 17:46:16 +00:00
final PaynymAccountLite? accountLite;
2022-08-26 08:11:35 +00:00
@override
ConsumerState<SendView> createState() => _SendViewState();
}
class _SendViewState extends ConsumerState<SendView> {
late final String walletId;
2024-05-15 21:20:45 +00:00
late final CryptoCurrency coin;
2022-08-26 08:11:35 +00:00
late final ClipboardInterface clipboard;
late final BarcodeScannerInterface scanner;
late TextEditingController sendToController;
late TextEditingController cryptoAmountController;
late TextEditingController baseAmountController;
late TextEditingController noteController;
2023-07-05 12:57:17 +00:00
late TextEditingController onChainNoteController;
2022-08-26 08:11:35 +00:00
late TextEditingController feeController;
late TextEditingController memoController;
2022-08-26 08:11:35 +00:00
late final SendViewAutoFillData? _data;
final _addressFocusNode = FocusNode();
final _noteFocusNode = FocusNode();
2023-07-05 12:57:17 +00:00
final _onChainNoteFocusNode = FocusNode();
2022-08-26 08:11:35 +00:00
final _cryptoFocus = FocusNode();
final _baseFocus = FocusNode();
final _memoFocus = FocusNode();
late final bool isStellar;
late final bool isFiro;
2022-08-26 08:11:35 +00:00
2023-04-05 22:06:31 +00:00
Amount? _cachedAmountToSend;
2022-08-26 08:11:35 +00:00
String? _address;
bool _addressToggleFlag = false;
bool _isFiroExWarningDisplayed = false;
2022-08-26 08:11:35 +00:00
bool _cryptoAmountChangeLock = false;
late VoidCallback onCryptoAmountChanged;
2023-03-07 21:45:22 +00:00
Set<UTXO> selectedUTXOs = {};
Future<void> _scanQr() async {
try {
// ref
// .read(
// shouldShowLockscreenOnResumeStateProvider
// .state)
// .state = false;
if (FocusScope.of(context).hasFocus) {
FocusScope.of(context).unfocus();
await Future<void>.delayed(const Duration(milliseconds: 75));
}
final qrResult = await scanner.scan();
// Future<void>.delayed(
// const Duration(seconds: 2),
// () => ref
// .read(
// shouldShowLockscreenOnResumeStateProvider
// .state)
// .state = true,
// );
Logging.instance.log(
"qrResult content: ${qrResult.rawContent}",
level: LogLevel.Info,
);
final paymentData = AddressUtils.parsePaymentUri(
qrResult.rawContent,
logging: Logging.instance,
);
if (paymentData != null &&
paymentData.coin?.uriScheme == coin.uriScheme) {
// auto fill address
_address = paymentData.address.trim();
sendToController.text = _address!;
// autofill notes field
if (paymentData.message != null) {
noteController.text = paymentData.message!;
} else if (paymentData.label != null) {
noteController.text = paymentData.label!;
}
// autofill amount field
if (paymentData.amount != null) {
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
2024-05-15 21:20:45 +00:00
fractionDigits: coin.fractionDigits,
);
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
amount,
withUnitName: false,
);
ref.read(pSendAmount.notifier).state = amount;
}
_setValidAddressProviders(_address);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
// now check for non standard encoded basic address
} else {
_address = qrResult.rawContent.split("\n").first.trim();
sendToController.text = _address ?? "";
_setValidAddressProviders(_address);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
}
} on PlatformException catch (e, s) {
// ref
// .read(
// shouldShowLockscreenOnResumeStateProvider
// .state)
// .state = true;
// here we ignore the exception caused by not giving permission
// to use the camera to scan a qr code
Logging.instance.log(
"Failed to get camera permissions while trying to scan qr code in SendView: $e\n$s",
level: LogLevel.Warning,
);
}
}
void _fiatFieldChanged(String baseAmountString) {
final baseAmount = Amount.tryParseFiatString(
baseAmountString,
locale: ref.read(localeServiceChangeNotifierProvider).locale,
);
final Amount? amount;
if (baseAmount != null) {
final Decimal _price =
ref.read(priceAnd24hChangeNotifierProvider).getPrice(coin).item1;
if (_price == Decimal.zero) {
2024-05-15 21:20:45 +00:00
amount = 0.toAmountAsRaw(fractionDigits: coin.fractionDigits);
} else {
amount = baseAmount <= Amount.zero
2024-05-15 21:20:45 +00:00
? 0.toAmountAsRaw(fractionDigits: coin.fractionDigits)
: (baseAmount.decimal / _price)
.toDecimal(
2024-05-15 21:20:45 +00:00
scaleOnInfinitePrecision: coin.fractionDigits,
)
2024-05-15 21:20:45 +00:00
.toAmount(fractionDigits: coin.fractionDigits);
}
if (_cachedAmountToSend != null && _cachedAmountToSend == amount) {
return;
}
_cachedAmountToSend = amount;
Logging.instance
.log("it changed $amount $_cachedAmountToSend", level: LogLevel.Info);
final amountString = ref.read(pAmountFormatter(coin)).format(
amount,
withUnitName: false,
);
_cryptoAmountChangeLock = true;
cryptoAmountController.text = amountString;
_cryptoAmountChangeLock = false;
} else {
2024-05-15 21:20:45 +00:00
amount = 0.toAmountAsRaw(fractionDigits: coin.fractionDigits);
_cryptoAmountChangeLock = true;
cryptoAmountController.text = "";
_cryptoAmountChangeLock = false;
}
// setState(() {
// _calculateFeesFuture = calculateFees(
// Format.decimalAmountToSatoshis(
// _amountToSend!));
// });
ref.read(pSendAmount.notifier).state = amount;
}
2022-08-26 08:11:35 +00:00
void _cryptoAmountChanged() async {
if (!_cryptoAmountChangeLock) {
final cryptoAmount = ref.read(pAmountFormatter(coin)).tryParse(
cryptoAmountController.text,
);
final Amount? amount;
if (cryptoAmount != null) {
amount = cryptoAmount;
if (_cachedAmountToSend != null && _cachedAmountToSend == amount) {
2022-08-26 08:11:35 +00:00
return;
}
_cachedAmountToSend = amount;
Logging.instance.log(
"it changed $amount $_cachedAmountToSend",
level: LogLevel.Info,
);
2022-08-26 08:11:35 +00:00
final price =
ref.read(priceAnd24hChangeNotifierProvider).getPrice(coin).item1;
if (price > Decimal.zero) {
baseAmountController.text = (amount!.decimal * price)
2023-04-05 22:06:31 +00:00
.toAmount(
fractionDigits: 2,
)
2023-05-29 21:10:55 +00:00
.fiatString(
2023-04-05 22:06:31 +00:00
locale: ref.read(localeServiceChangeNotifierProvider).locale,
);
2022-08-26 08:11:35 +00:00
}
} else {
amount = null;
2022-08-26 08:11:35 +00:00
baseAmountController.text = "";
}
ref.read(pSendAmount.notifier).state = amount;
2022-08-26 08:11:35 +00:00
_cryptoAmountChangedFeeUpdateTimer?.cancel();
_cryptoAmountChangedFeeUpdateTimer = Timer(updateFeesTimerDuration, () {
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash && !_baseFocus.hasFocus) {
setState(() {
_calculateFeesFuture = calculateFees(
amount == null
2024-05-15 21:20:45 +00:00
? 0.toAmountAsRaw(fractionDigits: coin.fractionDigits)
: amount!,
);
});
}
});
2022-08-26 08:11:35 +00:00
}
}
final updateFeesTimerDuration = const Duration(milliseconds: 500);
Timer? _cryptoAmountChangedFeeUpdateTimer;
Timer? _baseAmountChangedFeeUpdateTimer;
void _baseAmountChanged() {
_baseAmountChangedFeeUpdateTimer?.cancel();
_baseAmountChangedFeeUpdateTimer = Timer(updateFeesTimerDuration, () {
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash && !_cryptoFocus.hasFocus) {
setState(() {
_calculateFeesFuture = calculateFees(
ref.read(pSendAmount) == null
2024-05-15 21:20:45 +00:00
? 0.toAmountAsRaw(fractionDigits: coin.fractionDigits)
: ref.read(pSendAmount)!,
);
});
}
});
}
2023-04-05 22:06:31 +00:00
late Amount _currentFee;
void _setCurrentFee(String fee, bool shouldSetState) {
2023-05-30 15:02:09 +00:00
fee = fee.trim();
if (fee.startsWith("~")) {
fee = fee.substring(1);
}
if (fee.contains(" ")) {
fee = fee.split(" ").first;
}
2023-04-17 21:36:40 +00:00
final value = fee.contains(",")
? Decimal.parse(fee.replaceFirst(",", "."))
2024-05-15 21:20:45 +00:00
.toAmount(fractionDigits: coin.fractionDigits)
: Decimal.parse(fee).toAmount(fractionDigits: coin.fractionDigits);
2023-04-05 22:06:31 +00:00
if (shouldSetState) {
setState(() => _currentFee = value);
} else {
_currentFee = value;
}
}
String? _updateInvalidAddressText(String address) {
2022-08-26 08:11:35 +00:00
if (_data != null && _data!.contactLabel == address) {
return null;
}
if (address.isNotEmpty &&
!ref
.read(pWallets)
.getWallet(walletId)
.cryptoCurrency
.validateAddress(address)) {
2022-08-26 08:11:35 +00:00
return "Invalid address";
}
return null;
}
void _setValidAddressProviders(String? address) {
2023-01-26 18:16:38 +00:00
if (isPaynymSend) {
ref.read(pValidSendToAddress.notifier).state = true;
2023-01-26 18:16:38 +00:00
} else {
final wallet = ref.read(pWallets).getWallet(walletId);
if (wallet is SparkInterface) {
ref.read(pValidSparkSendToAddress.notifier).state =
SparkInterface.validateSparkAddress(
address: address ?? "",
2024-06-19 15:15:49 +00:00
isTestNet: wallet.cryptoCurrency.network.isTestNet,
);
2024-06-25 20:46:36 +00:00
ref.read(pIsExchangeAddress.state).state =
(coin as Firo).isExchangeAddress(address ?? "");
2024-06-25 20:46:36 +00:00
if (ref.read(publicPrivateBalanceStateProvider) == FiroType.spark &&
ref.read(pIsExchangeAddress) &&
!_isFiroExWarningDisplayed) {
_isFiroExWarningDisplayed = true;
showFiroExchangeAddressWarning(
context,
() => _isFiroExWarningDisplayed = false,
);
2024-06-25 20:46:36 +00:00
}
}
ref.read(pValidSendToAddress.notifier).state =
wallet.cryptoCurrency.validateAddress(address ?? "");
2023-01-26 18:16:38 +00:00
}
2022-08-26 08:11:35 +00:00
}
late Future<String> _calculateFeesFuture;
2023-04-05 22:06:31 +00:00
Map<Amount, String> cachedFees = {};
Map<Amount, String> cachedFiroLelantusFees = {};
Map<Amount, String> cachedFiroSparkFees = {};
2023-04-05 22:06:31 +00:00
Map<Amount, String> cachedFiroPublicFees = {};
2022-08-26 08:11:35 +00:00
2023-04-05 22:06:31 +00:00
Future<String> calculateFees(Amount amount) async {
if (amount <= Amount.zero) {
2022-08-26 08:11:35 +00:00
return "0";
}
if (isFiro) {
switch (ref.read(publicPrivateBalanceStateProvider.state).state) {
case FiroType.public:
if (cachedFiroPublicFees[amount] != null) {
return cachedFiroPublicFees[amount]!;
}
break;
case FiroType.lelantus:
if (cachedFiroLelantusFees[amount] != null) {
return cachedFiroLelantusFees[amount]!;
}
break;
case FiroType.spark:
if (cachedFiroSparkFees[amount] != null) {
return cachedFiroSparkFees[amount]!;
}
break;
}
} else if (cachedFees[amount] != null) {
2022-08-26 08:11:35 +00:00
return cachedFees[amount]!;
}
final wallet = ref.read(pWallets).getWallet(walletId);
final feeObject = await wallet.fees;
2022-08-26 08:11:35 +00:00
late final int feeRate;
switch (ref.read(feeRateTypeStateProvider.state).state) {
case FeeRateType.fast:
feeRate = feeObject.fast;
break;
case FeeRateType.average:
feeRate = feeObject.medium;
break;
case FeeRateType.slow:
feeRate = feeObject.slow;
break;
default:
feeRate = -1;
2022-08-26 08:11:35 +00:00
}
2023-04-05 22:06:31 +00:00
Amount fee;
2024-05-15 21:20:45 +00:00
if (coin is Monero) {
lib_monero.TransactionPriority specialMoneroId;
switch (ref.read(feeRateTypeStateProvider.state).state) {
case FeeRateType.fast:
specialMoneroId = lib_monero.TransactionPriority.high;
break;
case FeeRateType.average:
specialMoneroId = lib_monero.TransactionPriority.medium;
break;
case FeeRateType.slow:
specialMoneroId = lib_monero.TransactionPriority.normal;
break;
default:
throw ArgumentError("custom fee not available for monero");
}
fee = await wallet.estimateFeeFor(amount, specialMoneroId.value);
2023-05-29 21:10:55 +00:00
cachedFees[amount] = ref.read(pAmountFormatter(coin)).format(
fee,
2023-05-30 15:02:09 +00:00
withUnitName: true,
indicatePrecisionLoss: false,
2023-05-29 21:10:55 +00:00
);
return cachedFees[amount]!;
} else if (isFiro) {
final firoWallet = wallet as FiroWallet;
switch (ref.read(publicPrivateBalanceStateProvider.state).state) {
case FiroType.public:
fee = await firoWallet.estimateFeeFor(amount, feeRate);
cachedFiroPublicFees[amount] =
ref.read(pAmountFormatter(coin)).format(
fee,
withUnitName: true,
indicatePrecisionLoss: false,
);
return cachedFiroPublicFees[amount]!;
2022-08-26 08:11:35 +00:00
case FiroType.lelantus:
fee = await firoWallet.estimateFeeForLelantus(amount);
cachedFiroLelantusFees[amount] =
ref.read(pAmountFormatter(coin)).format(
fee,
withUnitName: true,
indicatePrecisionLoss: false,
);
return cachedFiroLelantusFees[amount]!;
case FiroType.spark:
fee = await firoWallet.estimateFeeForSpark(amount);
cachedFiroSparkFees[amount] = ref.read(pAmountFormatter(coin)).format(
fee,
withUnitName: true,
indicatePrecisionLoss: false,
);
return cachedFiroSparkFees[amount]!;
}
} else {
fee = await wallet.estimateFeeFor(amount, feeRate);
2023-05-29 21:10:55 +00:00
cachedFees[amount] = ref.read(pAmountFormatter(coin)).format(
fee,
2023-05-30 15:02:09 +00:00
withUnitName: true,
indicatePrecisionLoss: false,
2023-05-29 21:10:55 +00:00
);
return cachedFees[amount]!;
}
2022-08-26 08:11:35 +00:00
}
2023-01-26 18:16:38 +00:00
Future<void> _previewTransaction() async {
// wait for keyboard to disappear
FocusScope.of(context).unfocus();
await Future<void>.delayed(
const Duration(milliseconds: 100),
);
final wallet = ref.read(pWallets).getWallet(walletId);
2023-01-26 18:16:38 +00:00
final Amount amount = ref.read(pSendAmount)!;
2023-04-05 22:06:31 +00:00
final Amount availableBalance;
if (isFiro) {
switch (ref.read(publicPrivateBalanceStateProvider.state).state) {
case FiroType.public:
availableBalance = wallet.info.cachedBalance.spendable;
break;
case FiroType.lelantus:
availableBalance = wallet.info.cachedBalanceSecondary.spendable;
break;
case FiroType.spark:
availableBalance = wallet.info.cachedBalanceTertiary.spendable;
break;
2023-01-26 18:16:38 +00:00
}
} else {
availableBalance = ref.read(pWalletBalance(walletId)).spendable;
2023-01-26 18:16:38 +00:00
}
2023-03-08 21:19:43 +00:00
final coinControlEnabled =
ref.read(prefsChangeNotifierProvider).enableCoinControl;
2024-05-15 21:20:45 +00:00
if (coin is! Ethereum &&
!(wallet is CoinControlInterface && coinControlEnabled) ||
(wallet is CoinControlInterface &&
2023-03-08 21:19:43 +00:00
coinControlEnabled &&
selectedUTXOs.isEmpty)) {
2023-03-07 21:45:22 +00:00
// confirm send all
if (amount == availableBalance) {
bool? shouldSendAll;
if (mounted) {
shouldSendAll = await showDialog<bool>(
context: context,
useSafeArea: false,
barrierDismissible: true,
builder: (context) {
return StackDialog(
title: "Confirm send all",
message:
"You are about to send your entire balance. Would you like to continue?",
leftButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
child: Text(
"Cancel",
style: STextStyles.button(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark,
),
),
onPressed: () {
Navigator.of(context).pop(false);
},
2023-03-07 21:45:22 +00:00
),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonStyle(context),
child: Text(
"Yes",
style: STextStyles.button(context),
),
onPressed: () {
Navigator.of(context).pop(true);
},
2023-03-07 21:45:22 +00:00
),
);
},
);
}
2023-01-26 18:16:38 +00:00
2023-03-07 21:45:22 +00:00
if (shouldSendAll == null || shouldSendAll == false) {
// cancel preview
return;
}
2023-01-26 18:16:38 +00:00
}
}
try {
bool wasCancelled = false;
if (mounted) {
unawaited(
showDialog<void>(
context: context,
useSafeArea: false,
barrierDismissible: false,
builder: (context) {
return BuildingTransactionDialog(
coin: wallet.info.coin,
isSpark: wallet is FiroWallet &&
ref.read(publicPrivateBalanceStateProvider.state).state ==
FiroType.spark,
onCancel: () {
wasCancelled = true;
2023-01-26 18:16:38 +00:00
Navigator.of(context).pop();
},
);
},
),
);
}
2023-01-26 18:16:38 +00:00
final time = Future<dynamic>.delayed(
const Duration(
2023-03-14 15:43:54 +00:00
milliseconds: 2500,
),
);
Future<TxData> txDataFuture;
2023-01-26 18:16:38 +00:00
if (isPaynymSend) {
2023-11-16 16:54:57 +00:00
final feeRate = ref.read(feeRateTypeStateProvider);
txDataFuture = (wallet as PaynymInterface).preparePaymentCodeSend(
txData: TxData(
paynymAccountLite: widget.accountLite!,
recipients: [
(
address: widget.accountLite!.code,
amount: amount,
2024-01-05 19:45:42 +00:00
isChange: false,
),
2023-11-16 16:54:57 +00:00
],
satsPerVByte: isCustomFee ? customFeeRate : null,
feeRateType: feeRate,
utxos: (wallet is CoinControlInterface &&
coinControlEnabled &&
selectedUTXOs.isNotEmpty)
? selectedUTXOs
: null,
),
2023-01-26 18:16:38 +00:00
);
} else if (wallet is FiroWallet) {
switch (ref.read(publicPrivateBalanceStateProvider.state).state) {
case FiroType.public:
if (ref.read(pValidSparkSendToAddress)) {
txDataFuture = wallet.prepareSparkMintTransaction(
txData: TxData(
sparkRecipients: [
(
address: _address!,
amount: amount,
memo: memoController.text,
2024-01-05 19:45:42 +00:00
isChange: false,
),
],
feeRateType: ref.read(feeRateTypeStateProvider),
satsPerVByte: isCustomFee ? customFeeRate : null,
utxos: (wallet is CoinControlInterface &&
coinControlEnabled &&
selectedUTXOs.isNotEmpty)
? selectedUTXOs
: null,
),
);
} else {
txDataFuture = wallet.prepareSend(
txData: TxData(
2024-01-05 19:45:42 +00:00
recipients: [
(
address: _address!,
amount: amount,
isChange: false,
),
2024-01-05 19:45:42 +00:00
],
feeRateType: ref.read(feeRateTypeStateProvider),
satsPerVByte: isCustomFee ? customFeeRate : null,
utxos: (wallet is CoinControlInterface &&
coinControlEnabled &&
selectedUTXOs.isNotEmpty)
? selectedUTXOs
: null,
),
);
}
break;
case FiroType.lelantus:
txDataFuture = wallet.prepareSendLelantus(
txData: TxData(
2024-01-05 19:45:42 +00:00
recipients: [
(
address: _address!,
amount: amount,
isChange: false,
),
2024-01-05 19:45:42 +00:00
],
),
);
break;
case FiroType.spark:
txDataFuture = wallet.prepareSendSpark(
txData: TxData(
recipients: ref.read(pValidSparkSendToAddress)
? null
2024-01-05 19:45:42 +00:00
: [
(
address: _address!,
amount: amount,
isChange: false,
),
2024-01-05 19:45:42 +00:00
],
sparkRecipients: ref.read(pValidSparkSendToAddress)
? [
(
address: _address!,
amount: amount,
memo: memoController.text,
2024-01-05 19:45:42 +00:00
isChange: false,
),
]
: null,
),
);
break;
}
2023-01-26 18:16:38 +00:00
} else {
2024-05-15 21:20:45 +00:00
final memo = coin is Stellar ? memoController.text : null;
txDataFuture = wallet.prepareSend(
txData: TxData(
2024-01-05 19:45:42 +00:00
recipients: [
(
address: _address!,
amount: amount,
isChange: false,
),
2024-01-05 19:45:42 +00:00
],
memo: memo,
feeRateType: ref.read(feeRateTypeStateProvider),
satsPerVByte: isCustomFee ? customFeeRate : null,
utxos: (wallet is CoinControlInterface &&
2023-03-08 21:19:43 +00:00
coinControlEnabled &&
selectedUTXOs.isNotEmpty)
2023-03-07 21:45:22 +00:00
? selectedUTXOs
: null,
),
2023-01-26 18:16:38 +00:00
);
}
final results = await Future.wait([
txDataFuture,
time,
]);
TxData txData = results.first as TxData;
2023-01-26 18:16:38 +00:00
if (!wasCancelled && mounted) {
if (isPaynymSend) {
2023-11-16 16:54:57 +00:00
txData = txData.copyWith(
paynymAccountLite: widget.accountLite!,
note: noteController.text.isNotEmpty
? noteController.text
: "PayNym send",
);
} else {
txData = txData.copyWith(note: noteController.text);
txData = txData.copyWith(noteOnChain: onChainNoteController.text);
2023-01-26 18:16:38 +00:00
}
2023-11-16 16:54:57 +00:00
// pop building dialog
Navigator.of(context).pop();
2024-01-04 18:52:00 +00:00
unawaited(
Navigator.of(context).push(
RouteGenerator.getRoute(
shouldUseMaterialRoute: RouteGenerator.useMaterialPageRoute,
builder: (_) => ConfirmTransactionView(
txData: txData,
walletId: walletId,
isPaynymTransaction: isPaynymSend,
onSuccess: clearSendForm,
),
settings: const RouteSettings(
name: ConfirmTransactionView.routeName,
),
2023-01-26 18:16:38 +00:00
),
),
2024-01-04 18:52:00 +00:00
);
2023-01-26 18:16:38 +00:00
}
2024-07-29 22:04:23 +00:00
} catch (e, s) {
Logging.instance.log("$e\n$s", level: LogLevel.Error);
2023-01-26 18:16:38 +00:00
if (mounted) {
// pop building dialog
Navigator.of(context).pop();
2024-01-04 18:52:00 +00:00
unawaited(
showDialog<dynamic>(
context: context,
useSafeArea: false,
barrierDismissible: true,
builder: (context) {
return StackDialog(
title: "Transaction failed",
message: e.toString(),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
child: Text(
"Ok",
style: STextStyles.button(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark,
),
2024-01-04 18:52:00 +00:00
),
onPressed: () {
Navigator.of(context).pop();
},
2023-01-26 18:16:38 +00:00
),
2024-01-04 18:52:00 +00:00
);
},
),
);
2023-01-26 18:16:38 +00:00
}
}
}
2024-01-04 18:52:00 +00:00
void clearSendForm() {
sendToController.text = "";
cryptoAmountController.text = "";
baseAmountController.text = "";
noteController.text = "";
onChainNoteController.text = "";
feeController.text = "";
memoController.text = "";
_address = "";
_addressToggleFlag = false;
if (mounted) {
setState(() {});
}
}
String _getSendAllTitle(bool showCoinControl, Set<UTXO> selectedUTXOs) {
if (showCoinControl && selectedUTXOs.isNotEmpty) {
return "Send all selected";
}
return "Send all ${coin.ticker}";
}
Amount _selectedUtxosAmount(Set<UTXO> utxos) => Amount(
rawValue:
utxos.map((e) => BigInt.from(e.value)).reduce((v, e) => v += e),
fractionDigits: ref.read(pWalletCoin(walletId)).fractionDigits,
);
Future<void> _sendAllTapped(bool showCoinControl) async {
final Amount amount;
if (showCoinControl && selectedUTXOs.isNotEmpty) {
amount = _selectedUtxosAmount(selectedUTXOs);
} else if (isFiro) {
switch (ref.read(publicPrivateBalanceStateProvider.state).state) {
case FiroType.public:
amount = ref.read(pWalletBalance(walletId)).spendable;
break;
case FiroType.lelantus:
amount = ref.read(pWalletBalanceSecondary(walletId)).spendable;
break;
case FiroType.spark:
amount = ref.read(pWalletBalanceTertiary(walletId)).spendable;
break;
}
} else {
amount = ref.read(pWalletBalance(walletId)).spendable;
}
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
amount,
withUnitName: false,
);
_cryptoAmountChanged();
}
2023-01-26 17:46:16 +00:00
bool get isPaynymSend => widget.accountLite != null;
bool isCustomFee = false;
int customFeeRate = 1;
2022-08-26 08:11:35 +00:00
@override
void initState() {
2023-04-05 22:06:31 +00:00
coin = widget.coin;
2024-06-25 20:46:36 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
ref.refresh(feeSheetSessionCacheProvider);
ref.refresh(pIsExchangeAddress);
});
2024-05-15 21:20:45 +00:00
_currentFee = 0.toAmountAsRaw(fractionDigits: coin.fractionDigits);
2022-08-26 08:11:35 +00:00
2023-04-05 22:06:31 +00:00
_calculateFeesFuture =
2024-05-15 21:20:45 +00:00
calculateFees(0.toAmountAsRaw(fractionDigits: coin.fractionDigits));
2022-08-26 08:11:35 +00:00
_data = widget.autoFillData;
walletId = widget.walletId;
clipboard = widget.clipboard;
scanner = widget.barcodeScanner;
2024-05-15 21:20:45 +00:00
isStellar = coin is Stellar;
isFiro = coin is Firo;
2022-08-26 08:11:35 +00:00
sendToController = TextEditingController();
cryptoAmountController = TextEditingController();
baseAmountController = TextEditingController();
noteController = TextEditingController();
2023-07-05 12:57:17 +00:00
onChainNoteController = TextEditingController();
2022-08-26 08:11:35 +00:00
feeController = TextEditingController();
memoController = TextEditingController();
2022-08-26 08:11:35 +00:00
onCryptoAmountChanged = _cryptoAmountChanged;
cryptoAmountController.addListener(onCryptoAmountChanged);
baseAmountController.addListener(_baseAmountChanged);
2022-08-26 08:11:35 +00:00
if (_data != null) {
if (_data!.amount != null) {
2023-05-30 15:02:09 +00:00
final amount = Amount.fromDecimal(
_data!.amount!,
2024-05-15 21:20:45 +00:00
fractionDigits: coin.fractionDigits,
2023-05-30 15:02:09 +00:00
);
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
amount,
withUnitName: false,
);
2022-08-26 08:11:35 +00:00
}
sendToController.text = _data!.contactLabel;
_address = _data!.address.trim();
2022-08-26 08:11:35 +00:00
_addressToggleFlag = true;
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_setValidAddressProviders(_address);
});
2022-08-26 08:11:35 +00:00
}
2023-01-26 17:46:16 +00:00
if (isPaynymSend) {
sendToController.text = widget.accountLite!.nymName;
noteController.text = "PayNym send";
WidgetsBinding.instance.addPostFrameCallback(
(_) => _setValidAddressProviders(sendToController.text),
);
2023-01-26 17:46:16 +00:00
}
2024-05-15 21:20:45 +00:00
// if (coin is! Epiccash) {
// _cryptoFocus.addListener(() {
// if (!_cryptoFocus.hasFocus && !_baseFocus.hasFocus) {
// if (_amountToSend == null) {
// setState(() {
// _calculateFeesFuture = calculateFees(0);
// });
// } else {
// setState(() {
// _calculateFeesFuture = calculateFees(
// Format.decimalAmountToSatoshis(_amountToSend!, coin));
// });
// }
// }
// });
// _baseFocus.addListener(() {
// if (!_cryptoFocus.hasFocus && !_baseFocus.hasFocus) {
// if (_amountToSend == null) {
// setState(() {
// _calculateFeesFuture = calculateFees(0);
// });
// } else {
// setState(() {
// _calculateFeesFuture = calculateFees(
// Format.decimalAmountToSatoshis(_amountToSend!, coin));
// });
// }
// }
// });
// }
2022-08-26 08:11:35 +00:00
super.initState();
}
@override
void dispose() {
_cryptoAmountChangedFeeUpdateTimer?.cancel();
_baseAmountChangedFeeUpdateTimer?.cancel();
2022-08-26 08:11:35 +00:00
cryptoAmountController.removeListener(onCryptoAmountChanged);
baseAmountController.removeListener(_baseAmountChanged);
2022-08-26 08:11:35 +00:00
sendToController.dispose();
cryptoAmountController.dispose();
baseAmountController.dispose();
noteController.dispose();
2023-07-05 12:57:17 +00:00
onChainNoteController.dispose();
2022-08-26 08:11:35 +00:00
feeController.dispose();
memoController.dispose();
2022-08-26 08:11:35 +00:00
_noteFocusNode.dispose();
2023-07-05 12:57:17 +00:00
_onChainNoteFocusNode.dispose();
2022-08-26 08:11:35 +00:00
_addressFocusNode.dispose();
_cryptoFocus.dispose();
_baseFocus.dispose();
_memoFocus.dispose();
2022-08-26 08:11:35 +00:00
super.dispose();
}
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
final wallet = ref.watch(pWallets).getWallet(walletId);
2022-08-26 08:11:35 +00:00
final String locale = ref.watch(
localeServiceChangeNotifierProvider.select((value) => value.locale),
);
final showCoinControl = wallet is CoinControlInterface &&
2023-03-08 21:19:43 +00:00
ref.watch(
prefsChangeNotifierProvider.select(
(value) => value.enableCoinControl,
),
) &&
(coin is Firo
? ref.watch(publicPrivateBalanceStateProvider) == FiroType.public
: true);
2023-03-07 21:45:22 +00:00
if (isFiro) {
2024-06-25 20:46:36 +00:00
final isExchangeAddress = ref.watch(pIsExchangeAddress);
ref.listen(publicPrivateBalanceStateProvider, (previous, next) {
selectedUTXOs = {};
if (ref.read(pSendAmount) == null) {
setState(() {
2024-05-15 21:20:45 +00:00
_calculateFeesFuture = calculateFees(
2024-05-27 23:56:22 +00:00
0.toAmountAsRaw(fractionDigits: coin.fractionDigits),
);
});
} else {
setState(() {
_calculateFeesFuture = calculateFees(
ref.read(pSendAmount)!,
2023-04-05 22:06:31 +00:00
);
});
}
2024-06-25 20:46:36 +00:00
if (previous != next &&
next == FiroType.spark &&
isExchangeAddress &&
!_isFiroExWarningDisplayed) {
_isFiroExWarningDisplayed = true;
2024-06-25 20:46:36 +00:00
WidgetsBinding.instance.addPostFrameCallback(
(_) => showFiroExchangeAddressWarning(
context,
() => _isFiroExWarningDisplayed = false,
),
2024-06-25 20:46:36 +00:00
);
}
});
}
2023-03-06 23:24:46 +00:00
// add listener for epic cash to strip http:// and https:// prefixes if the address also ocntains an @ symbol (indicating an epicbox address)
2024-05-15 21:20:45 +00:00
if (coin is Epiccash) {
sendToController.addListener(() {
_address = sendToController.text.trim();
if (_address != null && _address!.isNotEmpty) {
_address = _address!.trim();
if (_address!.contains("\n")) {
_address = _address!.substring(0, _address!.indexOf("\n"));
}
sendToController.text = AddressUtils().formatAddress(_address!);
}
});
}
return Background(
child: Scaffold(
backgroundColor: Theme.of(context).extension<StackColors>()!.background,
appBar: AppBar(
leading: AppBarBackButton(
onPressed: () async {
if (FocusScope.of(context).hasFocus) {
FocusScope.of(context).unfocus();
await Future<void>.delayed(const Duration(milliseconds: 50));
}
if (context.mounted) {
Navigator.of(context).pop();
}
},
),
title: Text(
"Send ${coin.ticker}",
style: STextStyles.navBarTitle(context),
),
2022-08-26 08:11:35 +00:00
),
body: LayoutBuilder(
builder: (builderContext, constraints) {
return Padding(
padding: const EdgeInsets.only(
left: 12,
top: 12,
right: 12,
),
child: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
// subtract top and bottom padding set in parent
minHeight: constraints.maxHeight - 24,
),
child: IntrinsicHeight(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
decoration: BoxDecoration(
color: Theme.of(context)
.extension<StackColors>()!
.popupBG,
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
2022-08-26 08:11:35 +00:00
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
children: [
SvgPicture.file(
File(
ref.watch(
coinIconProvider(coin),
),
),
width: 22,
height: 22,
2022-09-06 23:27:14 +00:00
),
const SizedBox(
width: 6,
),
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
ref.watch(pWalletName(walletId)),
style: STextStyles.titleBold12(context)
.copyWith(fontSize: 14),
overflow: TextOverflow.ellipsis,
maxLines: 1,
2022-09-06 23:27:14 +00:00
),
// const SizedBox(
// height: 2,
// ),
if (isFiro)
Text(
"${ref.watch(publicPrivateBalanceStateProvider.state).state.name.capitalize()} balance",
style: STextStyles.label(context)
.copyWith(fontSize: 10),
2022-08-26 08:11:35 +00:00
),
2024-05-15 21:20:45 +00:00
if (coin is! Firo)
Text(
"Available balance",
style: STextStyles.label(context)
.copyWith(fontSize: 10),
),
],
),
const Spacer(),
Builder(
builder: (context) {
final Amount amount;
if (isFiro) {
switch (ref
.watch(
publicPrivateBalanceStateProvider
.state,
)
.state) {
case FiroType.public:
amount = ref
.read(pWalletBalance(walletId))
.spendable;
break;
case FiroType.lelantus:
amount = ref
.read(
pWalletBalanceSecondary(
walletId,
),
)
.spendable;
break;
case FiroType.spark:
amount = ref
.read(
pWalletBalanceTertiary(
walletId,
),
)
.spendable;
break;
}
} else {
amount = ref
.read(pWalletBalance(walletId))
.spendable;
}
return GestureDetector(
onTap: () {
cryptoAmountController.text = ref
.read(pAmountFormatter(coin))
.format(
amount,
withUnitName: false,
);
},
child: Container(
color: Colors.transparent,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.end,
children: [
Text(
ref
.watch(
pAmountFormatter(coin),
)
.format(amount),
style: STextStyles.titleBold12(
context,
).copyWith(
fontSize: 10,
),
textAlign: TextAlign.right,
),
Text(
"${(amount.decimal * ref.watch(priceAnd24hChangeNotifierProvider.select((value) => value.getPrice(coin).item1))).toAmount(
fractionDigits: 2,
).fiatString(
locale: locale,
)} ${ref.watch(prefsChangeNotifierProvider.select((value) => value.currency))}",
style: STextStyles.subtitle(
context,
).copyWith(
fontSize: 8,
),
textAlign: TextAlign.right,
),
],
),
),
);
},
),
],
),
2022-08-26 08:11:35 +00:00
),
),
const SizedBox(
height: 16,
2022-08-26 08:11:35 +00:00
),
2023-05-26 21:17:09 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
2023-06-07 13:36:54 +00:00
isPaynymSend
? "Send to PayNym address"
: "Send to",
2023-05-26 21:17:09 +00:00
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
2024-05-15 21:20:45 +00:00
// if (coin is Monero)
2023-06-07 13:36:54 +00:00
// CustomTextButton(
// text: "Use OpenAlias",
// onTap: () async {
// await showModalBottomSheet(
// context: context,
// builder: (context) =>
// OpenAliasBottomSheet(
// onSelected: (address) {
// sendToController.text = address;
// },
// ),
// );
// },
// ),
2023-05-26 21:17:09 +00:00
],
),
const SizedBox(
height: 8,
),
2023-01-26 17:46:16 +00:00
if (isPaynymSend)
TextField(
key: const Key("sendViewPaynymAddressFieldKey"),
controller: sendToController,
2023-01-26 17:46:16 +00:00
enabled: false,
readOnly: true,
style: STextStyles.fieldLabel(context),
),
if (!isPaynymSend)
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
2022-08-26 08:11:35 +00:00
),
2023-01-26 17:46:16 +00:00
child: TextField(
key: const Key("sendViewAddressFieldKey"),
controller: sendToController,
readOnly: false,
autocorrect: false,
enableSuggestions: false,
// inputFormatters: <TextInputFormatter>[
// FilteringTextInputFormatter.allow(
// RegExp("[a-zA-Z0-9]{34}")),
// ],
toolbarOptions: const ToolbarOptions(
copy: false,
cut: false,
paste: true,
selectAll: false,
),
2023-01-26 17:46:16 +00:00
onChanged: (newValue) {
_address = newValue.trim();
_setValidAddressProviders(_address);
2023-01-26 17:46:16 +00:00
setState(() {
_addressToggleFlag = newValue.isNotEmpty;
});
},
focusNode: _addressFocusNode,
style: STextStyles.field(context),
decoration: standardInputDecoration(
"Enter ${coin.ticker} address",
_addressFocusNode,
context,
).copyWith(
contentPadding: const EdgeInsets.only(
left: 16,
top: 6,
bottom: 8,
right: 5,
),
suffixIcon: Padding(
padding: sendToController.text.isEmpty
? const EdgeInsets.only(right: 8)
: const EdgeInsets.only(right: 0),
child: UnconstrainedBox(
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
_addressToggleFlag
? TextFieldIconButton(
semanticsLabel:
"Clear Button. Clears The Address Field Input.",
2023-01-26 17:46:16 +00:00
key: const Key(
"sendViewClearAddressFieldButtonKey",
),
2023-01-26 17:46:16 +00:00
onTap: () {
sendToController.text = "";
_address = "";
_setValidAddressProviders(
_address,
);
2023-01-26 17:46:16 +00:00
setState(() {
_addressToggleFlag =
false;
});
},
child: const XIcon(),
)
: TextFieldIconButton(
semanticsLabel:
"Paste Button. Pastes From Clipboard To Address Field Input.",
2023-01-26 17:46:16 +00:00
key: const Key(
"sendViewPasteAddressFieldButtonKey",
),
2023-01-26 17:46:16 +00:00
onTap: () async {
final ClipboardData? data =
await clipboard.getData(
Clipboard.kTextPlain,
);
2023-01-26 17:46:16 +00:00
if (data?.text != null &&
data!
.text!.isNotEmpty) {
String content =
data.text!.trim();
if (content
.contains("\n")) {
content =
content.substring(
0,
content.indexOf(
"\n",
),
);
2023-01-26 17:46:16 +00:00
}
2024-05-15 21:20:45 +00:00
if (coin is Epiccash) {
// strip http:// and https:// if content contains @
content = AddressUtils()
.formatAddress(
content,
);
}
2023-01-26 17:46:16 +00:00
sendToController.text =
content.trim();
_address = content.trim();
2023-01-26 17:46:16 +00:00
_setValidAddressProviders(
_address,
);
2023-01-26 17:46:16 +00:00
setState(() {
_addressToggleFlag =
sendToController
.text
.isNotEmpty;
});
}
2023-01-26 17:46:16 +00:00
},
child: sendToController
.text.isEmpty
? const ClipboardIcon()
: const XIcon(),
),
if (sendToController.text.isEmpty)
TextFieldIconButton(
semanticsLabel:
"Address Book Button. Opens Address Book For Address Field.",
2023-01-26 17:46:16 +00:00
key: const Key(
"sendViewAddressBookButtonKey",
),
2023-01-26 17:46:16 +00:00
onTap: () {
Navigator.of(context).pushNamed(
AddressBookView.routeName,
arguments: widget.coin,
);
},
child: const AddressBookIcon(),
),
if (sendToController.text.isEmpty)
TextFieldIconButton(
semanticsLabel:
"Scan QR Button. Opens Camera For Scanning QR Code.",
2023-01-26 17:46:16 +00:00
key: const Key(
"sendViewScanQrButtonKey",
),
onTap: _scanQr,
2023-01-26 17:46:16 +00:00
child: const QrCodeIcon(),
),
2023-01-26 17:46:16 +00:00
],
),
),
2022-08-26 08:11:35 +00:00
),
),
),
),
const SizedBox(
height: 10,
),
if (isStellar ||
(ref.watch(pValidSparkSendToAddress) &&
ref.watch(
publicPrivateBalanceStateProvider,
) !=
FiroType.lelantus))
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
key: const Key("sendViewMemoFieldKey"),
2024-05-15 21:20:45 +00:00
maxLength: (coin is Firo) ? 31 : null,
controller: memoController,
readOnly: false,
autocorrect: false,
enableSuggestions: false,
focusNode: _memoFocus,
style: STextStyles.field(context),
onChanged: (_) {
setState(() {});
},
decoration: standardInputDecoration(
"Enter memo (optional)",
_memoFocus,
context,
).copyWith(
counterText: '',
contentPadding: const EdgeInsets.only(
left: 16,
top: 6,
bottom: 8,
right: 5,
),
suffixIcon: Padding(
padding: memoController.text.isEmpty
? const EdgeInsets.only(right: 8)
: const EdgeInsets.only(right: 0),
child: UnconstrainedBox(
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
memoController.text.isNotEmpty
? TextFieldIconButton(
semanticsLabel:
"Clear Button. Clears The Memo Field Input.",
key: const Key(
"sendViewClearMemoFieldButtonKey",
),
onTap: () {
memoController.text = "";
setState(() {});
},
child: const XIcon(),
)
: TextFieldIconButton(
semanticsLabel:
"Paste Button. Pastes From Clipboard To Memo Field Input.",
key: const Key(
"sendViewPasteMemoFieldButtonKey",
),
onTap: () async {
final ClipboardData? data =
await clipboard.getData(
Clipboard.kTextPlain,
);
if (data?.text != null &&
data!
.text!.isNotEmpty) {
final String content =
data.text!.trim();
memoController.text =
content.trim();
setState(() {});
}
},
child: const ClipboardIcon(),
),
],
),
),
),
),
),
),
Builder(
builder: (_) {
final String? error;
if (_address == null || _address!.isEmpty) {
error = null;
} else if (isFiro) {
if (ref.watch(
publicPrivateBalanceStateProvider,
) ==
FiroType.lelantus) {
if (_data != null &&
_data!.contactLabel == _address) {
error = SparkInterface.validateSparkAddress(
address: _data!.address,
2024-05-15 21:20:45 +00:00
isTestNet: coin.network ==
CryptoCurrencyNetwork.test,
)
? "Unsupported"
: null;
} else if (ref
.watch(pValidSparkSendToAddress)) {
error = "Unsupported";
} else {
error = ref.watch(pValidSendToAddress)
? null
: "Invalid address";
}
} else {
if (_data != null &&
_data!.contactLabel == _address) {
error = null;
} else if (!ref.watch(pValidSendToAddress) &&
!ref.watch(pValidSparkSendToAddress)) {
error = "Invalid address";
} else {
error = null;
}
}
} else {
if (_data != null &&
_data!.contactLabel == _address) {
error = null;
} else if (!ref.watch(pValidSendToAddress)) {
error = "Invalid address";
} else {
error = null;
}
}
if (error == null || error.isEmpty) {
return Container();
} else {
return Align(
alignment: Alignment.topLeft,
child: Padding(
padding: const EdgeInsets.only(
left: 12.0,
top: 4.0,
),
child: Text(
error,
textAlign: TextAlign.left,
style:
STextStyles.label(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textError,
),
2022-08-26 08:11:35 +00:00
),
),
);
}
},
2022-09-06 23:27:14 +00:00
),
if (isFiro)
const SizedBox(
height: 12,
),
if (isFiro)
Text(
"Send from",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
if (isFiro)
const SizedBox(
height: 8,
),
if (isFiro)
Stack(
children: [
TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions:
Util.isDesktop ? false : true,
readOnly: true,
textInputAction: TextInputAction.none,
2022-09-06 23:27:14 +00:00
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12,
2022-09-06 23:27:14 +00:00
),
child: RawMaterialButton(
splashColor: Theme.of(context)
.extension<StackColors>()!
.highlight,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
2022-09-06 23:27:14 +00:00
),
),
onPressed: () {
showModalBottomSheet<dynamic>(
backgroundColor: Colors.transparent,
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
2022-09-06 23:27:14 +00:00
),
),
builder: (_) =>
FiroBalanceSelectionSheet(
walletId: walletId,
),
);
},
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
"${ref.watch(publicPrivateBalanceStateProvider.state).state.name.capitalize()} balance",
style: STextStyles.itemSubtitle12(
context,
),
),
const SizedBox(
width: 10,
),
Builder(
builder: (_) {
final Amount amount;
switch (ref
.read(
publicPrivateBalanceStateProvider
.state,
)
.state) {
case FiroType.public:
amount = ref
.watch(
pWalletBalance(
walletId,
),
)
.spendable;
break;
case FiroType.lelantus:
amount = ref
.watch(
pWalletBalanceSecondary(
walletId,
),
)
.spendable;
break;
case FiroType.spark:
amount = ref
.watch(
pWalletBalanceTertiary(
walletId,
),
)
.spendable;
break;
}
return Text(
ref
.watch(
pAmountFormatter(coin),
)
.format(
amount,
),
style:
STextStyles.itemSubtitle(
context,
),
);
},
),
],
),
SvgPicture.asset(
Assets.svg.chevronDown,
width: 8,
height: 4,
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle2,
),
],
),
2022-09-06 23:27:14 +00:00
),
),
],
2022-08-26 08:11:35 +00:00
),
const SizedBox(
height: 12,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Amount",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
2024-05-15 21:20:45 +00:00
if (coin is! Ethereum && coin is! Tezos)
2023-03-31 21:42:28 +00:00
CustomTextButton(
text: _getSendAllTitle(
showCoinControl, selectedUTXOs),
onTap: () => _sendAllTapped(showCoinControl),
2023-03-31 21:42:28 +00:00
),
],
2022-08-26 08:11:35 +00:00
),
const SizedBox(
height: 8,
2022-08-26 08:11:35 +00:00
),
TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions: Util.isDesktop ? false : true,
style: STextStyles.smallMed14(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textDark,
2022-08-26 08:11:35 +00:00
),
key:
const Key("amountInputFieldCryptoTextFieldKey"),
controller: cryptoAmountController,
focusNode: _cryptoFocus,
2022-12-30 17:36:00 +00:00
keyboardType: Util.isDesktop
? null
: const TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
textAlign: TextAlign.right,
inputFormatters: [
AmountInputFormatter(
2024-05-15 21:20:45 +00:00
decimals: coin.fractionDigits,
2023-06-16 23:04:39 +00:00
unit: ref.watch(pAmountUnit(coin)),
locale: locale,
),
// regex to validate a crypto amount with 8 decimal places
// TextInputFormatter.withFunction((oldValue,
// newValue) =>
// // RegExp(r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$')
// // RegExp(r'^\d{1,3}([,\.]\d+)?|[,\.\d]+$')
2024-05-15 21:20:45 +00:00
// getAmountRegex(locale, coin.fractionDigits)
// .hasMatch(newValue.text)
// ? newValue
// : oldValue),
],
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(
top: 12,
right: 12,
),
hintText: "0",
hintStyle:
STextStyles.fieldLabel(context).copyWith(
fontSize: 14,
),
prefixIcon: FittedBox(
fit: BoxFit.scaleDown,
child: Padding(
padding: const EdgeInsets.all(12),
child: Text(
2023-05-30 15:02:09 +00:00
ref
.watch(pAmountUnit(coin))
.unitForCoin(coin),
style: STextStyles.smallMed14(context)
.copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark,
),
),
2022-08-26 08:11:35 +00:00
),
),
),
),
if (Prefs.instance.externalCalls)
const SizedBox(
height: 8,
2022-08-26 08:11:35 +00:00
),
if (Prefs.instance.externalCalls)
2022-08-26 08:11:35 +00:00
TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions: Util.isDesktop ? false : true,
style: STextStyles.smallMed14(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textDark,
),
key:
const Key("amountInputFieldFiatTextFieldKey"),
controller: baseAmountController,
focusNode: _baseFocus,
2022-12-30 17:36:00 +00:00
keyboardType: Util.isDesktop
? null
: const TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
textAlign: TextAlign.right,
inputFormatters: [
AmountInputFormatter(
decimals: 2,
locale: locale,
),
// regex to validate a fiat amount with 2 decimal places
// TextInputFormatter.withFunction((oldValue,
// newValue) =>
// // RegExp(r'^([0-9]*[,.]?[0-9]{0,2}|[,.][0-9]{0,2})$')
// getAmountRegex(locale, 2)
// .hasMatch(newValue.text)
// ? newValue
// : oldValue),
],
onChanged: _fiatFieldChanged,
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(
top: 12,
right: 12,
),
hintText: "0",
hintStyle:
STextStyles.fieldLabel(context).copyWith(
fontSize: 14,
),
prefixIcon: FittedBox(
fit: BoxFit.scaleDown,
child: Padding(
padding: const EdgeInsets.all(12),
child: Text(
ref.watch(
prefsChangeNotifierProvider
.select((value) => value.currency),
),
style: STextStyles.smallMed14(context)
.copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark,
),
),
),
),
),
),
2023-03-07 21:45:22 +00:00
if (showCoinControl)
const SizedBox(
height: 8,
),
if (showCoinControl)
RoundedWhiteContainer(
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
"Coin control",
style:
STextStyles.w500_14(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
),
CustomTextButton(
text: selectedUTXOs.isEmpty
? "Select coins"
: "Selected coins (${selectedUTXOs.length})",
onTap: () async {
if (FocusScope.of(context).hasFocus) {
FocusScope.of(context).unfocus();
await Future<void>.delayed(
const Duration(milliseconds: 100),
);
}
if (context.mounted) {
final spendable = ref
.read(pWalletBalance(walletId))
.spendable;
2023-04-05 22:06:31 +00:00
Amount? amount;
if (ref.read(pSendAmount) != null) {
amount = ref.read(pSendAmount)!;
if (spendable == amount) {
// this is now a send all
} else {
amount += _currentFee;
}
}
final result =
await Navigator.of(context)
.pushNamed(
CoinControlView.routeName,
arguments: Tuple4(
walletId,
CoinControlViewType.use,
amount,
selectedUTXOs,
),
);
2023-03-07 21:45:22 +00:00
if (result is Set<UTXO>) {
setState(() {
selectedUTXOs = result;
});
}
2023-03-07 21:45:22 +00:00
}
},
),
],
),
),
const SizedBox(
height: 12,
),
2024-05-15 21:20:45 +00:00
if (coin is Epiccash)
2023-07-05 12:57:17 +00:00
Text(
"On chain Note (optional)",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
2024-05-15 21:20:45 +00:00
if (coin is Epiccash)
2023-07-05 12:57:17 +00:00
const SizedBox(
height: 8,
),
2024-05-15 21:20:45 +00:00
if (coin is Epiccash)
2023-07-05 12:57:17 +00:00
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions:
Util.isDesktop ? false : true,
2023-07-05 12:57:17 +00:00
maxLength: 256,
controller: onChainNoteController,
focusNode: _onChainNoteFocusNode,
style: STextStyles.field(context),
onChanged: (_) => setState(() {}),
decoration: standardInputDecoration(
"Type something...",
_onChainNoteFocusNode,
context,
).copyWith(
suffixIcon: onChainNoteController
.text.isNotEmpty
2023-07-05 12:57:17 +00:00
? Padding(
padding:
const EdgeInsets.only(right: 0),
child: UnconstrainedBox(
child: Row(
children: [
TextFieldIconButton(
child: const XIcon(),
onTap: () async {
setState(() {
onChainNoteController
.text = "";
});
},
),
],
),
2023-07-05 12:57:17 +00:00
),
)
2023-07-05 12:57:17 +00:00
: null,
),
),
),
2024-05-15 21:20:45 +00:00
if (coin is Epiccash)
const SizedBox(
height: 12,
),
Text(
2024-05-15 21:20:45 +00:00
(coin is Epiccash)
? "Local Note (optional)"
: "Note (optional)",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
const SizedBox(
height: 8,
),
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions: Util.isDesktop ? false : true,
controller: noteController,
focusNode: _noteFocusNode,
style: STextStyles.field(context),
onChanged: (_) => setState(() {}),
decoration: standardInputDecoration(
"Type something...",
_noteFocusNode,
context,
).copyWith(
suffixIcon: noteController.text.isNotEmpty
? Padding(
padding:
const EdgeInsets.only(right: 0),
child: UnconstrainedBox(
child: Row(
children: [
TextFieldIconButton(
child: const XIcon(),
onTap: () async {
setState(() {
noteController.text = "";
});
},
),
],
),
),
)
: null,
2022-08-26 08:11:35 +00:00
),
),
),
const SizedBox(
height: 12,
),
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash &&
coin is! NanoCurrency &&
coin is! Tezos)
2022-12-06 14:36:03 +00:00
Text(
"Transaction fee (estimated)",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash &&
coin is! NanoCurrency &&
coin is! Tezos)
2022-12-06 14:36:03 +00:00
const SizedBox(
height: 8,
),
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash &&
coin is! NanoCurrency &&
coin is! Tezos)
2022-12-06 14:36:03 +00:00
Stack(
children: [
TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions:
Util.isDesktop ? false : true,
controller: feeController,
readOnly: true,
textInputAction: TextInputAction.none,
),
2022-12-06 14:36:03 +00:00
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12,
),
2022-12-06 14:36:03 +00:00
child: RawMaterialButton(
splashColor: Theme.of(context)
.extension<StackColors>()!
.highlight,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
onPressed: isFiro &&
2022-12-06 14:36:03 +00:00
ref
.watch(
publicPrivateBalanceStateProvider
.state,
)
.state !=
FiroType.public
2022-12-06 14:36:03 +00:00
? null
: () {
showModalBottomSheet<dynamic>(
backgroundColor:
Colors.transparent,
context: context,
shape:
const RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(
top: Radius.circular(20),
),
),
2022-12-06 14:36:03 +00:00
builder: (_) =>
TransactionFeeSelectionSheet(
walletId: walletId,
2023-04-05 22:06:31 +00:00
amount: (Decimal.tryParse(
cryptoAmountController
.text,
) ??
ref
.watch(pSendAmount)
2023-05-30 15:02:09 +00:00
?.decimal ??
2023-04-05 22:06:31 +00:00
Decimal.zero)
.toAmount(
2024-05-15 21:20:45 +00:00
fractionDigits:
coin.fractionDigits,
2023-04-05 22:06:31 +00:00
),
2022-12-06 14:36:03 +00:00
updateChosen: (String fee) {
if (fee == "custom") {
if (!isCustomFee) {
setState(() {
isCustomFee = true;
});
}
return;
}
_setCurrentFee(
fee,
true,
);
2022-12-06 14:36:03 +00:00
setState(() {
_calculateFeesFuture =
Future(() => fee);
if (isCustomFee) {
isCustomFee = false;
}
2022-12-06 14:36:03 +00:00
});
},
),
);
},
child: (isFiro &&
2022-12-06 14:36:03 +00:00
ref
.watch(
publicPrivateBalanceStateProvider
.state,
)
.state !=
FiroType.public)
2022-12-06 14:36:03 +00:00
? Row(
children: [
FutureBuilder(
future: _calculateFeesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState
.done &&
snapshot.hasData) {
_setCurrentFee(
2023-06-17 00:32:56 +00:00
snapshot.data!,
false,
);
2022-12-06 14:36:03 +00:00
return Text(
2023-06-17 00:32:56 +00:00
"~${snapshot.data!}",
2022-12-06 14:36:03 +00:00
style: STextStyles
.itemSubtitle(
context,
),
2022-12-06 14:36:03 +00:00
);
} else {
return AnimatedText(
stringsToLoopThrough: const [
"Calculating",
"Calculating.",
"Calculating..",
"Calculating...",
],
style: STextStyles
.itemSubtitle(
context,
),
2022-12-06 14:36:03 +00:00
);
}
},
),
],
)
: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
ref
.watch(
feeRateTypeStateProvider
.state,
)
2022-12-06 14:36:03 +00:00
.state
.prettyName,
style: STextStyles
2022-12-06 14:36:03 +00:00
.itemSubtitle12(
context,
),
2022-12-06 14:36:03 +00:00
),
const SizedBox(
width: 10,
),
FutureBuilder(
future:
_calculateFeesFuture,
builder:
(context, snapshot) {
if (snapshot.connectionState ==
ConnectionState
.done &&
snapshot.hasData) {
_setCurrentFee(
snapshot.data!,
false,
);
2022-12-06 14:36:03 +00:00
return Text(
isCustomFee
? ""
: "~${snapshot.data!}",
2022-12-06 14:36:03 +00:00
style: STextStyles
.itemSubtitle(
context,
),
2022-12-06 14:36:03 +00:00
);
} else {
return AnimatedText(
stringsToLoopThrough: const [
"Calculating",
"Calculating.",
"Calculating..",
"Calculating...",
],
style: STextStyles
.itemSubtitle(
context,
),
2022-12-06 14:36:03 +00:00
);
}
},
),
],
),
SvgPicture.asset(
Assets.svg.chevronDown,
width: 8,
height: 4,
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle2,
),
],
),
),
),
2022-12-06 14:36:03 +00:00
],
),
if (isCustomFee)
Padding(
padding: const EdgeInsets.only(
bottom: 12,
top: 16,
),
child: FeeSlider(
coin: coin,
onSatVByteChanged: (rate) {
customFeeRate = rate;
},
),
),
const Spacer(),
const SizedBox(
height: 12,
),
TextButton(
onPressed: ref.watch(pPreviewTxButtonEnabled(coin))
2023-01-26 18:16:38 +00:00
? _previewTransaction
: null,
style: ref.watch(pPreviewTxButtonEnabled(coin))
? Theme.of(context)
.extension<StackColors>()!
2023-01-24 19:29:12 +00:00
.getPrimaryEnabledButtonStyle(context)
: Theme.of(context)
.extension<StackColors>()!
2023-01-24 19:29:12 +00:00
.getPrimaryDisabledButtonStyle(context),
child: Text(
"Preview",
style: STextStyles.button(context),
),
),
const SizedBox(
height: 4,
2022-08-26 08:11:35 +00:00
),
],
),
2022-08-26 08:11:35 +00:00
),
),
),
),
);
},
),
2022-08-26 08:11:35 +00:00
),
);
}
}