home_screen.dart 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. import 'dart:async';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
  6. import 'package:go_router/go_router.dart';
  7. import 'package:shimmer/shimmer.dart';
  8. import 'package:url_launcher/url_launcher.dart';
  9. import '../../../core/config/app_config.dart';
  10. import '../../../core/l10n/app_localizations.dart';
  11. import '../../../core/theme/app_colors.dart';
  12. import '../../../core/navigation/broker_navigation.dart';
  13. import '../../../core/utils/number_format.dart';
  14. import '../../../core/utils/symbol_display.dart';
  15. import '../../../providers/currency_provider.dart';
  16. import '../../../data/models/announcement/announcement.dart';
  17. import '../../../data/models/home/app_header_item.dart';
  18. import '../../../data/models/home/market_ticker.dart';
  19. import '../../../providers/announcement_popup_provider.dart';
  20. import '../../../providers/announcement_unread_provider.dart';
  21. import '../../../core/network/dio_client.dart' show versionOutdatedProvider;
  22. import '../../../providers/app_version_provider.dart';
  23. import '../../../providers/customer_service_provider.dart';
  24. import '../../../providers/asset_provider.dart';
  25. import '../../../providers/home_provider.dart';
  26. import '../../../providers/market_provider.dart'
  27. show marketProvider, spotTickerProvider, MarketMode;
  28. import '../../widgets/common/app_refresh_indicator.dart';
  29. import '../../widgets/common/coin_icon.dart';
  30. import '../../widgets/common/update_dialog.dart';
  31. import '../../widgets/market_list_row_layout.dart';
  32. import 'activity_carousel.dart';
  33. import 'top_traders_section.dart';
  34. class HomeScreen extends ConsumerStatefulWidget {
  35. const HomeScreen({super.key});
  36. @override
  37. ConsumerState<HomeScreen> createState() => _HomeScreenState();
  38. }
  39. class _HomeScreenState extends ConsumerState<HomeScreen>
  40. with WidgetsBindingObserver {
  41. bool _popupQueueStarted = false;
  42. bool _updateDialogShown = false;
  43. Timer? _unreadPollTimer;
  44. @override
  45. void initState() {
  46. super.initState();
  47. WidgetsBinding.instance.addObserver(this);
  48. _unreadPollTimer = Timer.periodic(const Duration(minutes: 1), (_) {
  49. if (mounted) ref.invalidate(announcementUnreadProvider);
  50. });
  51. }
  52. @override
  53. void dispose() {
  54. _unreadPollTimer?.cancel();
  55. WidgetsBinding.instance.removeObserver(this);
  56. super.dispose();
  57. }
  58. @override
  59. void didChangeAppLifecycleState(AppLifecycleState state) {
  60. if (state == AppLifecycleState.resumed) {
  61. ref.invalidate(announcementUnreadProvider);
  62. }
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. final state = ref.watch(homeProvider);
  67. // 监听系统弹窗公告(支持多个,按顺序逐一弹出)
  68. ref.listen<AsyncValue<List<AnnouncementBean>>>(
  69. announcementPopupProvider,
  70. (_, next) {
  71. final beans = next.valueOrNull;
  72. if (beans != null &&
  73. beans.isNotEmpty &&
  74. !_popupQueueStarted &&
  75. context.mounted) {
  76. _popupQueueStarted = true;
  77. _showAnnouncementPopupQueue(context, beans, 0);
  78. }
  79. },
  80. );
  81. // 监听版本更新
  82. ref.listen<AsyncValue<VersionCheckResult?>>(
  83. appVersionProvider,
  84. (_, next) {
  85. next.whenData((result) {
  86. if (result != null &&
  87. result.hasUpdate &&
  88. !_updateDialogShown &&
  89. context.mounted) {
  90. _updateDialogShown = true;
  91. UpdateDialog.show(context, result);
  92. }
  93. });
  94. },
  95. );
  96. // 后端返回 4099 时重新触发版本检查并弹出更新弹窗
  97. ref.listen<bool>(
  98. versionOutdatedProvider,
  99. (_, isOutdated) {
  100. if (isOutdated && context.mounted) {
  101. _updateDialogShown = false;
  102. ref.invalidate(appVersionProvider);
  103. }
  104. },
  105. );
  106. return Scaffold(
  107. body: SafeArea(
  108. child: state.isLoading && state.tickers.isEmpty
  109. ? _HomeShimmer(isLoggedIn: state.isLoggedIn)
  110. : _HomeBody(state: state),
  111. ),
  112. );
  113. }
  114. void _showAnnouncementPopupQueue(
  115. BuildContext context, List<AnnouncementBean> beans, int index) {
  116. if (index >= beans.length || !context.mounted) return;
  117. final bean = beans[index];
  118. final cs = Theme.of(context).colorScheme;
  119. // 限制弹窗最大高度为屏幕 65%,给 Flexible 提供有界约束
  120. final maxHeight = MediaQuery.of(context).size.height * 0.65;
  121. showDialog<void>(
  122. context: context,
  123. barrierDismissible: true,
  124. builder: (dialogContext) => Dialog(
  125. backgroundColor: cs.surface,
  126. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  127. insetPadding: const EdgeInsets.symmetric(horizontal: 28, vertical: 48),
  128. child: ConstrainedBox(
  129. constraints: BoxConstraints(maxHeight: maxHeight),
  130. child: Column(
  131. mainAxisSize: MainAxisSize.min,
  132. crossAxisAlignment: CrossAxisAlignment.stretch,
  133. children: [
  134. // 标题(固定高度)
  135. Padding(
  136. padding: const EdgeInsets.fromLTRB(20, 20, 20, 16),
  137. child: Text(
  138. bean.title,
  139. style: TextStyle(
  140. color: cs.onSurface,
  141. fontSize: 16,
  142. fontWeight: FontWeight.w600,
  143. ),
  144. textAlign: TextAlign.center,
  145. ),
  146. ),
  147. Divider(height: 1, thickness: 1, color: cs.outline),
  148. // Flexible 在 ConstrainedBox 有界约束下正确填充剩余空间
  149. Flexible(
  150. child: SingleChildScrollView(
  151. padding: const EdgeInsets.fromLTRB(20, 16, 20, 16),
  152. child: HtmlWidget(
  153. bean.content,
  154. textStyle: TextStyle(
  155. color: cs.onSurface,
  156. fontSize: 14,
  157. height: 1.6,
  158. ),
  159. ),
  160. ),
  161. ),
  162. Divider(height: 1, thickness: 1, color: cs.outline),
  163. // 确认按钮(固定高度,始终在底部)
  164. Padding(
  165. padding: const EdgeInsets.fromLTRB(20, 12, 20, 20),
  166. child: ElevatedButton(
  167. onPressed: () => Navigator.of(dialogContext).pop(),
  168. child: Text(AppLocalizations.of(dialogContext)!.iUnderstand),
  169. ),
  170. ),
  171. ],
  172. ),
  173. ),
  174. ),
  175. ).whenComplete(() {
  176. // 无论按按钮还是点空白关闭,都标记已读并显示下一条
  177. markPopupShown(bean.id);
  178. final rid = int.tryParse(bean.id);
  179. if (rid != null) markAnnouncementsRead(ref, id: rid);
  180. if (context.mounted) {
  181. _showAnnouncementPopupQueue(context, beans, index + 1);
  182. }
  183. });
  184. }
  185. }
  186. // ── Shimmer 骨架屏 ──────────────────────────────────────────
  187. class _HomeShimmer extends StatelessWidget {
  188. const _HomeShimmer({required this.isLoggedIn});
  189. final bool isLoggedIn;
  190. @override
  191. Widget build(BuildContext context) {
  192. final cs = Theme.of(context).colorScheme;
  193. return Shimmer.fromColors(
  194. baseColor: cs.onSurface.withAlpha(15),
  195. highlightColor: cs.onSurface.withAlpha(30),
  196. child: CustomScrollView(
  197. physics: const NeverScrollableScrollPhysics(),
  198. slivers: [
  199. // AppBar 占位
  200. SliverToBoxAdapter(
  201. child: Padding(
  202. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  203. child: Row(
  204. children: [
  205. _shimmerCircle(32),
  206. const SizedBox(width: 10),
  207. Expanded(
  208. child: Container(
  209. height: 36,
  210. decoration: BoxDecoration(
  211. color: Colors.white,
  212. borderRadius: BorderRadius.circular(18),
  213. ),
  214. ),
  215. ),
  216. const SizedBox(width: 10),
  217. _shimmerCircle(22),
  218. const SizedBox(width: 14),
  219. _shimmerCircle(22),
  220. ],
  221. ),
  222. ),
  223. ),
  224. // 资产卡片 / 未登录引导区 占位
  225. if (isLoggedIn) ...[
  226. SliverToBoxAdapter(
  227. child: Container(
  228. margin: const EdgeInsets.fromLTRB(16, 8, 16, 4),
  229. padding:
  230. const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
  231. decoration: BoxDecoration(
  232. color: Colors.white,
  233. borderRadius: BorderRadius.circular(16),
  234. ),
  235. child: Column(
  236. crossAxisAlignment: CrossAxisAlignment.start,
  237. children: [
  238. _shimmerBox(80, 13),
  239. const SizedBox(height: 10),
  240. _shimmerBox(150, 28),
  241. const SizedBox(height: 10),
  242. _shimmerBox(200, 13),
  243. ],
  244. ),
  245. ),
  246. ),
  247. // 快捷入口占位
  248. SliverToBoxAdapter(
  249. child: Padding(
  250. padding:
  251. const EdgeInsets.symmetric(horizontal: 8, vertical: 12),
  252. child: Row(
  253. mainAxisAlignment: MainAxisAlignment.spaceAround,
  254. children: List.generate(5, (_) {
  255. return Column(
  256. children: [
  257. Container(
  258. width: 48,
  259. height: 48,
  260. decoration: BoxDecoration(
  261. color: Colors.white,
  262. borderRadius: BorderRadius.circular(14),
  263. ),
  264. ),
  265. const SizedBox(height: 6),
  266. _shimmerBox(30, 10),
  267. ],
  268. );
  269. }),
  270. ),
  271. ),
  272. ),
  273. ] else ...[
  274. SliverToBoxAdapter(
  275. child: Padding(
  276. padding: const EdgeInsets.fromLTRB(24, 16, 24, 16),
  277. child: Column(
  278. children: [
  279. _shimmerCircle(90),
  280. const SizedBox(height: 20),
  281. Container(
  282. height: 52,
  283. decoration: BoxDecoration(
  284. color: Colors.white,
  285. borderRadius: BorderRadius.circular(12),
  286. ),
  287. ),
  288. ],
  289. ),
  290. ),
  291. ),
  292. ],
  293. // Banner 占位
  294. SliverToBoxAdapter(
  295. child: Container(
  296. margin: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  297. height: 100,
  298. decoration: BoxDecoration(
  299. color: Colors.white,
  300. borderRadius: BorderRadius.circular(12),
  301. ),
  302. ),
  303. ),
  304. // 行情标题占位
  305. SliverToBoxAdapter(
  306. child: Padding(
  307. padding: const EdgeInsets.fromLTRB(16, 20, 16, 12),
  308. child: Row(
  309. children: [
  310. _shimmerBox(60, 14),
  311. const SizedBox(width: 20),
  312. _shimmerBox(50, 14),
  313. const SizedBox(width: 20),
  314. _shimmerBox(50, 14),
  315. ],
  316. ),
  317. ),
  318. ),
  319. // 行情列表行占位
  320. SliverList.builder(
  321. itemCount: 6,
  322. itemBuilder: (_, __) => Padding(
  323. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  324. child: MarketListDataRow(
  325. leading: Container(
  326. width: 36,
  327. height: 36,
  328. decoration: BoxDecoration(
  329. color: Colors.white,
  330. borderRadius: BorderRadius.circular(10),
  331. ),
  332. ),
  333. name: Column(
  334. crossAxisAlignment: CrossAxisAlignment.start,
  335. children: [
  336. _shimmerBox(70, 14),
  337. const SizedBox(height: 6),
  338. _shimmerBox(50, 11),
  339. ],
  340. ),
  341. price: Column(
  342. crossAxisAlignment: CrossAxisAlignment.end,
  343. mainAxisSize: MainAxisSize.min,
  344. children: [
  345. _shimmerBox(80, 14),
  346. const SizedBox(height: 6),
  347. _shimmerBox(50, 11),
  348. ],
  349. ),
  350. change: Container(
  351. height: 30,
  352. decoration: BoxDecoration(
  353. color: Colors.white,
  354. borderRadius: BorderRadius.circular(6),
  355. ),
  356. ),
  357. ),
  358. ),
  359. ),
  360. ],
  361. ),
  362. );
  363. }
  364. static Widget _shimmerBox(double width, double height) {
  365. return Container(
  366. width: width,
  367. height: height,
  368. decoration: BoxDecoration(
  369. color: Colors.white,
  370. borderRadius: BorderRadius.circular(4),
  371. ),
  372. );
  373. }
  374. static Widget _shimmerCircle(double size) {
  375. return Container(
  376. width: size,
  377. height: size,
  378. decoration: const BoxDecoration(
  379. color: Colors.white,
  380. shape: BoxShape.circle,
  381. ),
  382. );
  383. }
  384. }
  385. // ── 主体内容 ──────────────────────────────────────────────
  386. class _HomeBody extends ConsumerWidget {
  387. const _HomeBody({required this.state});
  388. final HomeState state;
  389. @override
  390. Widget build(BuildContext context, WidgetRef ref) {
  391. final notifier = ref.read(homeProvider.notifier);
  392. ref.watch(currencyProvider); // 法币切换时触发行情列表重建
  393. return AppRefreshIndicator(
  394. onRefresh: notifier.refresh,
  395. child: CustomScrollView(
  396. slivers: [
  397. // AppBar
  398. SliverToBoxAdapter(child: _HomeAppBar(state: state)),
  399. // 未登录:引导区域
  400. if (!state.isLoggedIn) ...[
  401. const SliverToBoxAdapter(child: _GuestHeroSection()),
  402. ],
  403. // 已登录:资产卡片 + 快捷入口
  404. if (state.isLoggedIn) ...[
  405. SliverToBoxAdapter(child: _AssetCard(state: state)),
  406. const SliverToBoxAdapter(child: _QuickActions()),
  407. // 活动专区
  408. SliverToBoxAdapter(child: ActivityCarousel(banners: state.banners)),
  409. ],
  410. // 顶级交易专家(登录/未登录共享)
  411. const SliverToBoxAdapter(child: TopTradersSection()),
  412. // 热门行情(登录/未登录共享)
  413. SliverToBoxAdapter(
  414. child: _MarketSectionHeader(
  415. tabIndex: state.marketTabIndex,
  416. notifier: notifier,
  417. showFavorites: state.isLoggedIn,
  418. ),
  419. ),
  420. const SliverToBoxAdapter(child: _MarketListHeader()),
  421. // tabIndex=4 展示现货,其余展示合约
  422. if (state.marketTabIndex == 4)
  423. _HomeSpotTickerSliver()
  424. else
  425. SliverList.builder(
  426. itemCount: state.displayTickers.length,
  427. itemBuilder: (context, index) {
  428. final ticker = state.displayTickers[index];
  429. return _MarketTickerRow(
  430. ticker: ticker,
  431. isFavorite: state.favorites.contains(ticker.symbol),
  432. onToggleFavorite: () =>
  433. notifier.toggleFavorite(ticker.symbol),
  434. onTap: () => context.push('/market/futures/${ticker.symbol}'),
  435. );
  436. },
  437. ),
  438. const SliverToBoxAdapter(child: SizedBox(height: 24)),
  439. ],
  440. ),
  441. );
  442. }
  443. }
  444. // ── AppBar ────────────────────────────────────────────────
  445. class _HomeAppBar extends ConsumerWidget {
  446. const _HomeAppBar({required this.state});
  447. final HomeState state;
  448. @override
  449. Widget build(BuildContext context, WidgetRef ref) {
  450. final cs = Theme.of(context).colorScheme;
  451. final isDark = Theme.of(context).brightness == Brightness.dark;
  452. return Padding(
  453. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  454. child: Row(
  455. children: [
  456. // 个人中心入口
  457. Semantics(
  458. label: 'home_btn_profile',
  459. button: true,
  460. enabled: state.isLoggedIn,
  461. onTap: state.isLoggedIn ? () => context.push('/user') : null,
  462. child: GestureDetector(
  463. onTap: () => context.push('/user'),
  464. child: Container(
  465. width: 32,
  466. height: 32,
  467. decoration: BoxDecoration(
  468. color: state.isLoggedIn
  469. ? (isDark
  470. ? AppColors.darkBgTertiary
  471. : AppColors.lightBgTertiary)
  472. : cs.onSurface.withAlpha(30),
  473. shape: BoxShape.circle,
  474. ),
  475. child: Icon(
  476. state.isLoggedIn ? Icons.person : Icons.person_outline,
  477. color: state.isLoggedIn
  478. ? cs.onSurface
  479. : cs.onSurface.withAlpha(153),
  480. size: 20,
  481. ),
  482. ),
  483. ),
  484. ),
  485. const SizedBox(width: 10),
  486. // 搜索栏
  487. Expanded(
  488. child: Semantics(
  489. label: 'home_search_market',
  490. onTap: () => context.go('/market'),
  491. child: GestureDetector(
  492. onTap: () => context.go('/market'),
  493. child: Container(
  494. height: 36,
  495. padding: const EdgeInsets.symmetric(horizontal: 12),
  496. decoration: BoxDecoration(
  497. color: isDark
  498. ? AppColors.darkBgTertiary
  499. : AppColors.lightBgTertiary,
  500. borderRadius: BorderRadius.circular(18),
  501. ),
  502. child: Row(
  503. children: [
  504. Icon(Icons.search,
  505. color: cs.onSurface.withAlpha(100), size: 18),
  506. const SizedBox(width: 6),
  507. Text(
  508. AppLocalizations.of(context)!.searchPair,
  509. style: TextStyle(
  510. color: cs.onSurface.withAlpha(100),
  511. fontSize: 13,
  512. ),
  513. ),
  514. ],
  515. ),
  516. ),
  517. ),
  518. ),
  519. ),
  520. const SizedBox(width: 10),
  521. // 客服入口
  522. if (AppConfig.customerServiceEnabled)
  523. Padding(
  524. padding: const EdgeInsets.only(right: 14),
  525. child: Semantics(
  526. label: 'home_btn_service',
  527. button: true,
  528. onTap: () => openCustomerService(context, ref),
  529. child: GestureDetector(
  530. onTap: () => openCustomerService(context, ref),
  531. child: Icon(Icons.headset_mic_outlined,
  532. color: cs.onSurface, size: 22),
  533. ),
  534. ),
  535. ),
  536. Semantics(
  537. label: 'home_btn_messages',
  538. button: true,
  539. onTap: () => context.push('/user/messages'),
  540. child: GestureDetector(
  541. onTap: () => context.push('/user/messages'),
  542. child: Stack(
  543. clipBehavior: Clip.none,
  544. children: [
  545. Icon(Icons.notifications_outlined,
  546. color: cs.onSurface, size: 22),
  547. if (ref.watch(announcementUnreadProvider).valueOrNull
  548. case final s? when s.isLoaded && s.hasUnread)
  549. Positioned(
  550. top: -2,
  551. right: -2,
  552. child: Container(
  553. width: 8,
  554. height: 8,
  555. decoration: const BoxDecoration(
  556. color: AppColors.fall,
  557. shape: BoxShape.circle,
  558. ),
  559. ),
  560. ),
  561. ],
  562. ),
  563. ),
  564. ),
  565. ],
  566. ),
  567. );
  568. }
  569. }
  570. // ── 未登录/已登录:主视觉引导区 ───────────────────────────
  571. class _GuestHeroSection extends ConsumerWidget {
  572. // ignore: unused_element_parameter
  573. const _GuestHeroSection({this.showLoginButton = true});
  574. final bool showLoginButton;
  575. Future<void> _onHeaderTap(BuildContext context, AppHeaderItem item) async {
  576. if (item.linkUrl.isEmpty) return;
  577. if (item.isExternal) {
  578. await launchUrl(Uri.parse(item.linkUrl),
  579. mode: LaunchMode.externalApplication);
  580. return;
  581. }
  582. final url = item.linkUrl;
  583. // 复用 ActivityCarousel 的跳转逻辑
  584. const tabRoutes = {
  585. '/',
  586. 'market',
  587. '/market',
  588. '/futures',
  589. '/copy-trading',
  590. '/asset'
  591. };
  592. final isTabRoute = tabRoutes.any((r) => url == r || url.startsWith('$r/'));
  593. if (!context.mounted) return;
  594. if (isTabRoute) {
  595. context.go(url);
  596. } else {
  597. context.push(url);
  598. }
  599. }
  600. @override
  601. Widget build(BuildContext context, WidgetRef ref) {
  602. // 仅在 appHeaders 变化时重建(不因行情 WS 刷新而重建)
  603. final appHeaders = ref.watch(homeProvider.select((s) => s.appHeaders));
  604. final children = [
  605. // Header 媒体区域
  606. _HomeHeaderMedia(
  607. appHeaders: appHeaders,
  608. onHeaderTap: (item) => _onHeaderTap(context, item),
  609. ),
  610. if (showLoginButton) ...[
  611. const SizedBox(height: 12),
  612. // 登录 / 注册 按钮
  613. SizedBox(
  614. width: double.infinity,
  615. height: 52,
  616. child: ElevatedButton(
  617. onPressed: () => context.push('/login'),
  618. style: ElevatedButton.styleFrom(
  619. backgroundColor: const Color(0xFF1E1E1E),
  620. foregroundColor: Colors.white,
  621. shape: RoundedRectangleBorder(
  622. borderRadius: BorderRadius.circular(12),
  623. ),
  624. elevation: 0,
  625. ),
  626. child: Text(
  627. AppLocalizations.of(context)!.loginRegister,
  628. style: const TextStyle(
  629. fontSize: 17,
  630. fontWeight: FontWeight.w600,
  631. letterSpacing: 1,
  632. ),
  633. ),
  634. ),
  635. ),
  636. ]
  637. ];
  638. return Padding(
  639. padding: EdgeInsets.fromLTRB(16, 8, 16, showLoginButton ? 0 : 12),
  640. child: Column(children: children),
  641. );
  642. }
  643. }
  644. // ── Header 媒体区域(统一走图片管线:本地 WebP 动图 / 远程图片)──
  645. class _HomeHeaderMedia extends StatelessWidget {
  646. const _HomeHeaderMedia({
  647. required this.appHeaders,
  648. required this.onHeaderTap,
  649. });
  650. final List<AppHeaderItem> appHeaders;
  651. final Future<void> Function(AppHeaderItem) onHeaderTap;
  652. @override
  653. Widget build(BuildContext context) {
  654. final cs = Theme.of(context).colorScheme;
  655. final brightness = Theme.of(context).brightness;
  656. Widget placeholder() => Container(
  657. color: cs.surface,
  658. child: Center(
  659. child: Icon(
  660. Icons.image_outlined,
  661. color: cs.onSurface.withAlpha(60),
  662. size: 32,
  663. ),
  664. ),
  665. );
  666. Widget errorBox() => Container(
  667. color: cs.surface,
  668. child: Center(
  669. child: Icon(
  670. Icons.broken_image_outlined,
  671. color: cs.onSurface.withAlpha(80),
  672. size: 28,
  673. ),
  674. ),
  675. );
  676. // 本地兜底:深色/浅色 PNG(与 assets/animations 资源一致)
  677. Widget localFallback() {
  678. final asset = brightness == Brightness.dark
  679. ? 'assets/animations/home_header_dart.png'
  680. : 'assets/animations/home_header_light.png';
  681. return Image.asset(
  682. asset,
  683. fit: BoxFit.cover,
  684. gaplessPlayback: true,
  685. errorBuilder: (_, __, ___) => errorBox(),
  686. );
  687. }
  688. // 接口返回时用远程图片,否则走本地兜底
  689. Widget content;
  690. VoidCallback? onTap;
  691. if (appHeaders.isNotEmpty) {
  692. final item = appHeaders.first;
  693. final url = item.resolveUrl(brightness);
  694. onTap = () => onHeaderTap(item);
  695. if (url.isNotEmpty) {
  696. content = CachedNetworkImage(
  697. imageUrl: url,
  698. fit: BoxFit.cover,
  699. placeholder: (_, __) => placeholder(),
  700. errorWidget: (_, __, ___) => localFallback(),
  701. );
  702. } else {
  703. content = localFallback();
  704. }
  705. } else {
  706. content = localFallback();
  707. }
  708. final media = ClipRRect(
  709. borderRadius: BorderRadius.circular(16),
  710. child: AspectRatio(
  711. aspectRatio: 16 / 9,
  712. child: Container(
  713. color: Colors.black,
  714. child: content,
  715. ),
  716. ),
  717. );
  718. return onTap == null ? media : GestureDetector(onTap: onTap, child: media);
  719. }
  720. }
  721. // ══════════════════════════════════════════════════════════
  722. // 以下是已登录状态下的组件
  723. // ══════════════════════════════════════════════════════════
  724. // ── 已登录:资产卡片 ───────────────────────────────────────
  725. class _AssetCard extends ConsumerWidget {
  726. const _AssetCard({required this.state});
  727. final HomeState state;
  728. @override
  729. Widget build(BuildContext context, WidgetRef ref) {
  730. final cs = Theme.of(context).colorScheme;
  731. final isDark = Theme.of(context).brightness == Brightness.dark;
  732. final assetState = ref.watch(assetProvider);
  733. final obscure = assetState.obscureBalance;
  734. final pnlColor = AppColors.changeColor(state.todayPnl);
  735. final sign = state.todayPnl >= 0 ? '+' : '';
  736. return Container(
  737. margin: const EdgeInsets.fromLTRB(16, 8, 16, 4),
  738. padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
  739. decoration: BoxDecoration(
  740. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  741. borderRadius: BorderRadius.circular(16),
  742. border: isDark
  743. ? null
  744. : Border.all(color: AppColors.lightBorder, width: 0.5),
  745. ),
  746. child: Column(
  747. crossAxisAlignment: CrossAxisAlignment.start,
  748. children: [
  749. Row(
  750. children: [
  751. Text(
  752. AppLocalizations.of(context)!.totalAssetsValue,
  753. style:
  754. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 13),
  755. ),
  756. const SizedBox(width: 6),
  757. GestureDetector(
  758. onTap: () => ref.read(assetProvider.notifier).toggleObscure(),
  759. child: Icon(
  760. obscure
  761. ? Icons.visibility_off_outlined
  762. : Icons.visibility_outlined,
  763. size: 16,
  764. color: cs.onSurface.withAlpha(153),
  765. ),
  766. ),
  767. const Spacer(),
  768. GestureDetector(
  769. onTap: () => context.push('/asset/deposit'),
  770. child: Container(
  771. padding:
  772. const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
  773. decoration: BoxDecoration(
  774. color: AppColors.brand,
  775. borderRadius: BorderRadius.circular(4),
  776. ),
  777. child: Text(
  778. AppLocalizations.of(context)!.recharge,
  779. style: const TextStyle(
  780. color: Colors.black,
  781. fontSize: 12,
  782. fontWeight: FontWeight.w500),
  783. ),
  784. ),
  785. ),
  786. ],
  787. ),
  788. const SizedBox(height: 6),
  789. Row(
  790. crossAxisAlignment: CrossAxisAlignment.end,
  791. children: [
  792. Flexible(
  793. child: Text(
  794. obscure
  795. ? '****'
  796. : formatPrice(state.totalAsset, decimalPlaces: 2),
  797. maxLines: 1,
  798. overflow: TextOverflow.ellipsis,
  799. style: TextStyle(
  800. color: cs.onSurface,
  801. fontSize: 28,
  802. fontWeight: FontWeight.w700,
  803. letterSpacing: -0.5,
  804. ),
  805. ),
  806. ),
  807. const SizedBox(width: 6),
  808. Padding(
  809. padding: const EdgeInsets.only(bottom: 4),
  810. child: Text(
  811. 'USDT',
  812. style: TextStyle(
  813. color: cs.onSurface.withAlpha(153), fontSize: 13),
  814. ),
  815. ),
  816. ],
  817. ),
  818. const SizedBox(height: 6),
  819. Row(
  820. children: [
  821. Text(
  822. AppLocalizations.of(context)!.todayPnl,
  823. style:
  824. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 12),
  825. ),
  826. const SizedBox(width: 8),
  827. Flexible(
  828. child: Text(
  829. obscure
  830. ? '****'
  831. : state.todayPnlRateAvailable
  832. ? '$sign$fiatSymbol${(state.todayPnl * fiatRate).toStringAsFixed(2)} ($sign${state.todayPnlPct.toStringAsFixed(2)}%)'
  833. : '$sign$fiatSymbol${(state.todayPnl * fiatRate).toStringAsFixed(2)} (--)',
  834. maxLines: 1,
  835. overflow: TextOverflow.ellipsis,
  836. style: TextStyle(
  837. color: obscure ? cs.onSurface.withAlpha(153) : pnlColor,
  838. fontSize: 13,
  839. fontWeight: FontWeight.w500,
  840. ),
  841. ),
  842. ),
  843. ],
  844. ),
  845. ],
  846. ),
  847. );
  848. }
  849. }
  850. // ── 快捷入口 ──────────────────────────────────────────────
  851. class _QuickActions extends ConsumerWidget {
  852. const _QuickActions();
  853. static const _actionDefs = [
  854. (
  855. icon: Icons.trending_up,
  856. key: 'perpetual',
  857. route: '/futures/BTCUSDT',
  858. useGo: true
  859. ),
  860. (
  861. icon: Icons.people_alt_outlined,
  862. key: 'copy',
  863. route: '/copy-trading',
  864. useGo: true
  865. ),
  866. (
  867. icon: Icons.account_balance_wallet_outlined,
  868. key: 'recharge',
  869. route: '/asset/deposit',
  870. useGo: false
  871. ),
  872. (
  873. icon: Icons.card_giftcard_outlined,
  874. key: 'invite',
  875. route: '/user/referral',
  876. useGo: false
  877. ),
  878. (
  879. icon: Icons.business_center_outlined,
  880. key: 'broker',
  881. route: '/broker',
  882. useGo: false
  883. ),
  884. (icon: Icons.auto_graph, key: 'ido', route: '/finance/ido', useGo: false),
  885. (
  886. icon: Icons.bar_chart_rounded,
  887. key: 'commodity',
  888. route: '',
  889. useGo: false
  890. ),
  891. (
  892. icon: Icons.candlestick_chart,
  893. key: 'stock',
  894. route: '',
  895. useGo: false
  896. ),
  897. (icon: Icons.currency_exchange, key: 'forex', route: '', useGo: false),
  898. (icon: Icons.apps_outlined, key: 'app', route: '', useGo: false),
  899. ];
  900. Future<void> _onBrokerTap(BuildContext context, WidgetRef ref) async {
  901. await openBrokerEntry(context, ref);
  902. }
  903. static void _showDevelopingDialog(BuildContext context) {
  904. showDialog<void>(
  905. context: context,
  906. barrierDismissible: true,
  907. builder: (dialogContext) => Dialog(
  908. backgroundColor: Colors.white,
  909. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
  910. insetPadding: const EdgeInsets.symmetric(horizontal: 48, vertical: 48),
  911. child: Padding(
  912. padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 28),
  913. child: Column(
  914. mainAxisSize: MainAxisSize.min,
  915. children: [
  916. const Text(
  917. '敬请期待',
  918. style: TextStyle(
  919. color: Colors.black,
  920. fontSize: 16,
  921. fontWeight: FontWeight.w600,
  922. ),
  923. ),
  924. const SizedBox(height: 8),
  925. const Text(
  926. '该功能正在开发中',
  927. style: TextStyle(
  928. color: Color(0xFF666666),
  929. fontSize: 13,
  930. ),
  931. ),
  932. const SizedBox(height: 20),
  933. SizedBox(
  934. width: double.infinity,
  935. child: TextButton(
  936. onPressed: () => Navigator.of(dialogContext).pop(),
  937. style: TextButton.styleFrom(
  938. backgroundColor: Colors.black,
  939. foregroundColor: Colors.white,
  940. shape: RoundedRectangleBorder(
  941. borderRadius: BorderRadius.circular(8),
  942. ),
  943. padding: const EdgeInsets.symmetric(vertical: 12),
  944. ),
  945. child: const Text('确定'),
  946. ),
  947. ),
  948. ],
  949. ),
  950. ),
  951. ),
  952. );
  953. }
  954. @override
  955. Widget build(BuildContext context, WidgetRef ref) {
  956. final l10n = AppLocalizations.of(context)!;
  957. final labelMap = {
  958. 'perpetual': l10n.perpetualFutures,
  959. 'copy': l10n.copyTrading,
  960. 'recharge': l10n.recharge,
  961. 'invite': l10n.inviteFriends,
  962. 'broker': l10n.broker,
  963. 'ido': 'IDO理财',
  964. 'commodity': '大宗贵金属',
  965. 'stock': '股票',
  966. 'forex': '外汇',
  967. 'app': '应用',
  968. };
  969. final semanticsMap = {
  970. 'perpetual': 'home_btn_futures',
  971. 'copy': 'home_btn_copy_trading',
  972. 'recharge': 'home_btn_deposit',
  973. 'invite': 'home_btn_referral',
  974. 'broker': 'home_btn_broker',
  975. 'ido': 'home_btn_ido',
  976. 'commodity': 'home_btn_commodity',
  977. 'stock': 'home_btn_stock',
  978. 'forex': 'home_btn_forex',
  979. 'app': 'home_btn_app',
  980. };
  981. const developingKeys = {'commodity', 'stock', 'forex'};
  982. List<Widget> buildActionItems() {
  983. return _actionDefs.map((action) {
  984. return _QuickActionItem(
  985. icon: action.icon,
  986. label: labelMap[action.key]!,
  987. semanticsLabel: semanticsMap[action.key]!,
  988. onTap: developingKeys.contains(action.key)
  989. ? () => _showDevelopingDialog(context)
  990. : action.key == 'app'
  991. ? () => context.push('/app')
  992. : action.key == 'broker'
  993. ? () => _onBrokerTap(context, ref)
  994. : () => action.useGo
  995. ? context.go(action.route)
  996. : context.push(action.route),
  997. );
  998. }).toList();
  999. }
  1000. return Padding(
  1001. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 12),
  1002. child: GridView.count(
  1003. crossAxisCount: 5,
  1004. shrinkWrap: true,
  1005. physics: const NeverScrollableScrollPhysics(),
  1006. mainAxisSpacing: 12,
  1007. crossAxisSpacing: 4,
  1008. childAspectRatio: 0.84,
  1009. children: buildActionItems(),
  1010. ),
  1011. );
  1012. }
  1013. }
  1014. class _QuickActionItem extends StatelessWidget {
  1015. const _QuickActionItem({
  1016. required this.icon,
  1017. required this.label,
  1018. required this.semanticsLabel,
  1019. required this.onTap,
  1020. });
  1021. final IconData icon;
  1022. final String label;
  1023. final String semanticsLabel;
  1024. final VoidCallback onTap;
  1025. @override
  1026. Widget build(BuildContext context) {
  1027. final cs = Theme.of(context).colorScheme;
  1028. final isDark = Theme.of(context).brightness == Brightness.dark;
  1029. return Semantics(
  1030. label: semanticsLabel,
  1031. button: true,
  1032. onTap: onTap,
  1033. child: GestureDetector(
  1034. onTap: onTap,
  1035. child: SizedBox(
  1036. width: 62,
  1037. child: Column(
  1038. mainAxisSize: MainAxisSize.min,
  1039. children: [
  1040. Container(
  1041. width: 48,
  1042. height: 48,
  1043. decoration: BoxDecoration(
  1044. color: isDark
  1045. ? AppColors.darkBgTertiary
  1046. : AppColors.lightBgTertiary,
  1047. borderRadius: BorderRadius.circular(14),
  1048. ),
  1049. child: Icon(icon, color: AppColors.brand, size: 22),
  1050. ),
  1051. const SizedBox(height: 6),
  1052. Text(
  1053. label == "应用" ? '$label >' : label,
  1054. style: TextStyle(
  1055. color: cs.onSurface.withAlpha(153),
  1056. fontSize: 11,
  1057. ),
  1058. textAlign: TextAlign.center,
  1059. ),
  1060. ],
  1061. ),
  1062. ),
  1063. ),
  1064. );
  1065. }
  1066. }
  1067. // ── 行情区块:标题 + Tab ──────────────────────────────────
  1068. class _MarketSectionHeader extends StatelessWidget {
  1069. const _MarketSectionHeader({
  1070. required this.tabIndex,
  1071. required this.notifier,
  1072. this.showFavorites = true,
  1073. });
  1074. final int tabIndex;
  1075. final HomeNotifier notifier;
  1076. final bool showFavorites;
  1077. List<String> _getTabs(BuildContext context) {
  1078. final l10n = AppLocalizations.of(context)!;
  1079. return [l10n.hotTrading, l10n.gainers, l10n.losers, l10n.spotTab];
  1080. }
  1081. static const _tabIndices = [1, 2, 3, 4];
  1082. @override
  1083. Widget build(BuildContext context) {
  1084. final cs = Theme.of(context).colorScheme;
  1085. return Padding(
  1086. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  1087. child: Row(
  1088. children: List.generate(_tabIndices.length, (i) {
  1089. final tabs = _getTabs(context);
  1090. final actualIndex = _tabIndices[i];
  1091. final selected = actualIndex == tabIndex;
  1092. final tabSemantics = [
  1093. 'home_tab_market_hot',
  1094. 'home_tab_market_gainers',
  1095. 'home_tab_market_losers',
  1096. 'home_tab_market_spot'
  1097. ];
  1098. return Semantics(
  1099. label: tabSemantics[i],
  1100. button: true,
  1101. enabled: true,
  1102. onTap: () => notifier.setMarketTab(actualIndex),
  1103. child: GestureDetector(
  1104. onTap: () => notifier.setMarketTab(actualIndex),
  1105. child: Padding(
  1106. padding: const EdgeInsets.only(right: 20),
  1107. child: Column(
  1108. crossAxisAlignment: CrossAxisAlignment.start,
  1109. children: [
  1110. Text(
  1111. tabs[i],
  1112. style: TextStyle(
  1113. fontSize: 14,
  1114. fontWeight:
  1115. selected ? FontWeight.w600 : FontWeight.w400,
  1116. color: selected
  1117. ? cs.onSurface
  1118. : cs.onSurface.withAlpha(153),
  1119. ),
  1120. ),
  1121. const SizedBox(height: 4),
  1122. if (selected)
  1123. Container(height: 2, width: 20, color: AppColors.brand),
  1124. ],
  1125. ),
  1126. ),
  1127. ),
  1128. );
  1129. }),
  1130. ),
  1131. );
  1132. }
  1133. }
  1134. // ── 行情列表表头 ──────────────────────────────────────────
  1135. // ── 首页现货行情 Sliver(tabIndex=4)─────────────────────────
  1136. class _HomeSpotTickerSliver extends ConsumerWidget {
  1137. @override
  1138. Widget build(BuildContext context, WidgetRef ref) {
  1139. final symbols = ref.watch(
  1140. marketProvider.select((s) => s.spotDisplaySymbols.take(10).toList()),
  1141. );
  1142. final spotLoading = ref.watch(
  1143. marketProvider.select((s) => s.spotLoading),
  1144. );
  1145. // 首次进入现货 Tab 时触发加载
  1146. final loaded =
  1147. ref.watch(marketProvider.select((s) => s.spotTickers.isNotEmpty));
  1148. if (!loaded && !spotLoading) {
  1149. Future.microtask(() {
  1150. ref.read(marketProvider.notifier).setMode(MarketMode.spot);
  1151. });
  1152. }
  1153. if (spotLoading || symbols.isEmpty) {
  1154. return SliverToBoxAdapter(
  1155. child: SizedBox(
  1156. height: 80,
  1157. child: const Center(child: CircularProgressIndicator()),
  1158. ),
  1159. );
  1160. }
  1161. return SliverList.builder(
  1162. itemCount: symbols.length,
  1163. itemBuilder: (context, index) {
  1164. final sym = symbols[index];
  1165. return _HomeSpotTickerRow(symbol: sym);
  1166. },
  1167. );
  1168. }
  1169. }
  1170. class _HomeSpotTickerRow extends ConsumerWidget {
  1171. const _HomeSpotTickerRow({required this.symbol});
  1172. final String symbol;
  1173. @override
  1174. Widget build(BuildContext context, WidgetRef ref) {
  1175. final cs = Theme.of(context).colorScheme;
  1176. final ticker = ref.watch(spotTickerProvider(symbol));
  1177. if (ticker == null) return const SizedBox.shrink();
  1178. final changeColor = AppColors.changeColor(ticker.change24h);
  1179. final changeStr = formatChange(ticker.change24h);
  1180. return InkWell(
  1181. onTap: () => context.push('/market/spot/${ticker.symbol}'),
  1182. child: Padding(
  1183. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1184. child: MarketListDataRow(
  1185. leading: CoinIcon(
  1186. symbol: ticker.baseAsset,
  1187. iconUrl: ticker.icon,
  1188. size: 36,
  1189. borderRadius: 10,
  1190. ),
  1191. name: Column(
  1192. crossAxisAlignment: CrossAxisAlignment.start,
  1193. children: [
  1194. Text(
  1195. formatUsdtPairDisplay(ticker.symbol),
  1196. style: TextStyle(
  1197. color: cs.onSurface,
  1198. fontSize: 14,
  1199. fontWeight: FontWeight.w500,
  1200. ),
  1201. maxLines: 1,
  1202. overflow: TextOverflow.ellipsis,
  1203. ),
  1204. Text(
  1205. AppLocalizations.of(context)!.spot,
  1206. style: TextStyle(
  1207. color: cs.onSurface.withAlpha(153),
  1208. fontSize: 11,
  1209. ),
  1210. ),
  1211. ],
  1212. ),
  1213. price: Column(
  1214. crossAxisAlignment: CrossAxisAlignment.end,
  1215. mainAxisSize: MainAxisSize.min,
  1216. children: [
  1217. Text(
  1218. ticker.lastPrice > 0
  1219. ? (ticker.lastPriceStr != null
  1220. ? formatRawPrice(ticker.lastPriceStr!)
  1221. : formatPrice(ticker.lastPrice))
  1222. : '--',
  1223. style: TextStyle(
  1224. color: cs.onSurface,
  1225. fontSize: 14,
  1226. fontWeight: FontWeight.w500,
  1227. fontFeatures: const [FontFeature.tabularFigures()],
  1228. ),
  1229. maxLines: 1,
  1230. overflow: TextOverflow.ellipsis,
  1231. ),
  1232. Text(
  1233. ticker.lastPrice > 0 ? formatFiatPrice(ticker.lastPrice) : '--',
  1234. style: TextStyle(
  1235. color: cs.onSurface.withAlpha(153),
  1236. fontSize: 11,
  1237. fontFeatures: const [FontFeature.tabularFigures()],
  1238. ),
  1239. maxLines: 1,
  1240. overflow: TextOverflow.ellipsis,
  1241. ),
  1242. ],
  1243. ),
  1244. change: Container(
  1245. height: 34,
  1246. alignment: Alignment.center,
  1247. decoration: BoxDecoration(
  1248. color: changeColor,
  1249. borderRadius: BorderRadius.circular(6),
  1250. ),
  1251. child: Text(
  1252. changeStr,
  1253. style: const TextStyle(
  1254. color: Colors.white,
  1255. fontSize: 13,
  1256. fontWeight: FontWeight.w600,
  1257. fontFeatures: [FontFeature.tabularFigures()],
  1258. ),
  1259. textAlign: TextAlign.center,
  1260. ),
  1261. ),
  1262. ),
  1263. ),
  1264. );
  1265. }
  1266. }
  1267. class _MarketListHeader extends StatelessWidget {
  1268. const _MarketListHeader();
  1269. @override
  1270. Widget build(BuildContext context) {
  1271. final cs = Theme.of(context).colorScheme;
  1272. return Padding(
  1273. padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
  1274. child: MarketListHeaderRow(
  1275. leading: const SizedBox(width: 36),
  1276. name: Text(
  1277. AppLocalizations.of(context)!.coinNameLabel,
  1278. style: TextStyle(
  1279. color: cs.onSurface.withAlpha(153),
  1280. fontSize: 12,
  1281. ),
  1282. ),
  1283. price: Text(
  1284. AppLocalizations.of(context)!.latestPrice,
  1285. textAlign: TextAlign.end,
  1286. style: TextStyle(
  1287. color: cs.onSurface.withAlpha(153),
  1288. fontSize: 12,
  1289. ),
  1290. ),
  1291. change: Text(
  1292. AppLocalizations.of(context)!.change24h,
  1293. textAlign: TextAlign.center,
  1294. style: TextStyle(
  1295. color: cs.onSurface.withAlpha(153),
  1296. fontSize: 12,
  1297. ),
  1298. ),
  1299. ),
  1300. );
  1301. }
  1302. }
  1303. // ── 行情行 ────────────────────────────────────────────────
  1304. class _MarketTickerRow extends StatelessWidget {
  1305. const _MarketTickerRow({
  1306. required this.ticker,
  1307. required this.isFavorite,
  1308. required this.onToggleFavorite,
  1309. required this.onTap,
  1310. });
  1311. final MarketTicker ticker;
  1312. final bool isFavorite;
  1313. final VoidCallback onToggleFavorite;
  1314. final VoidCallback onTap;
  1315. @override
  1316. Widget build(BuildContext context) {
  1317. final cs = Theme.of(context).colorScheme;
  1318. final color = AppColors.changeColor(ticker.change24h);
  1319. final changeStr = formatChange(ticker.change24h);
  1320. final vol = ticker.volume24h;
  1321. final volStr = vol >= 1e9
  1322. ? '${(vol / 1e9).toStringAsFixed(2)}B'
  1323. : '${(vol / 1e6).toStringAsFixed(0)}M';
  1324. return InkWell(
  1325. onTap: onTap,
  1326. child: Padding(
  1327. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1328. child: MarketListDataRow(
  1329. leading: CoinIcon(
  1330. symbol: ticker.baseAsset,
  1331. iconUrl: ticker.icon,
  1332. size: 36,
  1333. borderRadius: 10,
  1334. ),
  1335. name: Column(
  1336. crossAxisAlignment: CrossAxisAlignment.start,
  1337. children: [
  1338. Text(
  1339. formatUsdtPairDisplay(ticker.symbol),
  1340. style: TextStyle(
  1341. color: cs.onSurface,
  1342. fontSize: 14,
  1343. fontWeight: FontWeight.w500,
  1344. ),
  1345. maxLines: 1,
  1346. overflow: TextOverflow.ellipsis,
  1347. ),
  1348. Text(
  1349. ticker.isFutures
  1350. ? '${AppLocalizations.of(context)!.turnover} $volStr'
  1351. : AppLocalizations.of(context)!.spot,
  1352. style: TextStyle(
  1353. color: cs.onSurface.withAlpha(153),
  1354. fontSize: 11,
  1355. ),
  1356. maxLines: 1,
  1357. overflow: TextOverflow.ellipsis,
  1358. ),
  1359. ],
  1360. ),
  1361. price: Column(
  1362. crossAxisAlignment: CrossAxisAlignment.end,
  1363. mainAxisSize: MainAxisSize.min,
  1364. children: [
  1365. Text(
  1366. ticker.lastPriceStr != null
  1367. ? formatRawPrice(ticker.lastPriceStr!)
  1368. : formatPrice(ticker.lastPrice,
  1369. decimalPlaces: ticker.pricePrecision),
  1370. style: TextStyle(
  1371. color: cs.onSurface,
  1372. fontSize: 14,
  1373. fontWeight: FontWeight.w500,
  1374. fontFeatures: const [FontFeature.tabularFigures()],
  1375. ),
  1376. maxLines: 1,
  1377. overflow: TextOverflow.ellipsis,
  1378. ),
  1379. Text(
  1380. formatFiatPrice(ticker.lastPrice,
  1381. pricePrecision: ticker.pricePrecision),
  1382. style: TextStyle(
  1383. color: cs.onSurface.withAlpha(153),
  1384. fontSize: 11,
  1385. fontFeatures: const [FontFeature.tabularFigures()],
  1386. ),
  1387. maxLines: 1,
  1388. overflow: TextOverflow.ellipsis,
  1389. ),
  1390. ],
  1391. ),
  1392. change: Container(
  1393. height: 34,
  1394. alignment: Alignment.center,
  1395. decoration: BoxDecoration(
  1396. color: color,
  1397. borderRadius: BorderRadius.circular(6),
  1398. ),
  1399. child: Text(
  1400. changeStr,
  1401. style: const TextStyle(
  1402. color: Colors.white,
  1403. fontSize: 13,
  1404. fontWeight: FontWeight.w600,
  1405. fontFeatures: [FontFeature.tabularFigures()],
  1406. ),
  1407. textAlign: TextAlign.center,
  1408. ),
  1409. ),
  1410. ),
  1411. ),
  1412. );
  1413. }
  1414. }