asset_spot_trading_tab.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import '../../../core/l10n/app_localizations.dart';
  5. import '../../../core/theme/app_colors.dart';
  6. import '../../../core/utils/number_format.dart';
  7. import '../../../providers/asset_provider.dart';
  8. import '../../../providers/currency_provider.dart';
  9. import '../../../providers/futures_provider.dart'
  10. show activeBottomTabProvider, lastTradingRouteProvider;
  11. import '../../../providers/spot_coin_cache_provider.dart';
  12. import '../../../providers/spot_provider.dart' show SpotWalletAsset;
  13. import '../../widgets/common/app_refresh_indicator.dart';
  14. import '../../widgets/common/coin_icon.dart';
  15. /// 现货 Tab — 展示现货账户总估值、今日盈亏、持仓币种列表
  16. class AssetSpotTradingTab extends ConsumerWidget {
  17. const AssetSpotTradingTab(
  18. {super.key, required this.state, required this.notifier});
  19. final AssetState state;
  20. final AssetNotifier notifier;
  21. @override
  22. Widget build(BuildContext context, WidgetRef ref) {
  23. ref.watch(currencyProvider); // 法币切换触发重建
  24. final cs = Theme.of(context).colorScheme;
  25. final l10n = AppLocalizations.of(context)!;
  26. final obscure = state.obscureBalance;
  27. final total = state.spotTradingTotal;
  28. final display =
  29. obscure ? '******' : formatPrice(total, decimalPlaces: 2);
  30. final hideZero = state.hideZeroBalanceInSpotTab;
  31. final assets = state.spotWallets;
  32. return AppRefreshIndicator(
  33. onRefresh: notifier.silentRefresh,
  34. child: ListView(
  35. children: [
  36. // 总估值
  37. Padding(
  38. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  39. child: Column(
  40. crossAxisAlignment: CrossAxisAlignment.start,
  41. children: [
  42. Row(
  43. children: [
  44. Text(l10n.totalAssets,
  45. style: TextStyle(
  46. color: cs.onSurface.withAlpha(153), fontSize: 13)),
  47. const SizedBox(width: 6),
  48. GestureDetector(
  49. onTap: notifier.toggleObscure,
  50. child: Icon(
  51. obscure
  52. ? Icons.visibility_off_outlined
  53. : Icons.visibility_outlined,
  54. size: 16,
  55. color: cs.onSurface.withAlpha(153),
  56. ),
  57. ),
  58. ],
  59. ),
  60. const SizedBox(height: 8),
  61. Row(
  62. crossAxisAlignment: CrossAxisAlignment.end,
  63. children: [
  64. Text(display,
  65. style: TextStyle(
  66. color: cs.onSurface,
  67. fontSize: 32,
  68. fontWeight: FontWeight.w700,
  69. letterSpacing: -0.5)),
  70. const SizedBox(width: 6),
  71. Padding(
  72. padding: const EdgeInsets.only(bottom: 5),
  73. child: Text('USDT',
  74. style: TextStyle(
  75. color: cs.onSurface.withAlpha(153),
  76. fontSize: 14)),
  77. ),
  78. ],
  79. ),
  80. ],
  81. ),
  82. ),
  83. // 划转 / 交易
  84. Padding(
  85. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  86. child: Row(
  87. children: [
  88. Expanded(
  89. child: GestureDetector(
  90. onTap: () =>
  91. context.push('/asset/transfer?from=SPOT&to=SPOT_TRADING'),
  92. child: Container(
  93. height: 44,
  94. decoration: BoxDecoration(
  95. color: AppColors.brand,
  96. borderRadius: BorderRadius.circular(22)),
  97. child: Center(
  98. child: Text(l10n.transfer,
  99. style: const TextStyle(
  100. color: Colors.black,
  101. fontSize: 15,
  102. fontWeight: FontWeight.w600))),
  103. ),
  104. ),
  105. ),
  106. const SizedBox(width: 12),
  107. Expanded(
  108. child: GestureDetector(
  109. onTap: () {
  110. const path = '/spot/BTCUSDT';
  111. ref.read(lastTradingRouteProvider.notifier).state = path;
  112. ref.read(activeBottomTabProvider.notifier).state = 2;
  113. context.go(path);
  114. },
  115. child: Container(
  116. height: 44,
  117. decoration: BoxDecoration(
  118. border: Border.all(color: AppColors.brand),
  119. borderRadius: BorderRadius.circular(22)),
  120. child: Center(
  121. child: Text(l10n.goToTrade,
  122. style: TextStyle(
  123. color: AppColors.brand,
  124. fontSize: 15,
  125. fontWeight: FontWeight.w600))),
  126. ),
  127. ),
  128. ),
  129. ],
  130. ),
  131. ),
  132. Padding(
  133. padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
  134. child: Row(
  135. mainAxisAlignment: MainAxisAlignment.end,
  136. children: [
  137. GestureDetector(
  138. onTap: notifier.toggleHideZeroBalanceInSpotTab,
  139. child: Row(
  140. children: [
  141. Icon(
  142. hideZero
  143. ? Icons.check_box_outlined
  144. : Icons.check_box_outline_blank,
  145. size: 18,
  146. color: hideZero
  147. ? AppColors.brand
  148. : cs.onSurface.withAlpha(140),
  149. ),
  150. const SizedBox(width: 6),
  151. Text(
  152. l10n.hideZeroBalanceAssets,
  153. style: TextStyle(
  154. color: cs.onSurface.withAlpha(170),
  155. fontSize: 13,
  156. ),
  157. ),
  158. ],
  159. ),
  160. ),
  161. ],
  162. ),
  163. ),
  164. const SizedBox(height: 8),
  165. // 币种列表
  166. if (assets.isEmpty)
  167. Padding(
  168. padding: const EdgeInsets.symmetric(vertical: 36),
  169. child: Center(
  170. child: Text(l10n.noAssets,
  171. style: TextStyle(
  172. color: cs.onSurface.withAlpha(140), fontSize: 13)),
  173. ),
  174. )
  175. else
  176. for (final asset in assets)
  177. _SpotAssetRow(asset: asset, obscure: obscure),
  178. const SizedBox(height: 32),
  179. ],
  180. ),
  181. );
  182. }
  183. }
  184. class _SpotAssetRow extends ConsumerWidget {
  185. const _SpotAssetRow({required this.asset, required this.obscure});
  186. final SpotWalletAsset asset;
  187. final bool obscure;
  188. @override
  189. Widget build(BuildContext context, WidgetRef ref) {
  190. final cs = Theme.of(context).colorScheme;
  191. final l10n = AppLocalizations.of(context)!;
  192. final mapState = ref.watch(spotCoinCacheProvider);
  193. final coinConfig = lookupSpotCoinConfig(mapState, asset.coin);
  194. final iconUrl = spotCoinIconUrl(mapState, asset.coin);
  195. final decimals = coinConfig?.assetDisplayDecimals ?? 2;
  196. final val = obscure ? '******' : null;
  197. return Container(
  198. padding: const EdgeInsets.fromLTRB(16, 16, 16, 16),
  199. decoration: BoxDecoration(
  200. border: Border(
  201. bottom: BorderSide(color: cs.outline.withAlpha(40), width: 0.6),
  202. ),
  203. ),
  204. child: Column(
  205. crossAxisAlignment: CrossAxisAlignment.start,
  206. children: [
  207. // 币种图标 + 名称
  208. Row(
  209. children: [
  210. CoinIcon(
  211. symbol: asset.coin,
  212. iconUrl: iconUrl,
  213. size: 40,
  214. shape: BoxShape.circle),
  215. const SizedBox(width: 10),
  216. Text(asset.coin,
  217. style: TextStyle(
  218. color: cs.onSurface,
  219. fontSize: 16,
  220. fontWeight: FontWeight.w700)),
  221. ],
  222. ),
  223. const SizedBox(height: 14),
  224. // 三列数据
  225. Row(
  226. children: [
  227. Expanded(
  228. child: _AssetCell(
  229. label: l10n.assetBalance,
  230. value: val ?? formatAmount(asset.total, decimals: decimals),
  231. align: CrossAxisAlignment.start,
  232. ),
  233. ),
  234. Expanded(
  235. child: _AssetCell(
  236. label: l10n.availableLabel,
  237. value: val ?? formatAmount(asset.balance, decimals: decimals),
  238. align: CrossAxisAlignment.center,
  239. ),
  240. ),
  241. Expanded(
  242. child: _AssetCell(
  243. label: l10n.unavailableLabel,
  244. value:
  245. val ?? formatAmount(asset.frozenBalance, decimals: decimals),
  246. align: CrossAxisAlignment.end,
  247. ),
  248. ),
  249. ],
  250. ),
  251. ],
  252. ),
  253. );
  254. }
  255. }
  256. class _AssetCell extends StatelessWidget {
  257. const _AssetCell({
  258. required this.label,
  259. required this.value,
  260. required this.align,
  261. });
  262. final String label;
  263. final String value;
  264. final CrossAxisAlignment align;
  265. @override
  266. Widget build(BuildContext context) {
  267. final cs = Theme.of(context).colorScheme;
  268. final textAlign = align == CrossAxisAlignment.start
  269. ? TextAlign.left
  270. : align == CrossAxisAlignment.end
  271. ? TextAlign.right
  272. : TextAlign.center;
  273. return Column(
  274. crossAxisAlignment: align,
  275. children: [
  276. Text(label,
  277. style:
  278. TextStyle(color: cs.onSurface.withAlpha(140), fontSize: 11),
  279. textAlign: textAlign),
  280. const SizedBox(height: 4),
  281. Text(value,
  282. style: TextStyle(
  283. color: cs.onSurface,
  284. fontSize: 13,
  285. fontWeight: FontWeight.w600,
  286. fontFeatures: const [FontFeature.tabularFigures()],
  287. ),
  288. textAlign: textAlign),
  289. ],
  290. );
  291. }
  292. }