stack_wallet/lib/pages/send_view/token_send_view.dart

1344 lines
56 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
*
*/
2023-03-30 16:55:02 +00:00
import 'dart:async';
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-07-29 22:04:23 +00:00
import '../../models/isar/models/isar_models.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 '../../route_generator.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/logger.dart';
import '../../utilities/prefs.dart';
import '../../utilities/text_styles.dart';
import '../../utilities/util.dart';
import '../../wallets/crypto_currency/crypto_currency.dart';
import '../../wallets/isar/providers/eth/current_token_wallet_provider.dart';
import '../../wallets/isar/providers/eth/token_balance_provider.dart';
import '../../wallets/isar/providers/wallet_info_provider.dart';
import '../../wallets/models/tx_data.dart';
import '../../widgets/animated_text.dart';
import '../../widgets/background.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/icon_widgets/addressbook_icon.dart';
import '../../widgets/icon_widgets/clipboard_icon.dart';
import '../../widgets/icon_widgets/eth_token_icon.dart';
import '../../widgets/icon_widgets/qrcode_icon.dart';
import '../../widgets/icon_widgets/x_icon.dart';
import '../../widgets/stack_dialog.dart';
import '../../widgets/stack_text_field.dart';
import '../../widgets/textfield_icon_button.dart';
2024-07-29 22:04:23 +00:00
import '../address_book_views/address_book_view.dart';
import '../token_view/token_view.dart';
2024-07-29 22:04:23 +00:00
import 'confirm_transaction_view.dart';
import 'sub_widgets/building_transaction_dialog.dart';
import 'sub_widgets/transaction_fee_selection_sheet.dart';
2023-03-30 16:55:02 +00:00
class TokenSendView extends ConsumerStatefulWidget {
const TokenSendView({
super.key,
2023-03-30 16:55:02 +00:00
required this.walletId,
required this.coin,
required this.tokenContract,
this.autoFillData,
this.clipboard = const ClipboardWrapper(),
this.barcodeScanner = const BarcodeScannerWrapper(),
});
2023-03-30 16:55:02 +00:00
static const String routeName = "/tokenSendView";
final String walletId;
2024-05-15 21:20:45 +00:00
final CryptoCurrency coin;
2023-03-30 16:55:02 +00:00
final EthContract tokenContract;
final SendViewAutoFillData? autoFillData;
final ClipboardInterface clipboard;
final BarcodeScannerInterface barcodeScanner;
@override
ConsumerState<TokenSendView> createState() => _TokenSendViewState();
}
class _TokenSendViewState extends ConsumerState<TokenSendView> {
late final String walletId;
2024-05-15 21:20:45 +00:00
late final CryptoCurrency coin;
2023-03-30 16:55:02 +00:00
late final EthContract tokenContract;
late final ClipboardInterface clipboard;
late final BarcodeScannerInterface scanner;
late TextEditingController sendToController;
late TextEditingController cryptoAmountController;
late TextEditingController baseAmountController;
late TextEditingController noteController;
late TextEditingController feeController;
late final SendViewAutoFillData? _data;
final _addressFocusNode = FocusNode();
final _noteFocusNode = FocusNode();
final _cryptoFocus = FocusNode();
final _baseFocus = FocusNode();
Amount? _amountToSend;
Amount? _cachedAmountToSend;
String? _address;
bool _addressToggleFlag = false;
bool _cryptoAmountChangeLock = false;
late VoidCallback onCryptoAmountChanged;
final updateFeesTimerDuration = const Duration(milliseconds: 500);
Timer? _cryptoAmountChangedFeeUpdateTimer;
Timer? _baseAmountChangedFeeUpdateTimer;
late Future<String> _calculateFeesFuture;
2023-04-05 22:06:31 +00:00
String cachedFees = "";
2023-03-30 16:55:02 +00:00
void _onTokenSendViewPasteAddressFieldButtonPressed() async {
final ClipboardData? data = await clipboard.getData(Clipboard.kTextPlain);
if (data?.text != null && data!.text!.isNotEmpty) {
String content = data.text!.trim();
if (content.contains("\n")) {
content = content.substring(0, content.indexOf("\n"));
}
sendToController.text = content.trim();
_address = content.trim();
_updatePreviewButtonState(_address, _amountToSend);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
}
}
void _onTokenSendViewScanQrButtonPressed() 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,
);
2023-03-30 16:55:02 +00:00
final paymentData = AddressUtils.parsePaymentUri(
qrResult.rawContent,
logging: Logging.instance,
);
2023-03-30 16:55:02 +00:00
Logging.instance
.log("qrResult parsed: $paymentData", level: LogLevel.Info);
2023-03-30 16:55:02 +00:00
if (paymentData != null &&
paymentData.coin?.uriScheme == coin.uriScheme) {
2023-03-30 16:55:02 +00:00
// auto fill address
_address = paymentData.address.trim();
2023-03-30 16:55:02 +00:00
sendToController.text = _address!;
// autofill notes field
if (paymentData.message != null) {
noteController.text = paymentData.message!;
} else if (paymentData.label != null) {
noteController.text = paymentData.label!;
2023-03-30 16:55:02 +00:00
}
// autofill amount field
if (paymentData.amount != null) {
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
2023-03-30 16:55:02 +00:00
fractionDigits: tokenContract.decimals,
);
2023-05-29 21:10:55 +00:00
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
amount,
withUnitName: false,
2023-05-30 15:02:09 +00:00
indicatePrecisionLoss: false,
2023-05-29 21:10:55 +00:00
);
2023-04-05 22:06:31 +00:00
_amountToSend = amount;
2023-03-30 16:55:02 +00:00
}
_updatePreviewButtonState(_address, _amountToSend);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
// now check for non standard encoded basic address
} else {
_address = qrResult.rawContent.split("\n").first.trim();
2023-03-30 16:55:02 +00:00
sendToController.text = _address ?? "";
_updatePreviewButtonState(_address, _amountToSend);
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,
);
2023-03-30 16:55:02 +00:00
}
}
void _onFiatAmountFieldChanged(String baseAmountString) {
final baseAmount = Amount.tryParseFiatString(
baseAmountString,
locale: ref.read(localeServiceChangeNotifierProvider).locale,
);
if (baseAmount != null) {
2023-03-30 16:55:02 +00:00
final _price = ref
.read(priceAnd24hChangeNotifierProvider)
.getTokenPrice(tokenContract.address)
.item1;
if (_price == Decimal.zero) {
_amountToSend = Amount.zero;
} else {
_amountToSend = baseAmount <= Amount.zero
? Amount.zero
: Amount.fromDecimal(
(baseAmount.decimal / _price).toDecimal(
scaleOnInfinitePrecision: tokenContract.decimals,
),
fractionDigits: tokenContract.decimals,
);
2023-03-30 16:55:02 +00:00
}
if (_cachedAmountToSend != null && _cachedAmountToSend == _amountToSend) {
return;
}
_cachedAmountToSend = _amountToSend;
Logging.instance.log(
"it changed $_amountToSend $_cachedAmountToSend",
level: LogLevel.Info,
);
2023-03-30 16:55:02 +00:00
2023-04-05 22:06:31 +00:00
_cryptoAmountChangeLock = true;
2023-05-29 21:10:55 +00:00
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
_amountToSend!,
withUnitName: false,
);
2023-03-30 16:55:02 +00:00
_cryptoAmountChangeLock = false;
} else {
_amountToSend = Amount.zero;
_cryptoAmountChangeLock = true;
cryptoAmountController.text = "";
_cryptoAmountChangeLock = false;
}
// setState(() {
// _calculateFeesFuture = calculateFees(
// Format.decimalAmountToSatoshis(
// _amountToSend!));
// });
_updatePreviewButtonState(_address, _amountToSend);
}
void _cryptoAmountChanged() async {
if (!_cryptoAmountChangeLock) {
final cryptoAmount = ref.read(pAmountFormatter(coin)).tryParse(
cryptoAmountController.text,
ethContract: tokenContract,
);
if (cryptoAmount != null) {
_amountToSend = cryptoAmount;
2023-03-30 16:55:02 +00:00
if (_cachedAmountToSend != null &&
_cachedAmountToSend == _amountToSend) {
return;
}
_cachedAmountToSend = _amountToSend;
Logging.instance.log(
"it changed $_amountToSend $_cachedAmountToSend",
level: LogLevel.Info,
);
2023-03-30 16:55:02 +00:00
final price = ref
.read(priceAnd24hChangeNotifierProvider)
.getTokenPrice(tokenContract.address)
.item1;
if (price > Decimal.zero) {
2023-04-05 22:06:31 +00:00
baseAmountController.text = (_amountToSend!.decimal * price)
.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,
);
2023-03-30 16:55:02 +00:00
}
} else {
_amountToSend = null;
baseAmountController.text = "";
}
_updatePreviewButtonState(_address, _amountToSend);
_cryptoAmountChangedFeeUpdateTimer?.cancel();
_cryptoAmountChangedFeeUpdateTimer = Timer(updateFeesTimerDuration, () {
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash && !_baseFocus.hasFocus) {
2023-03-30 16:55:02 +00:00
setState(() {
2023-04-05 22:06:31 +00:00
_calculateFeesFuture = calculateFees();
2023-03-30 16:55:02 +00:00
});
}
});
}
}
void _baseAmountChanged() {
_baseAmountChangedFeeUpdateTimer?.cancel();
_baseAmountChangedFeeUpdateTimer = Timer(updateFeesTimerDuration, () {
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash && !_cryptoFocus.hasFocus) {
2023-03-30 16:55:02 +00:00
setState(() {
2023-04-05 22:06:31 +00:00
_calculateFeesFuture = calculateFees();
2023-03-30 16:55:02 +00:00
});
}
});
}
String? _updateInvalidAddressText(String address) {
2023-03-30 16:55:02 +00:00
if (_data != null && _data!.contactLabel == address) {
return null;
}
if (address.isNotEmpty &&
!ref
.read(pWallets)
.getWallet(walletId)
.cryptoCurrency
.validateAddress(address)) {
2023-03-30 16:55:02 +00:00
return "Invalid address";
}
return null;
}
void _updatePreviewButtonState(String? address, Amount? amount) {
final isValidAddress = ref
.read(pWallets)
.getWallet(walletId)
.cryptoCurrency
.validateAddress(address ?? "");
ref.read(previewTokenTxButtonStateProvider.state).state =
2023-03-30 16:55:02 +00:00
(isValidAddress && amount != null && amount > Amount.zero);
}
2023-04-05 22:06:31 +00:00
Future<String> calculateFees() async {
2024-01-10 22:53:12 +00:00
final wallet = ref.read(pCurrentTokenWallet)!;
2023-03-30 16:55:02 +00:00
final feeObject = await wallet.fees;
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;
2023-03-30 16:55:02 +00:00
}
2024-01-10 22:53:12 +00:00
final Amount fee = await wallet.estimateFeeFor(Amount.zero, feeRate);
2023-05-29 21:10:55 +00:00
cachedFees = 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
);
2023-03-30 16:55:02 +00:00
2023-04-05 22:06:31 +00:00
return cachedFees;
2023-03-30 16:55:02 +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);
2024-01-10 22:53:12 +00:00
final tokenWallet = ref.read(pCurrentTokenWallet)!;
2023-03-30 16:55:02 +00:00
final Amount amount = _amountToSend!;
2023-03-31 21:42:28 +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);
// },
// ),
// rightButton: TextButton(
// style: Theme.of(context)
// .extension<StackColors>()!
// .getPrimaryEnabledButtonStyle(context),
// child: Text(
// "Yes",
// style: STextStyles.button(context),
// ),
// onPressed: () {
// Navigator.of(context).pop(true);
// },
// ),
// );
// },
// );
// }
//
// if (shouldSendAll == null || shouldSendAll == false) {
// // cancel preview
// return;
// }
// }
2023-03-30 16:55:02 +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: false,
2023-03-30 16:55:02 +00:00
onCancel: () {
wasCancelled = true;
Navigator.of(context).pop();
},
);
},
),
);
}
final time = Future<dynamic>.delayed(
const Duration(
milliseconds: 2500,
),
);
TxData txData;
Future<TxData> txDataFuture;
2023-03-30 16:55:02 +00:00
txDataFuture = tokenWallet.prepareSend(
txData: TxData(
recipients: [
(
address: _address!,
amount: amount,
2024-01-05 19:45:42 +00:00
isChange: false,
),
],
feeRateType: ref.read(feeRateTypeStateProvider),
note: noteController.text,
),
2023-03-30 16:55:02 +00:00
);
final results = await Future.wait([
txDataFuture,
time,
]);
txData = results.first as TxData;
2023-03-30 16:55:02 +00:00
if (!wasCancelled && mounted) {
// pop building dialog
Navigator.of(context).pop();
unawaited(
Navigator.of(context).push(
RouteGenerator.getRoute(
shouldUseMaterialRoute: RouteGenerator.useMaterialPageRoute,
builder: (_) => ConfirmTransactionView(
txData: txData,
walletId: walletId,
isTokenTx: true,
onSuccess: clearSendForm,
routeOnSuccessName: TokenView.routeName,
),
settings: const RouteSettings(
name: ConfirmTransactionView.routeName,
),
2023-03-30 16:55:02 +00:00
),
),
);
2023-03-30 16:55:02 +00:00
}
2024-07-29 22:04:23 +00:00
} catch (e, s) {
Logging.instance.log("$e\n$s", level: LogLevel.Error);
2023-03-30 16:55:02 +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-03-30 16:55:02 +00:00
),
2024-01-04 18:52:00 +00:00
);
},
),
);
2023-03-30 16:55:02 +00:00
}
}
}
2024-01-04 18:52:00 +00:00
void clearSendForm() {
sendToController.text = "";
cryptoAmountController.text = "";
baseAmountController.text = "";
noteController.text = "";
feeController.text = "";
_address = "";
_addressToggleFlag = false;
if (mounted) {
setState(() {});
}
}
2023-03-30 16:55:02 +00:00
@override
void initState() {
ref.refresh(feeSheetSessionCacheProvider);
2023-04-05 22:06:31 +00:00
_calculateFeesFuture = calculateFees();
2023-03-30 16:55:02 +00:00
_data = widget.autoFillData;
walletId = widget.walletId;
coin = widget.coin;
tokenContract = widget.tokenContract;
clipboard = widget.clipboard;
scanner = widget.barcodeScanner;
sendToController = TextEditingController();
cryptoAmountController = TextEditingController();
baseAmountController = TextEditingController();
noteController = TextEditingController();
feeController = TextEditingController();
onCryptoAmountChanged = _cryptoAmountChanged;
cryptoAmountController.addListener(onCryptoAmountChanged);
baseAmountController.addListener(_baseAmountChanged);
if (_data != null) {
if (_data!.amount != null) {
cryptoAmountController.text = _data!.amount!.toString();
}
sendToController.text = _data!.contactLabel;
_address = _data!.address.trim();
_addressToggleFlag = true;
}
super.initState();
}
@override
void dispose() {
_cryptoAmountChangedFeeUpdateTimer?.cancel();
_baseAmountChangedFeeUpdateTimer?.cancel();
cryptoAmountController.removeListener(onCryptoAmountChanged);
baseAmountController.removeListener(_baseAmountChanged);
sendToController.dispose();
cryptoAmountController.dispose();
baseAmountController.dispose();
noteController.dispose();
feeController.dispose();
_noteFocusNode.dispose();
_addressFocusNode.dispose();
_cryptoFocus.dispose();
_baseFocus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
final String locale = ref.watch(
localeServiceChangeNotifierProvider.select((value) => value.locale),
);
2023-03-30 16:55:02 +00:00
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) {
2023-03-30 16:55:02 +00:00
Navigator.of(context).pop();
}
},
),
title: Text(
"Send ${tokenContract.symbol}",
style: STextStyles.navBarTitle(context),
),
),
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,
),
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
children: [
EthTokenIcon(
contractAddress: tokenContract.address,
),
const SizedBox(
width: 6,
),
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
ref.watch(pWalletName(walletId)),
2023-03-30 16:55:02 +00:00
style: STextStyles.titleBold12(context)
.copyWith(fontSize: 14),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
Text(
"Available balance",
style: STextStyles.label(context)
.copyWith(fontSize: 10),
),
],
),
const Spacer(),
GestureDetector(
onTap: () {
cryptoAmountController.text = ref
2023-05-29 21:10:55 +00:00
.watch(pAmountFormatter(coin))
.format(
ref
.read(
pTokenBalance(
(
walletId: widget.walletId,
contractAddress:
tokenContract.address,
),
),
)
2023-05-29 21:10:55 +00:00
.spendable,
2023-05-30 15:02:09 +00:00
ethContract: tokenContract,
2023-05-29 21:10:55 +00:00
withUnitName: false,
2023-05-30 15:02:09 +00:00
indicatePrecisionLoss: true,
2023-04-05 22:06:31 +00:00
);
2023-03-30 16:55:02 +00:00
},
child: Container(
color: Colors.transparent,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.end,
children: [
Text(
2023-05-29 21:10:55 +00:00
ref
.watch(pAmountFormatter(coin))
.format(
2024-01-10 22:53:12 +00:00
ref
.watch(
pTokenBalance(
(
walletId:
widget.walletId,
contractAddress:
tokenContract
.address,
),
),
)
2024-01-10 22:53:12 +00:00
.spendable,
ethContract: tokenContract,
2023-04-05 22:06:31 +00:00
),
2023-03-30 16:55:02 +00:00
style:
STextStyles.titleBold12(context)
.copyWith(
fontSize: 10,
),
textAlign: TextAlign.right,
),
Text(
"${(ref.watch(
pTokenBalance(
(
2024-01-10 22:53:12 +00:00
walletId:
widget.walletId,
contractAddress:
tokenContract
.address,
),
),
).spendable.decimal * ref.watch(priceAnd24hChangeNotifierProvider.select((value) => value.getTokenPrice(tokenContract.address).item1))).toAmount(
2023-04-05 22:06:31 +00:00
fractionDigits: 2,
2023-05-29 21:10:55 +00:00
).fiatString(
2023-04-05 22:06:31 +00:00
locale: locale,
)} ${ref.watch(prefsChangeNotifierProvider.select((value) => value.currency))}",
2023-03-30 16:55:02 +00:00
style: STextStyles.subtitle(context)
.copyWith(
fontSize: 8,
),
textAlign: TextAlign.right,
),
2023-03-30 16:55:02 +00:00
],
),
),
),
],
),
),
),
const SizedBox(
height: 16,
),
Text(
"Send to",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
const SizedBox(
height: 8,
),
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
key: const Key("tokenSendViewAddressFieldKey"),
controller: sendToController,
readOnly: false,
autocorrect: false,
enableSuggestions: false,
toolbarOptions: const ToolbarOptions(
copy: false,
cut: false,
paste: true,
selectAll: false,
),
onChanged: (newValue) {
_address = newValue.trim();
_updatePreviewButtonState(
_address,
_amountToSend,
);
2023-03-30 16:55:02 +00:00
setState(() {
_addressToggleFlag = newValue.isNotEmpty;
});
},
focusNode: _addressFocusNode,
style: STextStyles.field(context),
decoration: standardInputDecoration(
"Enter ${tokenContract.symbol} 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(
key: const Key(
"tokenSendViewClearAddressFieldButtonKey",
),
2023-03-30 16:55:02 +00:00
onTap: () {
sendToController.text = "";
_address = "";
_updatePreviewButtonState(
_address,
_amountToSend,
);
2023-03-30 16:55:02 +00:00
setState(() {
_addressToggleFlag = false;
});
},
child: const XIcon(),
)
: TextFieldIconButton(
key: const Key(
"tokenSendViewPasteAddressFieldButtonKey",
),
2023-03-30 16:55:02 +00:00
onTap:
_onTokenSendViewPasteAddressFieldButtonPressed,
child: sendToController
.text.isEmpty
? const ClipboardIcon()
: const XIcon(),
),
if (sendToController.text.isEmpty)
TextFieldIconButton(
key: const Key(
"sendViewAddressBookButtonKey",
),
2023-03-30 16:55:02 +00:00
onTap: () {
Navigator.of(context).pushNamed(
AddressBookView.routeName,
arguments: widget.coin,
);
},
child: const AddressBookIcon(),
),
if (sendToController.text.isEmpty)
TextFieldIconButton(
key: const Key(
"sendViewScanQrButtonKey",
),
2023-03-30 16:55:02 +00:00
onTap:
_onTokenSendViewScanQrButtonPressed,
child: const QrCodeIcon(),
),
2023-03-30 16:55:02 +00:00
],
),
),
),
),
),
),
Builder(
builder: (_) {
final error = _updateInvalidAddressText(
_address ?? "",
);
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,
),
),
),
);
}
},
),
const SizedBox(
height: 12,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Amount",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
2023-03-31 21:42:28 +00:00
// CustomTextButton(
// text: "Send all ${tokenContract.symbol}",
// onTap: () async {
// cryptoAmountController.text = ref
// .read(tokenServiceProvider)!
// .balance
// .getSpendable()
// .toStringAsFixed(tokenContract.decimals);
//
// _cryptoAmountChanged();
// },
// ),
2023-03-30 16:55:02 +00:00
],
),
const SizedBox(
height: 8,
),
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("amountInputFieldCryptoTextFieldKey"),
controller: cryptoAmountController,
focusNode: _cryptoFocus,
keyboardType: Util.isDesktop
? null
: const TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
textAlign: TextAlign.right,
inputFormatters: [
AmountInputFormatter(
decimals: tokenContract.decimals,
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})$')
// .hasMatch(newValue.text)
// ? newValue
// : oldValue),
2023-03-30 16:55:02 +00:00
],
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(pAmountUnit(coin))
.unitForContract(tokenContract),
2023-03-30 16:55:02 +00:00
style: STextStyles.smallMed14(context)
.copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark,
),
2023-03-30 16:55:02 +00:00
),
),
),
),
),
if (Prefs.instance.externalCalls)
const SizedBox(
height: 8,
),
if (Prefs.instance.externalCalls)
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,
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})$')
// .hasMatch(newValue.text)
// ? newValue
// : oldValue),
2023-03-30 16:55:02 +00:00
],
onChanged: _onFiatAmountFieldChanged,
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),
),
2023-03-30 16:55:02 +00:00
style: STextStyles.smallMed14(context)
.copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark,
),
2023-03-30 16:55:02 +00:00
),
),
),
),
),
const SizedBox(
height: 12,
),
Text(
"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,
),
),
),
const SizedBox(
height: 12,
),
2024-05-15 21:20:45 +00:00
if (coin is! Epiccash)
2023-03-30 16:55:02 +00:00
Text(
"Transaction fee (estimated)",
style: STextStyles.smallMed12(context),
textAlign: TextAlign.left,
),
const SizedBox(
height: 8,
),
Stack(
children: [
TextField(
autocorrect: Util.isDesktop ? false : true,
enableSuggestions:
Util.isDesktop ? false : true,
controller: feeController,
readOnly: true,
textInputAction: TextInputAction.none,
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12,
),
child: RawMaterialButton(
splashColor: Theme.of(context)
.extension<StackColors>()!
.highlight,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
onPressed: () {
showModalBottomSheet<dynamic>(
backgroundColor: Colors.transparent,
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
builder: (_) =>
TransactionFeeSelectionSheet(
walletId: walletId,
2023-03-30 21:15:43 +00:00
isToken: true,
2023-04-05 22:06:31 +00:00
amount: (Decimal.tryParse(
cryptoAmountController.text,
) ??
2023-04-05 22:06:31 +00:00
Decimal.zero)
.toAmount(
fractionDigits:
tokenContract.decimals,
),
2023-03-30 16:55:02 +00:00
updateChosen: (String fee) {
setState(() {
_calculateFeesFuture =
Future(() => fee);
});
},
),
);
},
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
ref
.watch(
feeRateTypeStateProvider
.state,
)
2023-03-30 16:55:02 +00:00
.state
.prettyName,
style: STextStyles.itemSubtitle12(
context,
),
2023-03-30 16:55:02 +00:00
),
const SizedBox(
width: 10,
),
FutureBuilder(
future: _calculateFeesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.done &&
snapshot.hasData) {
return Text(
"~${snapshot.data!}",
2023-03-30 16:55:02 +00:00
style:
STextStyles.itemSubtitle(
context,
),
2023-03-30 16:55:02 +00:00
);
} else {
return AnimatedText(
stringsToLoopThrough: const [
"Calculating",
"Calculating.",
"Calculating..",
"Calculating...",
],
style:
STextStyles.itemSubtitle(
context,
),
2023-03-30 16:55:02 +00:00
);
}
},
),
],
),
SvgPicture.asset(
Assets.svg.chevronDown,
width: 8,
height: 4,
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle2,
),
],
),
),
),
2023-03-30 16:55:02 +00:00
],
),
const Spacer(),
const SizedBox(
height: 12,
),
TextButton(
onPressed: ref
.watch(
previewTokenTxButtonStateProvider.state,
)
2023-03-30 16:55:02 +00:00
.state
? _previewTransaction
: null,
style: ref
.watch(
previewTokenTxButtonStateProvider.state,
)
2023-03-30 16:55:02 +00:00
.state
? Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonStyle(context)
: Theme.of(context)
.extension<StackColors>()!
.getPrimaryDisabledButtonStyle(context),
child: Text(
"Preview",
style: STextStyles.button(context),
),
),
const SizedBox(
height: 4,
),
],
),
),
),
),
),
);
},
),
),
);
}
}