home_screen.dart 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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 total = assetState.totalUsdtValue;
  735. final pnl = assetState.todayPnl;
  736. final pnlRevenue = pnl?.revenue.toDouble() ?? 0;
  737. final double? pnlRate = pnl?.revenueRate?.toDouble();
  738. final pnlColor = AppColors.changeColor(pnlRevenue);
  739. final sign = pnlRevenue >= 0 ? '+' : '';
  740. return Container(
  741. margin: const EdgeInsets.fromLTRB(16, 8, 16, 4),
  742. padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
  743. decoration: BoxDecoration(
  744. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  745. borderRadius: BorderRadius.circular(16),
  746. border: isDark
  747. ? null
  748. : Border.all(color: AppColors.lightBorder, width: 0.5),
  749. ),
  750. child: Column(
  751. crossAxisAlignment: CrossAxisAlignment.start,
  752. children: [
  753. Row(
  754. children: [
  755. Text(
  756. AppLocalizations.of(context)!.totalAssetsValue,
  757. style:
  758. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 13),
  759. ),
  760. const SizedBox(width: 6),
  761. GestureDetector(
  762. onTap: () => ref.read(assetProvider.notifier).toggleObscure(),
  763. child: Icon(
  764. obscure
  765. ? Icons.visibility_off_outlined
  766. : Icons.visibility_outlined,
  767. size: 16,
  768. color: cs.onSurface.withAlpha(153),
  769. ),
  770. ),
  771. const Spacer(),
  772. GestureDetector(
  773. onTap: () => context.push('/asset/deposit'),
  774. child: Container(
  775. padding:
  776. const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
  777. decoration: BoxDecoration(
  778. color: AppColors.brand,
  779. borderRadius: BorderRadius.circular(4),
  780. ),
  781. child: Text(
  782. AppLocalizations.of(context)!.recharge,
  783. style: const TextStyle(
  784. color: Colors.black,
  785. fontSize: 12,
  786. fontWeight: FontWeight.w500),
  787. ),
  788. ),
  789. ),
  790. ],
  791. ),
  792. const SizedBox(height: 6),
  793. Row(
  794. crossAxisAlignment: CrossAxisAlignment.end,
  795. children: [
  796. Flexible(
  797. child: Text(
  798. obscure
  799. ? '****'
  800. : formatPrice(total, decimalPlaces: 2),
  801. maxLines: 1,
  802. overflow: TextOverflow.ellipsis,
  803. style: TextStyle(
  804. color: cs.onSurface,
  805. fontSize: 28,
  806. fontWeight: FontWeight.w700,
  807. letterSpacing: -0.5,
  808. ),
  809. ),
  810. ),
  811. const SizedBox(width: 6),
  812. Padding(
  813. padding: const EdgeInsets.only(bottom: 4),
  814. child: Text(
  815. 'USDT',
  816. style: TextStyle(
  817. color: cs.onSurface.withAlpha(153), fontSize: 13),
  818. ),
  819. ),
  820. ],
  821. ),
  822. const SizedBox(height: 6),
  823. Row(
  824. children: [
  825. Text(
  826. AppLocalizations.of(context)!.todayPnl,
  827. style:
  828. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 12),
  829. ),
  830. const SizedBox(width: 8),
  831. Flexible(
  832. child: Text(
  833. obscure
  834. ? '****'
  835. : pnlRate != null
  836. ? '$sign$fiatSymbol${(pnlRevenue * fiatRate).toStringAsFixed(2)} ($sign${(pnlRate * 100).toStringAsFixed(2)}%)'
  837. : '$sign$fiatSymbol${(pnlRevenue * fiatRate).toStringAsFixed(2)} (--)',
  838. maxLines: 1,
  839. overflow: TextOverflow.ellipsis,
  840. style: TextStyle(
  841. color: obscure ? cs.onSurface.withAlpha(153) : pnlColor,
  842. fontSize: 13,
  843. fontWeight: FontWeight.w500,
  844. ),
  845. ),
  846. ),
  847. ],
  848. ),
  849. ],
  850. ),
  851. );
  852. }
  853. }
  854. // ── 快捷入口 ──────────────────────────────────────────────
  855. class _QuickActions extends ConsumerWidget {
  856. const _QuickActions();
  857. static const _actionDefs = [
  858. (
  859. icon: Icons.trending_up,
  860. key: 'perpetual',
  861. route: '/futures/BTCUSDT',
  862. useGo: true
  863. ),
  864. (
  865. icon: Icons.people_alt_outlined,
  866. key: 'copy',
  867. route: '/copy-trading',
  868. useGo: true
  869. ),
  870. (
  871. icon: Icons.account_balance_wallet_outlined,
  872. key: 'recharge',
  873. route: '/asset/deposit',
  874. useGo: false
  875. ),
  876. (
  877. icon: Icons.card_giftcard_outlined,
  878. key: 'invite',
  879. route: '/user/referral',
  880. useGo: false
  881. ),
  882. (
  883. icon: Icons.business_center_outlined,
  884. key: 'broker',
  885. route: '/broker',
  886. useGo: false
  887. ),
  888. (icon: Icons.auto_graph, key: 'ido', route: '/finance/ido', useGo: false),
  889. (
  890. icon: Icons.bar_chart_rounded,
  891. key: 'commodity',
  892. route: '',
  893. useGo: false
  894. ),
  895. (
  896. icon: Icons.candlestick_chart,
  897. key: 'stock',
  898. route: '',
  899. useGo: false
  900. ),
  901. (icon: Icons.currency_exchange, key: 'forex', route: '', useGo: false),
  902. (icon: Icons.apps_outlined, key: 'app', route: '', useGo: false),
  903. ];
  904. Future<void> _onBrokerTap(BuildContext context, WidgetRef ref) async {
  905. await openBrokerEntry(context, ref);
  906. }
  907. static void _showDevelopingDialog(BuildContext context) {
  908. showDialog<void>(
  909. context: context,
  910. barrierDismissible: true,
  911. builder: (dialogContext) => Dialog(
  912. backgroundColor: Colors.white,
  913. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
  914. insetPadding: const EdgeInsets.symmetric(horizontal: 48, vertical: 48),
  915. child: Padding(
  916. padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 28),
  917. child: Column(
  918. mainAxisSize: MainAxisSize.min,
  919. children: [
  920. const Text(
  921. '敬请期待',
  922. style: TextStyle(
  923. color: Colors.black,
  924. fontSize: 16,
  925. fontWeight: FontWeight.w600,
  926. ),
  927. ),
  928. const SizedBox(height: 8),
  929. const Text(
  930. '该功能正在开发中',
  931. style: TextStyle(
  932. color: Color(0xFF666666),
  933. fontSize: 13,
  934. ),
  935. ),
  936. const SizedBox(height: 20),
  937. SizedBox(
  938. width: double.infinity,
  939. child: TextButton(
  940. onPressed: () => Navigator.of(dialogContext).pop(),
  941. style: TextButton.styleFrom(
  942. backgroundColor: Colors.black,
  943. foregroundColor: Colors.white,
  944. shape: RoundedRectangleBorder(
  945. borderRadius: BorderRadius.circular(8),
  946. ),
  947. padding: const EdgeInsets.symmetric(vertical: 12),
  948. ),
  949. child: const Text('确定'),
  950. ),
  951. ),
  952. ],
  953. ),
  954. ),
  955. ),
  956. );
  957. }
  958. @override
  959. Widget build(BuildContext context, WidgetRef ref) {
  960. final l10n = AppLocalizations.of(context)!;
  961. final labelMap = {
  962. 'perpetual': l10n.perpetualFutures,
  963. 'copy': l10n.copyTrading,
  964. 'recharge': l10n.recharge,
  965. 'invite': l10n.inviteFriends,
  966. 'broker': l10n.broker,
  967. 'ido': 'IDO理财',
  968. 'commodity': '大宗贵金属',
  969. 'stock': '股票',
  970. 'forex': '外汇',
  971. 'app': '应用',
  972. };
  973. final semanticsMap = {
  974. 'perpetual': 'home_btn_futures',
  975. 'copy': 'home_btn_copy_trading',
  976. 'recharge': 'home_btn_deposit',
  977. 'invite': 'home_btn_referral',
  978. 'broker': 'home_btn_broker',
  979. 'ido': 'home_btn_ido',
  980. 'commodity': 'home_btn_commodity',
  981. 'stock': 'home_btn_stock',
  982. 'forex': 'home_btn_forex',
  983. 'app': 'home_btn_app',
  984. };
  985. const developingKeys = {'commodity', 'stock', 'forex', 'app'};
  986. List<Widget> buildActionItems() {
  987. return _actionDefs.map((action) {
  988. return _QuickActionItem(
  989. icon: action.icon,
  990. label: labelMap[action.key]!,
  991. semanticsLabel: semanticsMap[action.key]!,
  992. onTap: developingKeys.contains(action.key)
  993. ? () => _showDevelopingDialog(context)
  994. : action.key == 'broker'
  995. ? () => _onBrokerTap(context, ref)
  996. : () => action.useGo
  997. ? context.go(action.route)
  998. : context.push(action.route),
  999. );
  1000. }).toList();
  1001. }
  1002. return Padding(
  1003. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 12),
  1004. child: GridView.count(
  1005. crossAxisCount: 5,
  1006. shrinkWrap: true,
  1007. physics: const NeverScrollableScrollPhysics(),
  1008. mainAxisSpacing: 12,
  1009. crossAxisSpacing: 4,
  1010. childAspectRatio: 0.84,
  1011. children: buildActionItems(),
  1012. ),
  1013. );
  1014. }
  1015. }
  1016. class _QuickActionItem extends StatelessWidget {
  1017. const _QuickActionItem({
  1018. required this.icon,
  1019. required this.label,
  1020. required this.semanticsLabel,
  1021. required this.onTap,
  1022. });
  1023. final IconData icon;
  1024. final String label;
  1025. final String semanticsLabel;
  1026. final VoidCallback onTap;
  1027. @override
  1028. Widget build(BuildContext context) {
  1029. final cs = Theme.of(context).colorScheme;
  1030. final isDark = Theme.of(context).brightness == Brightness.dark;
  1031. return Semantics(
  1032. label: semanticsLabel,
  1033. button: true,
  1034. onTap: onTap,
  1035. child: GestureDetector(
  1036. onTap: onTap,
  1037. child: SizedBox(
  1038. width: 62,
  1039. child: Column(
  1040. mainAxisSize: MainAxisSize.min,
  1041. children: [
  1042. Container(
  1043. width: 48,
  1044. height: 48,
  1045. decoration: BoxDecoration(
  1046. color: isDark
  1047. ? AppColors.darkBgTertiary
  1048. : AppColors.lightBgTertiary,
  1049. borderRadius: BorderRadius.circular(14),
  1050. ),
  1051. child: Icon(icon, color: AppColors.brand, size: 22),
  1052. ),
  1053. const SizedBox(height: 6),
  1054. Text(
  1055. label,
  1056. style: TextStyle(
  1057. color: cs.onSurface.withAlpha(153),
  1058. fontSize: 11,
  1059. ),
  1060. textAlign: TextAlign.center,
  1061. ),
  1062. ],
  1063. ),
  1064. ),
  1065. ),
  1066. );
  1067. }
  1068. }
  1069. // ── 行情区块:标题 + Tab ──────────────────────────────────
  1070. class _MarketSectionHeader extends StatelessWidget {
  1071. const _MarketSectionHeader({
  1072. required this.tabIndex,
  1073. required this.notifier,
  1074. this.showFavorites = true,
  1075. });
  1076. final int tabIndex;
  1077. final HomeNotifier notifier;
  1078. final bool showFavorites;
  1079. List<String> _getTabs(BuildContext context) {
  1080. final l10n = AppLocalizations.of(context)!;
  1081. return [l10n.hotTrading, l10n.gainers, l10n.losers, l10n.spotTab];
  1082. }
  1083. static const _tabIndices = [1, 2, 3, 4];
  1084. @override
  1085. Widget build(BuildContext context) {
  1086. final cs = Theme.of(context).colorScheme;
  1087. return Padding(
  1088. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  1089. child: Row(
  1090. children: List.generate(_tabIndices.length, (i) {
  1091. final tabs = _getTabs(context);
  1092. final actualIndex = _tabIndices[i];
  1093. final selected = actualIndex == tabIndex;
  1094. final tabSemantics = [
  1095. 'home_tab_market_hot',
  1096. 'home_tab_market_gainers',
  1097. 'home_tab_market_losers',
  1098. 'home_tab_market_spot'
  1099. ];
  1100. return Semantics(
  1101. label: tabSemantics[i],
  1102. button: true,
  1103. enabled: true,
  1104. onTap: () => notifier.setMarketTab(actualIndex),
  1105. child: GestureDetector(
  1106. onTap: () => notifier.setMarketTab(actualIndex),
  1107. child: Padding(
  1108. padding: const EdgeInsets.only(right: 20),
  1109. child: Column(
  1110. crossAxisAlignment: CrossAxisAlignment.start,
  1111. children: [
  1112. Text(
  1113. tabs[i],
  1114. style: TextStyle(
  1115. fontSize: 14,
  1116. fontWeight:
  1117. selected ? FontWeight.w600 : FontWeight.w400,
  1118. color: selected
  1119. ? cs.onSurface
  1120. : cs.onSurface.withAlpha(153),
  1121. ),
  1122. ),
  1123. const SizedBox(height: 4),
  1124. if (selected)
  1125. Container(height: 2, width: 20, color: AppColors.brand),
  1126. ],
  1127. ),
  1128. ),
  1129. ),
  1130. );
  1131. }),
  1132. ),
  1133. );
  1134. }
  1135. }
  1136. // ── 行情列表表头 ──────────────────────────────────────────
  1137. // ── 首页现货行情 Sliver(tabIndex=4)─────────────────────────
  1138. class _HomeSpotTickerSliver extends ConsumerWidget {
  1139. @override
  1140. Widget build(BuildContext context, WidgetRef ref) {
  1141. final symbols = ref.watch(
  1142. marketProvider.select((s) => s.spotDisplaySymbols.take(10).toList()),
  1143. );
  1144. final spotLoading = ref.watch(
  1145. marketProvider.select((s) => s.spotLoading),
  1146. );
  1147. // 首次进入现货 Tab 时触发加载
  1148. final loaded =
  1149. ref.watch(marketProvider.select((s) => s.spotTickers.isNotEmpty));
  1150. if (!loaded && !spotLoading) {
  1151. Future.microtask(() {
  1152. ref.read(marketProvider.notifier).setMode(MarketMode.spot);
  1153. });
  1154. }
  1155. if (spotLoading || symbols.isEmpty) {
  1156. return SliverToBoxAdapter(
  1157. child: SizedBox(
  1158. height: 80,
  1159. child: const Center(child: CircularProgressIndicator()),
  1160. ),
  1161. );
  1162. }
  1163. return SliverList.builder(
  1164. itemCount: symbols.length,
  1165. itemBuilder: (context, index) {
  1166. final sym = symbols[index];
  1167. return _HomeSpotTickerRow(symbol: sym);
  1168. },
  1169. );
  1170. }
  1171. }
  1172. class _HomeSpotTickerRow extends ConsumerWidget {
  1173. const _HomeSpotTickerRow({required this.symbol});
  1174. final String symbol;
  1175. @override
  1176. Widget build(BuildContext context, WidgetRef ref) {
  1177. final cs = Theme.of(context).colorScheme;
  1178. final ticker = ref.watch(spotTickerProvider(symbol));
  1179. if (ticker == null) return const SizedBox.shrink();
  1180. final changeColor = AppColors.changeColor(ticker.change24h);
  1181. final changeStr = formatChange(ticker.change24h);
  1182. return InkWell(
  1183. onTap: () => context.push('/market/spot/${ticker.symbol}'),
  1184. child: Padding(
  1185. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1186. child: MarketListDataRow(
  1187. leading: CoinIcon(
  1188. symbol: ticker.baseAsset,
  1189. iconUrl: ticker.icon,
  1190. size: 36,
  1191. borderRadius: 10,
  1192. ),
  1193. name: Column(
  1194. crossAxisAlignment: CrossAxisAlignment.start,
  1195. children: [
  1196. Text(
  1197. formatUsdtPairDisplay(ticker.symbol),
  1198. style: TextStyle(
  1199. color: cs.onSurface,
  1200. fontSize: 14,
  1201. fontWeight: FontWeight.w500,
  1202. ),
  1203. maxLines: 1,
  1204. overflow: TextOverflow.ellipsis,
  1205. ),
  1206. Text(
  1207. AppLocalizations.of(context)!.spot,
  1208. style: TextStyle(
  1209. color: cs.onSurface.withAlpha(153),
  1210. fontSize: 11,
  1211. ),
  1212. ),
  1213. ],
  1214. ),
  1215. price: Column(
  1216. crossAxisAlignment: CrossAxisAlignment.end,
  1217. mainAxisSize: MainAxisSize.min,
  1218. children: [
  1219. Text(
  1220. ticker.lastPrice > 0
  1221. ? (ticker.lastPriceStr != null
  1222. ? formatRawPrice(ticker.lastPriceStr!)
  1223. : formatPrice(ticker.lastPrice))
  1224. : '--',
  1225. style: TextStyle(
  1226. color: cs.onSurface,
  1227. fontSize: 14,
  1228. fontWeight: FontWeight.w500,
  1229. fontFeatures: const [FontFeature.tabularFigures()],
  1230. ),
  1231. maxLines: 1,
  1232. overflow: TextOverflow.ellipsis,
  1233. ),
  1234. Text(
  1235. ticker.lastPrice > 0 ? formatFiatPrice(ticker.lastPrice) : '--',
  1236. style: TextStyle(
  1237. color: cs.onSurface.withAlpha(153),
  1238. fontSize: 11,
  1239. fontFeatures: const [FontFeature.tabularFigures()],
  1240. ),
  1241. maxLines: 1,
  1242. overflow: TextOverflow.ellipsis,
  1243. ),
  1244. ],
  1245. ),
  1246. change: Container(
  1247. height: 34,
  1248. alignment: Alignment.center,
  1249. decoration: BoxDecoration(
  1250. color: changeColor,
  1251. borderRadius: BorderRadius.circular(6),
  1252. ),
  1253. child: Text(
  1254. changeStr,
  1255. style: const TextStyle(
  1256. color: Colors.white,
  1257. fontSize: 13,
  1258. fontWeight: FontWeight.w600,
  1259. fontFeatures: [FontFeature.tabularFigures()],
  1260. ),
  1261. textAlign: TextAlign.center,
  1262. ),
  1263. ),
  1264. ),
  1265. ),
  1266. );
  1267. }
  1268. }
  1269. class _MarketListHeader extends StatelessWidget {
  1270. const _MarketListHeader();
  1271. @override
  1272. Widget build(BuildContext context) {
  1273. final cs = Theme.of(context).colorScheme;
  1274. return Padding(
  1275. padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
  1276. child: MarketListHeaderRow(
  1277. leading: const SizedBox(width: 36),
  1278. name: Text(
  1279. AppLocalizations.of(context)!.coinNameLabel,
  1280. style: TextStyle(
  1281. color: cs.onSurface.withAlpha(153),
  1282. fontSize: 12,
  1283. ),
  1284. ),
  1285. price: Text(
  1286. AppLocalizations.of(context)!.latestPrice,
  1287. textAlign: TextAlign.end,
  1288. style: TextStyle(
  1289. color: cs.onSurface.withAlpha(153),
  1290. fontSize: 12,
  1291. ),
  1292. ),
  1293. change: Text(
  1294. AppLocalizations.of(context)!.change24h,
  1295. textAlign: TextAlign.center,
  1296. style: TextStyle(
  1297. color: cs.onSurface.withAlpha(153),
  1298. fontSize: 12,
  1299. ),
  1300. ),
  1301. ),
  1302. );
  1303. }
  1304. }
  1305. // ── 行情行 ────────────────────────────────────────────────
  1306. class _MarketTickerRow extends StatelessWidget {
  1307. const _MarketTickerRow({
  1308. required this.ticker,
  1309. required this.isFavorite,
  1310. required this.onToggleFavorite,
  1311. required this.onTap,
  1312. });
  1313. final MarketTicker ticker;
  1314. final bool isFavorite;
  1315. final VoidCallback onToggleFavorite;
  1316. final VoidCallback onTap;
  1317. @override
  1318. Widget build(BuildContext context) {
  1319. final cs = Theme.of(context).colorScheme;
  1320. final color = AppColors.changeColor(ticker.change24h);
  1321. final changeStr = formatChange(ticker.change24h);
  1322. final vol = ticker.volume24h;
  1323. final volStr = vol >= 1e9
  1324. ? '${(vol / 1e9).toStringAsFixed(2)}B'
  1325. : '${(vol / 1e6).toStringAsFixed(0)}M';
  1326. return InkWell(
  1327. onTap: onTap,
  1328. child: Padding(
  1329. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1330. child: MarketListDataRow(
  1331. leading: CoinIcon(
  1332. symbol: ticker.baseAsset,
  1333. iconUrl: ticker.icon,
  1334. size: 36,
  1335. borderRadius: 10,
  1336. ),
  1337. name: Column(
  1338. crossAxisAlignment: CrossAxisAlignment.start,
  1339. children: [
  1340. Text(
  1341. formatUsdtPairDisplay(ticker.symbol),
  1342. style: TextStyle(
  1343. color: cs.onSurface,
  1344. fontSize: 14,
  1345. fontWeight: FontWeight.w500,
  1346. ),
  1347. maxLines: 1,
  1348. overflow: TextOverflow.ellipsis,
  1349. ),
  1350. Text(
  1351. ticker.isFutures
  1352. ? '${AppLocalizations.of(context)!.turnover} $volStr'
  1353. : AppLocalizations.of(context)!.spot,
  1354. style: TextStyle(
  1355. color: cs.onSurface.withAlpha(153),
  1356. fontSize: 11,
  1357. ),
  1358. maxLines: 1,
  1359. overflow: TextOverflow.ellipsis,
  1360. ),
  1361. ],
  1362. ),
  1363. price: Column(
  1364. crossAxisAlignment: CrossAxisAlignment.end,
  1365. mainAxisSize: MainAxisSize.min,
  1366. children: [
  1367. Text(
  1368. ticker.lastPriceStr != null
  1369. ? formatRawPrice(ticker.lastPriceStr!)
  1370. : formatPrice(ticker.lastPrice,
  1371. decimalPlaces: ticker.pricePrecision),
  1372. style: TextStyle(
  1373. color: cs.onSurface,
  1374. fontSize: 14,
  1375. fontWeight: FontWeight.w500,
  1376. fontFeatures: const [FontFeature.tabularFigures()],
  1377. ),
  1378. maxLines: 1,
  1379. overflow: TextOverflow.ellipsis,
  1380. ),
  1381. Text(
  1382. formatFiatPrice(ticker.lastPrice,
  1383. pricePrecision: ticker.pricePrecision),
  1384. style: TextStyle(
  1385. color: cs.onSurface.withAlpha(153),
  1386. fontSize: 11,
  1387. fontFeatures: const [FontFeature.tabularFigures()],
  1388. ),
  1389. maxLines: 1,
  1390. overflow: TextOverflow.ellipsis,
  1391. ),
  1392. ],
  1393. ),
  1394. change: Container(
  1395. height: 34,
  1396. alignment: Alignment.center,
  1397. decoration: BoxDecoration(
  1398. color: color,
  1399. borderRadius: BorderRadius.circular(6),
  1400. ),
  1401. child: Text(
  1402. changeStr,
  1403. style: const TextStyle(
  1404. color: Colors.white,
  1405. fontSize: 13,
  1406. fontWeight: FontWeight.w600,
  1407. fontFeatures: [FontFeature.tabularFigures()],
  1408. ),
  1409. textAlign: TextAlign.center,
  1410. ),
  1411. ),
  1412. ),
  1413. ),
  1414. );
  1415. }
  1416. }