asset_staking_tab.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import 'package:flutter/material.dart';
  2. import '../../../core/l10n/app_localizations.dart';
  3. import '../../../core/theme/app_colors.dart';
  4. import '../../../core/utils/number_format.dart';
  5. import '../../../data/models/finance/staking_order_item.dart';
  6. import '../../../providers/asset_provider.dart';
  7. import '../../widgets/common/app_refresh_indicator.dart';
  8. /// 锁仓账户 Tab — 对齐 Web AssetsView STAKING Tab
  9. class AssetStakingTab extends StatelessWidget {
  10. const AssetStakingTab(
  11. {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. ],
  116. ),
  117. ),
  118. ),
  119. Padding(
  120. padding: const EdgeInsets.fromLTRB(16, 20, 16, 8),
  121. child: Row(
  122. children: [
  123. Expanded(
  124. child: Text(
  125. l10n.assetsMyStakingOrders,
  126. style: TextStyle(
  127. color: cs.onSurface,
  128. fontSize: 15,
  129. fontWeight: FontWeight.w600,
  130. ),
  131. ),
  132. ),
  133. _StakingStatusFilter(
  134. value: state.stakingOrderStatusFilter,
  135. loading: state.stakingOrdersLoading,
  136. onChanged: notifier.setStakingOrderStatusFilter,
  137. ),
  138. ],
  139. ),
  140. ),
  141. if (state.stakingOrdersLoading && state.stakingOrders.isEmpty)
  142. Padding(
  143. padding: const EdgeInsets.symmetric(vertical: 32),
  144. child: Center(
  145. child: SizedBox(
  146. width: 24,
  147. height: 24,
  148. child: CircularProgressIndicator(
  149. strokeWidth: 2,
  150. color: cs.onSurface.withAlpha(120),
  151. ),
  152. ),
  153. ),
  154. )
  155. else if (state.stakingOrders.isEmpty)
  156. Padding(
  157. padding: const EdgeInsets.symmetric(vertical: 32),
  158. child: Center(
  159. child: Text(
  160. l10n.noData,
  161. style: TextStyle(
  162. color: cs.onSurface.withAlpha(120),
  163. fontSize: 14,
  164. ),
  165. ),
  166. ),
  167. )
  168. else
  169. ...state.stakingOrders.map(
  170. (order) => _StakingOrderCard(order: order, obscure: obscure),
  171. ),
  172. const SizedBox(height: 32),
  173. ],
  174. ),
  175. );
  176. }
  177. }
  178. class _StakingStatusFilter extends StatelessWidget {
  179. const _StakingStatusFilter({
  180. required this.value,
  181. required this.loading,
  182. required this.onChanged,
  183. });
  184. final StakingOrderStatusFilter value;
  185. final bool loading;
  186. final ValueChanged<StakingOrderStatusFilter> onChanged;
  187. @override
  188. Widget build(BuildContext context) {
  189. final l10n = AppLocalizations.of(context)!;
  190. final cs = Theme.of(context).colorScheme;
  191. return Container(
  192. padding: const EdgeInsets.symmetric(horizontal: 10),
  193. decoration: BoxDecoration(
  194. border: Border.all(color: cs.outline.withAlpha(80)),
  195. borderRadius: BorderRadius.circular(8),
  196. ),
  197. child: DropdownButtonHideUnderline(
  198. child: DropdownButton<StakingOrderStatusFilter>(
  199. value: value,
  200. isDense: true,
  201. icon: Icon(Icons.expand_more,
  202. size: 18, color: cs.onSurface.withAlpha(153)),
  203. items: [
  204. DropdownMenuItem(
  205. value: StakingOrderStatusFilter.all,
  206. child: Text(l10n.all, style: const TextStyle(fontSize: 13)),
  207. ),
  208. DropdownMenuItem(
  209. value: StakingOrderStatusFilter.subscribing,
  210. child: Text(l10n.assetsSubscribing,
  211. style: const TextStyle(fontSize: 13)),
  212. ),
  213. DropdownMenuItem(
  214. value: StakingOrderStatusFilter.releasing,
  215. child: Text(l10n.assetsReleasing,
  216. style: const TextStyle(fontSize: 13)),
  217. ),
  218. DropdownMenuItem(
  219. value: StakingOrderStatusFilter.completed,
  220. child: Text(l10n.completed, style: const TextStyle(fontSize: 13)),
  221. ),
  222. ],
  223. onChanged: loading
  224. ? null
  225. : (next) {
  226. if (next != null) {
  227. onChanged(next);
  228. }
  229. },
  230. ),
  231. ),
  232. );
  233. }
  234. }
  235. class _StakingOrderCard extends StatelessWidget {
  236. const _StakingOrderCard({required this.order, required this.obscure});
  237. final StakingOrderItem order;
  238. final bool obscure;
  239. String _fmtAmount(double value, {int decimals = 2}) {
  240. if (obscure) {
  241. return '******';
  242. }
  243. return formatPrice(value, decimalPlaces: decimals);
  244. }
  245. String _fmtReleasedCompact(double value) {
  246. if (obscure) {
  247. return '******';
  248. }
  249. if (value == value.truncateToDouble()) {
  250. return value.toInt().toString();
  251. }
  252. return value
  253. .toStringAsFixed(8)
  254. .replaceAll(RegExp(r'0+$'), '')
  255. .replaceAll(RegExp(r'\.$'), '');
  256. }
  257. String _statusLabel(AppLocalizations l10n) {
  258. if (order.status == 0) {
  259. return l10n.assetsSubscribing;
  260. }
  261. if (order.status == 2) {
  262. return l10n.completed;
  263. }
  264. return l10n.assetsReleasing;
  265. }
  266. Color _statusTextColor() {
  267. if (order.status == 0) {
  268. return AppColors.brand;
  269. }
  270. if (order.status == 2) {
  271. return const Color(0xFF12161C);
  272. }
  273. return const Color(0xFF12161C);
  274. }
  275. Color _statusBackgroundColor() {
  276. if (order.status == 0) {
  277. return const Color(0xFF12161C);
  278. }
  279. if (order.status == 2) {
  280. return AppColors.brand;
  281. }
  282. return AppColors.brand;
  283. }
  284. String _displayDate(String? raw) {
  285. if (raw == null || raw.isEmpty) {
  286. return '-';
  287. }
  288. final normalized = raw.replaceFirst('T', ' ');
  289. return normalized.length >= 10 ? normalized.substring(0, 10) : normalized;
  290. }
  291. String _lockDaysText() {
  292. final startRaw = order.stakingTime ?? order.createTime;
  293. final endRaw = order.unlockTime;
  294. if (startRaw == null ||
  295. endRaw == null ||
  296. startRaw.isEmpty ||
  297. endRaw.isEmpty) {
  298. return '--';
  299. }
  300. final start = DateTime.tryParse(startRaw);
  301. final end = DateTime.tryParse(endRaw);
  302. if (start == null || end == null) {
  303. return '--';
  304. }
  305. final days = end.difference(start).inDays.abs();
  306. return days > 0 ? '$days天' : '--';
  307. }
  308. double _releaseProgress(double amount, double released) {
  309. if (!amount.isFinite || amount <= 0) {
  310. return 0;
  311. }
  312. final ratio = released / amount;
  313. if (!ratio.isFinite) {
  314. return 0;
  315. }
  316. return ratio.clamp(0.0, 1.0);
  317. }
  318. Color _progressColor() {
  319. if (order.status == 2) {
  320. return const Color(0xFF62656C);
  321. }
  322. if (order.status == 1) {
  323. return AppColors.brand;
  324. }
  325. return Colors.transparent;
  326. }
  327. @override
  328. Widget build(BuildContext context) {
  329. final cs = Theme.of(context).colorScheme;
  330. final l10n = AppLocalizations.of(context)!;
  331. final isDark = Theme.of(context).brightness == Brightness.dark;
  332. final coin = order.coinUnit.isNotEmpty
  333. ? order.coinUnit
  334. : l10n.assetsIbitUnit.toUpperCase();
  335. final amount = double.tryParse(order.amount) ?? 0;
  336. final released = double.tryParse(order.releasedAmount) ?? 0;
  337. final progress = _releaseProgress(amount, released);
  338. return Container(
  339. margin: const EdgeInsets.fromLTRB(16, 0, 16, 10),
  340. padding: const EdgeInsets.fromLTRB(14, 14, 14, 12),
  341. decoration: BoxDecoration(
  342. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  343. borderRadius: BorderRadius.circular(16),
  344. border: isDark
  345. ? null
  346. : Border.all(color: AppColors.lightBorder, width: 0.5),
  347. ),
  348. child: Column(
  349. crossAxisAlignment: CrossAxisAlignment.start,
  350. children: [
  351. Row(
  352. children: [
  353. Expanded(
  354. child: Text(
  355. '${l10n.assetsStakingOrderNo}: ORD${order.id}',
  356. style: TextStyle(
  357. color: cs.onSurface,
  358. fontSize: 13,
  359. fontWeight: FontWeight.w600,
  360. ),
  361. ),
  362. ),
  363. Container(
  364. padding:
  365. const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
  366. decoration: BoxDecoration(
  367. color: _statusBackgroundColor(),
  368. borderRadius: BorderRadius.circular(999),
  369. ),
  370. child: Row(
  371. mainAxisSize: MainAxisSize.min,
  372. children: [
  373. Text(
  374. _statusLabel(l10n),
  375. style: TextStyle(
  376. color: _statusTextColor(),
  377. fontSize: 12,
  378. fontWeight: FontWeight.w600,
  379. height: 1,
  380. ),
  381. ),
  382. if (order.status != 0) ...[
  383. const SizedBox(width: 2),
  384. Icon(
  385. Icons.chevron_right,
  386. size: 14,
  387. color: _statusTextColor(),
  388. ),
  389. ],
  390. ],
  391. ),
  392. ),
  393. ],
  394. ),
  395. const SizedBox(height: 14),
  396. _StakingOrderRow(
  397. label: l10n.orderTime,
  398. value: _displayDate(order.stakingTime ?? order.createTime),
  399. ),
  400. _StakingOrderRow(
  401. label: l10n.assetsStakingTotal,
  402. value: '${_fmtAmount(amount, decimals: 0)} $coin',
  403. ),
  404. _StakingOrderRow(
  405. label: l10n.stakingLockDaysLabel,
  406. value: _lockDaysText(),
  407. ),
  408. _StakingOrderRow(
  409. label: l10n.assetsReleased,
  410. value:
  411. '${_fmtReleasedCompact(released)}/${_fmtReleasedCompact(amount)}',
  412. ),
  413. const SizedBox(height: 8),
  414. ClipRRect(
  415. borderRadius: BorderRadius.circular(999),
  416. child: SizedBox(
  417. height: 8,
  418. child: Stack(
  419. fit: StackFit.expand,
  420. children: [
  421. Container(color: const Color(0xFF12161C)),
  422. FractionallySizedBox(
  423. alignment: Alignment.centerLeft,
  424. widthFactor: progress,
  425. child: Container(color: _progressColor()),
  426. ),
  427. ],
  428. ),
  429. ),
  430. ),
  431. ],
  432. ),
  433. );
  434. }
  435. }
  436. class _StakingOrderRow extends StatelessWidget {
  437. const _StakingOrderRow({
  438. required this.label,
  439. required this.value,
  440. });
  441. final String label;
  442. final String value;
  443. @override
  444. Widget build(BuildContext context) {
  445. final cs = Theme.of(context).colorScheme;
  446. return Padding(
  447. padding: const EdgeInsets.only(bottom: 10),
  448. child: Row(
  449. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  450. children: [
  451. Text(
  452. label,
  453. style: TextStyle(color: cs.onSurface.withAlpha(140), fontSize: 13),
  454. ),
  455. Expanded(
  456. child: Text(
  457. value,
  458. textAlign: TextAlign.right,
  459. style: TextStyle(
  460. color: cs.onSurface,
  461. fontSize: 13,
  462. fontWeight: FontWeight.w500,
  463. ),
  464. ),
  465. ),
  466. ],
  467. ),
  468. );
  469. }
  470. }