asset_staking_tab.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import 'package:flutter/material.dart';
  2. import 'package:go_router/go_router.dart';
  3. import '../../../core/l10n/app_localizations.dart';
  4. import '../../../core/theme/app_colors.dart';
  5. import '../../../core/utils/number_format.dart';
  6. import '../../../data/models/finance/staking_order_item.dart';
  7. import '../../../providers/asset_provider.dart';
  8. import '../../widgets/common/app_refresh_indicator.dart';
  9. /// 锁仓账户 Tab — 对齐 Web AssetsView STAKING Tab
  10. class AssetStakingTab extends StatelessWidget {
  11. const AssetStakingTab({super.key, required this.state, required this.notifier});
  12. final AssetState state;
  13. final AssetNotifier notifier;
  14. @override
  15. Widget build(BuildContext context) {
  16. final cs = Theme.of(context).colorScheme;
  17. final l10n = AppLocalizations.of(context)!;
  18. final obscure = state.obscureBalance;
  19. final locked = double.tryParse(state.stakingOverviewLocked) ?? 0;
  20. final available = double.tryParse(state.stakingOverviewAvailable) ?? 0;
  21. final totalLocked = locked + available;
  22. final lockedDisplay =
  23. obscure ? '******' : formatPrice(locked, decimalPlaces: 2);
  24. final totalDisplay =
  25. obscure ? '******' : formatPrice(totalLocked, decimalPlaces: 2);
  26. return AppRefreshIndicator(
  27. onRefresh: notifier.refreshStakingTab,
  28. child: ListView(
  29. children: [
  30. Padding(
  31. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  32. child: Container(
  33. padding: const EdgeInsets.all(16),
  34. decoration: BoxDecoration(
  35. color: Theme.of(context).brightness == Brightness.dark
  36. ? AppColors.darkBgSecondary
  37. : AppColors.lightBgSecondary,
  38. borderRadius: BorderRadius.circular(12),
  39. border: Theme.of(context).brightness == Brightness.dark
  40. ? null
  41. : Border.all(color: AppColors.lightBorder, width: 0.5),
  42. ),
  43. child: Column(
  44. crossAxisAlignment: CrossAxisAlignment.start,
  45. children: [
  46. Row(
  47. children: [
  48. Text(
  49. l10n.assetsReleaseTotal,
  50. style: TextStyle(
  51. color: cs.onSurface.withAlpha(153),
  52. fontSize: 13,
  53. ),
  54. ),
  55. const SizedBox(width: 6),
  56. GestureDetector(
  57. onTap: notifier.toggleObscure,
  58. child: Icon(
  59. obscure
  60. ? Icons.visibility_off_outlined
  61. : Icons.visibility_outlined,
  62. size: 16,
  63. color: cs.onSurface.withAlpha(153),
  64. ),
  65. ),
  66. ],
  67. ),
  68. const SizedBox(height: 8),
  69. Row(
  70. crossAxisAlignment: CrossAxisAlignment.end,
  71. children: [
  72. Text(
  73. lockedDisplay,
  74. style: TextStyle(
  75. color: cs.onSurface,
  76. fontSize: 32,
  77. fontWeight: FontWeight.w700,
  78. letterSpacing: -0.5,
  79. ),
  80. ),
  81. const SizedBox(width: 6),
  82. Padding(
  83. padding: const EdgeInsets.only(bottom: 5),
  84. child: Text(
  85. l10n.assetsIbitUnit,
  86. style: TextStyle(
  87. color: cs.onSurface.withAlpha(153),
  88. fontSize: 14,
  89. ),
  90. ),
  91. ),
  92. ],
  93. ),
  94. const SizedBox(height: 8),
  95. Row(
  96. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  97. children: [
  98. Text(
  99. l10n.assetsTotalLockedBalance,
  100. style: TextStyle(
  101. color: cs.onSurface.withAlpha(153),
  102. fontSize: 13,
  103. ),
  104. ),
  105. Text(
  106. totalDisplay,
  107. style: TextStyle(
  108. color: cs.onSurface,
  109. fontSize: 13,
  110. fontWeight: FontWeight.w500,
  111. ),
  112. ),
  113. ],
  114. ),
  115. const SizedBox(height: 16),
  116. Row(
  117. children: [
  118. Expanded(
  119. child: _StakingActionButton(
  120. label: l10n.depositCoin,
  121. primary: true,
  122. onTap: () => context.push('/asset/deposit'),
  123. ),
  124. ),
  125. const SizedBox(width: 8),
  126. Expanded(
  127. child: _StakingActionButton(
  128. label: l10n.withdrawCoin,
  129. onTap: () => context.push('/asset/withdraw'),
  130. ),
  131. ),
  132. const SizedBox(width: 8),
  133. Expanded(
  134. child: _StakingActionButton(
  135. label: l10n.transfer,
  136. onTap: () =>
  137. context.push('/asset/transfer?from=SPOT&to=SWAP'),
  138. ),
  139. ),
  140. ],
  141. ),
  142. ],
  143. ),
  144. ),
  145. ),
  146. Padding(
  147. padding: const EdgeInsets.fromLTRB(16, 20, 16, 8),
  148. child: Row(
  149. children: [
  150. Expanded(
  151. child: Text(
  152. l10n.assetsMyStakingOrders,
  153. style: TextStyle(
  154. color: cs.onSurface,
  155. fontSize: 15,
  156. fontWeight: FontWeight.w600,
  157. ),
  158. ),
  159. ),
  160. _StakingStatusFilter(
  161. value: state.stakingOrderStatusFilter,
  162. loading: state.stakingOrdersLoading,
  163. onChanged: notifier.setStakingOrderStatusFilter,
  164. ),
  165. ],
  166. ),
  167. ),
  168. if (state.stakingOrdersLoading && state.stakingOrders.isEmpty)
  169. Padding(
  170. padding: const EdgeInsets.symmetric(vertical: 32),
  171. child: Center(
  172. child: SizedBox(
  173. width: 24,
  174. height: 24,
  175. child: CircularProgressIndicator(
  176. strokeWidth: 2,
  177. color: cs.onSurface.withAlpha(120),
  178. ),
  179. ),
  180. ),
  181. )
  182. else if (state.stakingOrders.isEmpty)
  183. Padding(
  184. padding: const EdgeInsets.symmetric(vertical: 32),
  185. child: Center(
  186. child: Text(
  187. l10n.noData,
  188. style: TextStyle(
  189. color: cs.onSurface.withAlpha(120),
  190. fontSize: 14,
  191. ),
  192. ),
  193. ),
  194. )
  195. else
  196. ...state.stakingOrders.map(
  197. (order) => _StakingOrderCard(order: order, obscure: obscure),
  198. ),
  199. const SizedBox(height: 32),
  200. ],
  201. ),
  202. );
  203. }
  204. }
  205. class _StakingActionButton extends StatelessWidget {
  206. const _StakingActionButton({
  207. required this.label,
  208. required this.onTap,
  209. this.primary = false,
  210. });
  211. final String label;
  212. final VoidCallback onTap;
  213. final bool primary;
  214. @override
  215. Widget build(BuildContext context) {
  216. final cs = Theme.of(context).colorScheme;
  217. return GestureDetector(
  218. onTap: onTap,
  219. child: Container(
  220. height: 40,
  221. alignment: Alignment.center,
  222. decoration: BoxDecoration(
  223. color: primary ? AppColors.brand : cs.onSurface.withAlpha(20),
  224. borderRadius: BorderRadius.circular(20),
  225. ),
  226. child: Text(
  227. label,
  228. style: TextStyle(
  229. color: primary ? Colors.white : cs.onSurface,
  230. fontSize: 13,
  231. fontWeight: FontWeight.w600,
  232. ),
  233. ),
  234. ),
  235. );
  236. }
  237. }
  238. class _StakingStatusFilter extends StatelessWidget {
  239. const _StakingStatusFilter({
  240. required this.value,
  241. required this.loading,
  242. required this.onChanged,
  243. });
  244. final StakingOrderStatusFilter value;
  245. final bool loading;
  246. final ValueChanged<StakingOrderStatusFilter> onChanged;
  247. @override
  248. Widget build(BuildContext context) {
  249. final l10n = AppLocalizations.of(context)!;
  250. final cs = Theme.of(context).colorScheme;
  251. return Container(
  252. padding: const EdgeInsets.symmetric(horizontal: 10),
  253. decoration: BoxDecoration(
  254. border: Border.all(color: cs.outline.withAlpha(80)),
  255. borderRadius: BorderRadius.circular(8),
  256. ),
  257. child: DropdownButtonHideUnderline(
  258. child: DropdownButton<StakingOrderStatusFilter>(
  259. value: value,
  260. isDense: true,
  261. icon: Icon(Icons.expand_more, size: 18, color: cs.onSurface.withAlpha(153)),
  262. items: [
  263. DropdownMenuItem(
  264. value: StakingOrderStatusFilter.all,
  265. child: Text(l10n.all, style: const TextStyle(fontSize: 13)),
  266. ),
  267. DropdownMenuItem(
  268. value: StakingOrderStatusFilter.subscribing,
  269. child: Text(l10n.assetsSubscribing, style: const TextStyle(fontSize: 13)),
  270. ),
  271. DropdownMenuItem(
  272. value: StakingOrderStatusFilter.releasing,
  273. child: Text(l10n.assetsReleasing, style: const TextStyle(fontSize: 13)),
  274. ),
  275. DropdownMenuItem(
  276. value: StakingOrderStatusFilter.completed,
  277. child: Text(l10n.completed, style: const TextStyle(fontSize: 13)),
  278. ),
  279. ],
  280. onChanged: loading
  281. ? null
  282. : (next) {
  283. if (next != null) {
  284. onChanged(next);
  285. }
  286. },
  287. ),
  288. ),
  289. );
  290. }
  291. }
  292. class _StakingOrderCard extends StatelessWidget {
  293. const _StakingOrderCard({required this.order, required this.obscure});
  294. final StakingOrderItem order;
  295. final bool obscure;
  296. String _fmtAmount(double value) {
  297. if (obscure) {
  298. return '******';
  299. }
  300. return formatPrice(value, decimalPlaces: 2);
  301. }
  302. String _statusLabel(AppLocalizations l10n) {
  303. if (order.status == 0) {
  304. return l10n.assetsSubscribing;
  305. }
  306. if (order.status == 2) {
  307. return l10n.completed;
  308. }
  309. return l10n.assetsReleasing;
  310. }
  311. Color _statusColor() {
  312. if (order.status == 0) {
  313. return AppColors.brand;
  314. }
  315. if (order.status == 2) {
  316. return Colors.grey;
  317. }
  318. return Colors.orange;
  319. }
  320. @override
  321. Widget build(BuildContext context) {
  322. final cs = Theme.of(context).colorScheme;
  323. final l10n = AppLocalizations.of(context)!;
  324. final isDark = Theme.of(context).brightness == Brightness.dark;
  325. final coin = order.coinUnit.isNotEmpty ? order.coinUnit : l10n.assetsIbitUnit.toUpperCase();
  326. final amount = double.tryParse(order.amount) ?? 0;
  327. final released = double.tryParse(order.releasedAmount) ?? 0;
  328. return Container(
  329. margin: const EdgeInsets.fromLTRB(16, 0, 16, 10),
  330. padding: const EdgeInsets.all(16),
  331. decoration: BoxDecoration(
  332. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  333. borderRadius: BorderRadius.circular(12),
  334. border: isDark ? null : Border.all(color: AppColors.lightBorder, width: 0.5),
  335. ),
  336. child: Column(
  337. crossAxisAlignment: CrossAxisAlignment.start,
  338. children: [
  339. Text(
  340. '${l10n.assetsStakingOrderNo}: ORD${order.id}',
  341. style: TextStyle(
  342. color: cs.onSurface.withAlpha(153),
  343. fontSize: 12,
  344. ),
  345. ),
  346. const SizedBox(height: 12),
  347. _StakingOrderRow(
  348. label: l10n.assetsStakingTotal,
  349. value: '${_fmtAmount(amount)} $coin',
  350. ),
  351. _StakingOrderRow(
  352. label: l10n.assetsReleased,
  353. value: '${_fmtAmount(released)} $coin',
  354. ),
  355. _StakingOrderRow(
  356. label: l10n.assetsPendingRelease,
  357. value: '${_fmtAmount(order.pendingAmount)} $coin',
  358. ),
  359. _StakingOrderRow(
  360. label: l10n.startTime,
  361. value: order.displayStartTime,
  362. ),
  363. _StakingOrderRow(
  364. label: l10n.statusLabel,
  365. value: _statusLabel(l10n),
  366. valueColor: _statusColor(),
  367. ),
  368. ],
  369. ),
  370. );
  371. }
  372. }
  373. class _StakingOrderRow extends StatelessWidget {
  374. const _StakingOrderRow({
  375. required this.label,
  376. required this.value,
  377. this.valueColor,
  378. });
  379. final String label;
  380. final String value;
  381. final Color? valueColor;
  382. @override
  383. Widget build(BuildContext context) {
  384. final cs = Theme.of(context).colorScheme;
  385. return Padding(
  386. padding: const EdgeInsets.only(bottom: 8),
  387. child: Row(
  388. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  389. children: [
  390. Text(
  391. label,
  392. style: TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 13),
  393. ),
  394. Flexible(
  395. child: Text(
  396. value,
  397. textAlign: TextAlign.right,
  398. style: TextStyle(
  399. color: valueColor ?? cs.onSurface,
  400. fontSize: 13,
  401. fontWeight: FontWeight.w500,
  402. ),
  403. ),
  404. ),
  405. ],
  406. ),
  407. );
  408. }
  409. }