stack_wallet/lib/pages/token_view/sub_widgets/token_summary.dart

389 lines
12 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:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-02-27 16:37:41 +00:00
import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart';
import 'package:stackwallet/pages/buy_view/buy_in_wallet_view.dart';
import 'package:stackwallet/pages/exchange_view/wallet_initiated_exchange_view.dart';
import 'package:stackwallet/pages/receive_view/receive_view.dart';
2023-03-30 16:55:02 +00:00
import 'package:stackwallet/pages/send_view/token_send_view.dart';
2023-03-29 21:29:27 +00:00
import 'package:stackwallet/pages/wallet_view/sub_widgets/wallet_refresh_button.dart';
2023-03-30 18:58:15 +00:00
import 'package:stackwallet/providers/global/locale_provider.dart';
import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/providers/global/price_provider.dart';
2023-03-29 21:29:27 +00:00
import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/themes/theme_providers.dart';
2023-04-06 21:24:56 +00:00
import 'package:stackwallet/utilities/amount/amount.dart';
2023-05-29 21:10:55 +00:00
import 'package:stackwallet/utilities/amount/amount_formatter.dart';
2023-02-27 16:37:41 +00:00
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/text_styles.dart';
2024-05-15 21:20:45 +00:00
import 'package:stackwallet/wallets/crypto_currency/coins/ethereum.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
2024-01-10 22:53:12 +00:00
import 'package:stackwallet/wallets/isar/providers/eth/current_token_wallet_provider.dart';
import 'package:stackwallet/wallets/isar/providers/eth/token_balance_provider.dart';
import 'package:stackwallet/wallets/isar/providers/wallet_info_provider.dart';
2023-04-10 17:28:36 +00:00
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/rounded_container.dart';
import 'package:tuple/tuple.dart';
class TokenSummary extends ConsumerWidget {
const TokenSummary({
2024-05-15 21:20:45 +00:00
super.key,
required this.walletId,
2023-03-29 21:29:27 +00:00
required this.initialSyncStatus,
2024-05-15 21:20:45 +00:00
});
final String walletId;
2023-03-29 21:29:27 +00:00
final WalletSyncStatus initialSyncStatus;
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-02-27 16:37:41 +00:00
final token =
2024-01-10 22:53:12 +00:00
ref.watch(pCurrentTokenWallet.select((value) => value!.tokenContract));
final balance = ref.watch(
2024-05-15 21:20:45 +00:00
pTokenBalance((walletId: walletId, contractAddress: token.address)),
);
2023-02-27 16:37:41 +00:00
2023-03-29 21:29:27 +00:00
return Stack(
children: [
RoundedContainer(
2023-04-10 17:28:36 +00:00
color: Theme.of(context).extension<StackColors>()!.tokenSummaryBG,
2023-03-29 21:29:27 +00:00
padding: const EdgeInsets.all(24),
child: Column(
2023-02-27 16:37:41 +00:00
children: [
2023-03-29 21:29:27 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SvgPicture.asset(
Assets.svg.walletDesktop,
2023-04-10 17:28:36 +00:00
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryTextSecondary,
2023-03-29 21:29:27 +00:00
width: 12,
height: 12,
),
const SizedBox(
width: 6,
),
Text(
ref.watch(
pWalletName(walletId),
2023-03-29 21:29:27 +00:00
),
style: STextStyles.w500_12(context).copyWith(
2023-04-10 17:28:36 +00:00
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryTextSecondary,
2023-03-29 21:29:27 +00:00
),
),
],
),
2023-02-27 16:37:41 +00:00
const SizedBox(
2023-03-29 21:29:27 +00:00
height: 6,
2023-02-27 16:37:41 +00:00
),
2023-03-29 21:29:27 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
2024-05-15 21:20:45 +00:00
ref
.watch(
pAmountFormatter(
Ethereum(CryptoCurrencyNetwork.main),
),
)
.format(
2023-05-29 21:10:55 +00:00
balance.total,
ethContract: token,
2023-04-05 22:06:31 +00:00
),
2023-04-10 17:28:36 +00:00
style: STextStyles.pageTitleH1(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryTextPrimary,
),
2023-02-27 16:37:41 +00:00
),
2023-03-29 21:29:27 +00:00
const SizedBox(
width: 10,
),
CoinTickerTag(
walletId: walletId,
),
],
),
const SizedBox(
height: 6,
2023-02-27 16:37:41 +00:00
),
2023-03-23 22:28:56 +00:00
Text(
2023-04-05 22:06:31 +00:00
"${(balance.total.decimal * ref.watch(
priceAnd24hChangeNotifierProvider.select(
(value) => value.getTokenPrice(token.address).item1,
),
)).toAmount(
fractionDigits: 2,
2023-05-29 21:10:55 +00:00
).fiatString(
2023-04-05 22:06:31 +00:00
locale: ref.watch(
localeServiceChangeNotifierProvider.select(
(value) => value.locale,
),
),
)} ${ref.watch(
prefsChangeNotifierProvider.select(
(value) => value.currency,
),
)}",
2023-04-10 17:28:36 +00:00
style: STextStyles.subtitle500(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryTextPrimary,
),
2023-03-23 22:28:56 +00:00
),
const SizedBox(
2023-03-29 21:29:27 +00:00
height: 20,
2023-03-23 22:28:56 +00:00
),
2023-03-29 21:29:27 +00:00
TokenWalletOptions(
walletId: walletId,
2023-03-29 21:29:27 +00:00
tokenContract: token,
2023-03-23 22:28:56 +00:00
),
],
2023-02-27 16:37:41 +00:00
),
2023-03-29 21:29:27 +00:00
),
Positioned(
top: 10,
right: 10,
child: WalletRefreshButton(
walletId: walletId,
2023-03-29 21:29:27 +00:00
initialSyncStatus: initialSyncStatus,
2023-04-10 17:28:36 +00:00
tokenContractAddress: ref.watch(
2024-01-10 22:53:12 +00:00
pCurrentTokenWallet.select(
2023-04-10 17:28:36 +00:00
(value) => value!.tokenContract.address,
),
),
overrideIconColor:
Theme.of(context).extension<StackColors>()!.topNavIconPrimary,
),
2023-03-29 21:29:27 +00:00
),
],
);
}
}
2023-02-27 16:37:41 +00:00
class TokenWalletOptions extends ConsumerWidget {
const TokenWalletOptions({
2024-05-15 21:20:45 +00:00
super.key,
required this.walletId,
required this.tokenContract,
2024-05-15 21:20:45 +00:00
});
final String walletId;
final EthContract tokenContract;
2024-05-15 21:20:45 +00:00
CryptoCurrency get ethereum => Ethereum(CryptoCurrencyNetwork.main);
void _onExchangePressed(BuildContext context) async {
unawaited(
Navigator.of(context).pushNamed(
WalletInitiatedExchangeView.routeName,
arguments: Tuple3(
walletId,
2024-05-15 21:20:45 +00:00
ethereum,
tokenContract,
),
),
);
}
void _onBuyPressed(BuildContext context) {
unawaited(
Navigator.of(context).pushNamed(
BuyInWalletView.routeName,
arguments: Tuple2(
2024-05-15 21:20:45 +00:00
ethereum,
tokenContract,
),
),
);
}
2023-02-27 16:37:41 +00:00
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-02-27 16:37:41 +00:00
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TokenOptionsButton(
onPressed: () {
Navigator.of(context).pushNamed(
ReceiveView.routeName,
arguments: Tuple2(
walletId,
tokenContract,
),
);
},
2023-02-27 16:37:41 +00:00
subLabel: "Receive",
iconAssetPathSVG: Assets.svg.arrowDownLeft,
2023-02-27 16:37:41 +00:00
),
const SizedBox(
width: 16,
),
TokenOptionsButton(
2023-03-30 16:55:02 +00:00
onPressed: () {
Navigator.of(context).pushNamed(
TokenSendView.routeName,
arguments: Tuple3(
walletId,
2024-05-15 21:20:45 +00:00
ethereum,
2023-03-30 16:55:02 +00:00
tokenContract,
),
);
},
2023-03-28 22:33:33 +00:00
subLabel: "Send",
iconAssetPathSVG: Assets.svg.arrowUpRight,
2023-02-27 16:37:41 +00:00
),
const SizedBox(
width: 16,
),
TokenOptionsButton(
onPressed: () => _onExchangePressed(context),
subLabel: "Swap",
iconAssetPathSVG: ref.watch(
themeProvider.select(
(value) => value.assets.exchange,
),
),
2023-02-27 16:37:41 +00:00
),
const SizedBox(
width: 16,
),
TokenOptionsButton(
onPressed: () => _onBuyPressed(context),
2023-02-27 16:37:41 +00:00
subLabel: "Buy",
iconAssetPathSVG: Assets.svg.creditCard,
2023-02-27 16:37:41 +00:00
),
],
);
}
}
class TokenOptionsButton extends StatelessWidget {
const TokenOptionsButton({
2024-05-15 21:20:45 +00:00
super.key,
2023-02-27 16:37:41 +00:00
required this.onPressed,
required this.subLabel,
required this.iconAssetPathSVG,
2024-05-15 21:20:45 +00:00
});
2023-02-27 16:37:41 +00:00
final VoidCallback onPressed;
final String subLabel;
final String iconAssetPathSVG;
2023-02-27 16:37:41 +00:00
@override
Widget build(BuildContext context) {
2023-04-10 17:28:36 +00:00
final iconSize = subLabel == "Send" || subLabel == "Receive" ? 12.0 : 24.0;
2023-02-27 16:37:41 +00:00
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RawMaterialButton(
2023-04-10 17:28:36 +00:00
fillColor:
Theme.of(context).extension<StackColors>()!.tokenSummaryButtonBG,
2023-02-27 16:37:41 +00:00
elevation: 0,
focusElevation: 0,
hoverElevation: 0,
highlightElevation: 0,
constraints: const BoxConstraints(),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
onPressed: onPressed,
child: Padding(
padding: const EdgeInsets.all(10),
2023-04-10 17:28:36 +00:00
child: ConditionalParent(
condition: iconSize < 24,
builder: (child) => RoundedContainer(
padding: const EdgeInsets.all(6),
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryIcon
.withOpacity(0.4),
radiusMultiplier: 10,
child: Center(
child: child,
),
),
child: iconAssetPathSVG.startsWith("assets/")
? SvgPicture.asset(
iconAssetPathSVG,
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryIcon,
width: iconSize,
height: iconSize,
)
: SvgPicture.file(
File(iconAssetPathSVG),
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryIcon,
width: iconSize,
height: iconSize,
),
2023-02-27 16:37:41 +00:00
),
),
),
const SizedBox(
height: 6,
),
Text(
subLabel,
2023-04-10 17:28:36 +00:00
style: STextStyles.w500_12(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.tokenSummaryTextPrimary,
),
2024-05-15 21:20:45 +00:00
),
2023-02-27 16:37:41 +00:00
],
);
}
}
class CoinTickerTag extends ConsumerWidget {
const CoinTickerTag({
Key? key,
required this.walletId,
}) : super(key: key);
final String walletId;
@override
Widget build(BuildContext context, WidgetRef ref) {
return RoundedContainer(
padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 4),
radiusMultiplier: 0.25,
2023-04-10 17:28:36 +00:00
color: Theme.of(context).extension<StackColors>()!.ethTagBG,
child: Text(
ref
.watch(
pWalletCoin(walletId),
)
.ticker,
style: STextStyles.w600_12(context).copyWith(
2023-04-10 17:28:36 +00:00
color: Theme.of(context).extension<StackColors>()!.ethTagText,
),
),
);
}
}