home_screen.dart 49 KB

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