home_screen.dart 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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/constants/market_list_layout.dart';
  11. import '../../../core/l10n/app_localizations.dart';
  12. import '../../../core/theme/app_colors.dart';
  13. import '../../../core/navigation/broker_navigation.dart';
  14. import '../../../core/utils/number_format.dart';
  15. import '../../../core/utils/symbol_display.dart';
  16. import '../../../providers/currency_provider.dart';
  17. import '../../../data/models/announcement/announcement.dart';
  18. import '../../../data/models/home/app_header_item.dart';
  19. import '../../../data/models/home/market_ticker.dart';
  20. import '../../../providers/announcement_popup_provider.dart';
  21. import '../../../providers/announcement_unread_provider.dart';
  22. import '../../../core/network/dio_client.dart' show versionOutdatedProvider;
  23. import '../../../providers/app_version_provider.dart';
  24. import '../../../providers/customer_service_provider.dart';
  25. import '../../../providers/asset_provider.dart';
  26. import '../../../providers/home_provider.dart';
  27. import '../../../providers/market_provider.dart'
  28. show marketProvider, spotTickerProvider, MarketMode;
  29. import '../../widgets/common/app_refresh_indicator.dart';
  30. import '../../widgets/common/coin_icon.dart';
  31. import '../../widgets/common/update_dialog.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: Row(
  325. children: [
  326. Expanded(
  327. flex: kMarketListNameClusterFlex,
  328. child: Row(
  329. children: [
  330. Container(
  331. width: 36,
  332. height: 36,
  333. decoration: BoxDecoration(
  334. color: Colors.white,
  335. borderRadius: BorderRadius.circular(10),
  336. ),
  337. ),
  338. const SizedBox(width: 10),
  339. Expanded(
  340. child: Column(
  341. crossAxisAlignment: CrossAxisAlignment.start,
  342. children: [
  343. _shimmerBox(70, 14),
  344. const SizedBox(height: 6),
  345. _shimmerBox(50, 11),
  346. ],
  347. ),
  348. ),
  349. ],
  350. ),
  351. ),
  352. SizedBox(width: kMarketListNameToPriceGap),
  353. Column(
  354. crossAxisAlignment: CrossAxisAlignment.end,
  355. children: [
  356. _shimmerBox(80, 14),
  357. const SizedBox(height: 6),
  358. _shimmerBox(50, 11),
  359. ],
  360. ),
  361. Spacer(flex: kMarketListPriceTailSpacerFlex),
  362. SizedBox(width: kMarketListPriceToBadgeGap),
  363. Container(
  364. width: kMarketListChangeBadgeWidth,
  365. height: 30,
  366. decoration: BoxDecoration(
  367. color: Colors.white,
  368. borderRadius: BorderRadius.circular(6),
  369. ),
  370. ),
  371. ],
  372. ),
  373. ),
  374. ),
  375. ],
  376. ),
  377. );
  378. }
  379. static Widget _shimmerBox(double width, double height) {
  380. return Container(
  381. width: width,
  382. height: height,
  383. decoration: BoxDecoration(
  384. color: Colors.white,
  385. borderRadius: BorderRadius.circular(4),
  386. ),
  387. );
  388. }
  389. static Widget _shimmerCircle(double size) {
  390. return Container(
  391. width: size,
  392. height: size,
  393. decoration: const BoxDecoration(
  394. color: Colors.white,
  395. shape: BoxShape.circle,
  396. ),
  397. );
  398. }
  399. }
  400. // ── 主体内容 ──────────────────────────────────────────────
  401. class _HomeBody extends ConsumerWidget {
  402. const _HomeBody({required this.state});
  403. final HomeState state;
  404. @override
  405. Widget build(BuildContext context, WidgetRef ref) {
  406. final notifier = ref.read(homeProvider.notifier);
  407. ref.watch(currencyProvider); // 法币切换时触发行情列表重建
  408. return AppRefreshIndicator(
  409. onRefresh: notifier.refresh,
  410. child: CustomScrollView(
  411. slivers: [
  412. // AppBar
  413. SliverToBoxAdapter(child: _HomeAppBar(state: state)),
  414. // 未登录:引导区域
  415. if (!state.isLoggedIn) ...[
  416. const SliverToBoxAdapter(child: _GuestHeroSection()),
  417. ],
  418. // 已登录:资产卡片 + 快捷入口
  419. if (state.isLoggedIn) ...[
  420. SliverToBoxAdapter(child: _AssetCard(state: state)),
  421. const SliverToBoxAdapter(child: _QuickActions()),
  422. // 活动专区
  423. SliverToBoxAdapter(child: ActivityCarousel(banners: state.banners)),
  424. ],
  425. // 顶级交易专家(登录/未登录共享)
  426. const SliverToBoxAdapter(child: TopTradersSection()),
  427. // 热门行情(登录/未登录共享)
  428. SliverToBoxAdapter(
  429. child: _MarketSectionHeader(
  430. tabIndex: state.marketTabIndex,
  431. notifier: notifier,
  432. showFavorites: state.isLoggedIn,
  433. ),
  434. ),
  435. const SliverToBoxAdapter(child: _MarketListHeader()),
  436. // tabIndex=4 展示现货,其余展示合约
  437. if (state.marketTabIndex == 4)
  438. _HomeSpotTickerSliver()
  439. else
  440. SliverList.builder(
  441. itemCount: state.displayTickers.length,
  442. itemBuilder: (context, index) {
  443. final ticker = state.displayTickers[index];
  444. return _MarketTickerRow(
  445. ticker: ticker,
  446. isFavorite: state.favorites.contains(ticker.symbol),
  447. onToggleFavorite: () =>
  448. notifier.toggleFavorite(ticker.symbol),
  449. onTap: () => context.push('/market/futures/${ticker.symbol}'),
  450. );
  451. },
  452. ),
  453. const SliverToBoxAdapter(child: SizedBox(height: 24)),
  454. ],
  455. ),
  456. );
  457. }
  458. }
  459. // ── AppBar ────────────────────────────────────────────────
  460. class _HomeAppBar extends ConsumerWidget {
  461. const _HomeAppBar({required this.state});
  462. final HomeState state;
  463. @override
  464. Widget build(BuildContext context, WidgetRef ref) {
  465. final cs = Theme.of(context).colorScheme;
  466. final isDark = Theme.of(context).brightness == Brightness.dark;
  467. return Padding(
  468. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  469. child: Row(
  470. children: [
  471. // 个人中心入口
  472. Semantics(
  473. label: 'home_btn_profile',
  474. button: true,
  475. enabled: state.isLoggedIn,
  476. onTap: state.isLoggedIn ? () => context.push('/user') : null,
  477. child: GestureDetector(
  478. onTap: () => context.push('/user'),
  479. child: Container(
  480. width: 32,
  481. height: 32,
  482. decoration: BoxDecoration(
  483. color: state.isLoggedIn
  484. ? (isDark
  485. ? AppColors.darkBgTertiary
  486. : AppColors.lightBgTertiary)
  487. : cs.onSurface.withAlpha(30),
  488. shape: BoxShape.circle,
  489. ),
  490. child: Icon(
  491. state.isLoggedIn ? Icons.person : Icons.person_outline,
  492. color: state.isLoggedIn
  493. ? cs.onSurface
  494. : cs.onSurface.withAlpha(153),
  495. size: 20,
  496. ),
  497. ),
  498. ),
  499. ),
  500. const SizedBox(width: 10),
  501. // 搜索栏
  502. Expanded(
  503. child: Semantics(
  504. label: 'home_search_market',
  505. onTap: () => context.go('/market'),
  506. child: GestureDetector(
  507. onTap: () => context.go('/market'),
  508. child: Container(
  509. height: 36,
  510. padding: const EdgeInsets.symmetric(horizontal: 12),
  511. decoration: BoxDecoration(
  512. color: isDark
  513. ? AppColors.darkBgTertiary
  514. : AppColors.lightBgTertiary,
  515. borderRadius: BorderRadius.circular(18),
  516. ),
  517. child: Row(
  518. children: [
  519. Icon(Icons.search,
  520. color: cs.onSurface.withAlpha(100), size: 18),
  521. const SizedBox(width: 6),
  522. Text(
  523. AppLocalizations.of(context)!.searchPair,
  524. style: TextStyle(
  525. color: cs.onSurface.withAlpha(100),
  526. fontSize: 13,
  527. ),
  528. ),
  529. ],
  530. ),
  531. ),
  532. ),
  533. ),
  534. ),
  535. const SizedBox(width: 10),
  536. // 客服入口
  537. if (AppConfig.customerServiceEnabled)
  538. Padding(
  539. padding: const EdgeInsets.only(right: 14),
  540. child: Semantics(
  541. label: 'home_btn_service',
  542. button: true,
  543. onTap: () => openCustomerService(context, ref),
  544. child: GestureDetector(
  545. onTap: () => openCustomerService(context, ref),
  546. child: Icon(Icons.headset_mic_outlined,
  547. color: cs.onSurface, size: 22),
  548. ),
  549. ),
  550. ),
  551. Semantics(
  552. label: 'home_btn_messages',
  553. button: true,
  554. onTap: () => context.push('/user/messages'),
  555. child: GestureDetector(
  556. onTap: () => context.push('/user/messages'),
  557. child: Stack(
  558. clipBehavior: Clip.none,
  559. children: [
  560. Icon(Icons.notifications_outlined,
  561. color: cs.onSurface, size: 22),
  562. if (ref.watch(announcementUnreadProvider).valueOrNull
  563. case final s? when s.isLoaded && s.hasUnread)
  564. Positioned(
  565. top: -2,
  566. right: -2,
  567. child: Container(
  568. width: 8,
  569. height: 8,
  570. decoration: const BoxDecoration(
  571. color: AppColors.fall,
  572. shape: BoxShape.circle,
  573. ),
  574. ),
  575. ),
  576. ],
  577. ),
  578. ),
  579. ),
  580. ],
  581. ),
  582. );
  583. }
  584. }
  585. // ── 未登录/已登录:主视觉引导区 ───────────────────────────
  586. class _GuestHeroSection extends ConsumerWidget {
  587. // ignore: unused_element_parameter
  588. const _GuestHeroSection({this.showLoginButton = true});
  589. final bool showLoginButton;
  590. Future<void> _onHeaderTap(BuildContext context, AppHeaderItem item) async {
  591. if (item.linkUrl.isEmpty) return;
  592. if (item.isExternal) {
  593. await launchUrl(Uri.parse(item.linkUrl),
  594. mode: LaunchMode.externalApplication);
  595. return;
  596. }
  597. final url = item.linkUrl;
  598. // 复用 ActivityCarousel 的跳转逻辑
  599. const tabRoutes = {
  600. '/',
  601. 'market',
  602. '/market',
  603. '/futures',
  604. '/copy-trading',
  605. '/asset'
  606. };
  607. final isTabRoute = tabRoutes.any((r) => url == r || url.startsWith('$r/'));
  608. if (!context.mounted) return;
  609. if (isTabRoute) {
  610. context.go(url);
  611. } else {
  612. context.push(url);
  613. }
  614. }
  615. @override
  616. Widget build(BuildContext context, WidgetRef ref) {
  617. // 仅在 appHeaders 变化时重建(不因行情 WS 刷新而重建)
  618. final appHeaders = ref.watch(homeProvider.select((s) => s.appHeaders));
  619. final children = [
  620. // Header 媒体区域
  621. _HomeHeaderMedia(
  622. appHeaders: appHeaders,
  623. onHeaderTap: (item) => _onHeaderTap(context, item),
  624. ),
  625. if (showLoginButton) ...[
  626. const SizedBox(height: 12),
  627. // 登录 / 注册 按钮
  628. SizedBox(
  629. width: double.infinity,
  630. height: 52,
  631. child: ElevatedButton(
  632. onPressed: () => context.push('/login'),
  633. style: ElevatedButton.styleFrom(
  634. backgroundColor: const Color(0xFF1E1E1E),
  635. foregroundColor: Colors.white,
  636. shape: RoundedRectangleBorder(
  637. borderRadius: BorderRadius.circular(12),
  638. ),
  639. elevation: 0,
  640. ),
  641. child: Text(
  642. AppLocalizations.of(context)!.loginRegister,
  643. style: const TextStyle(
  644. fontSize: 17,
  645. fontWeight: FontWeight.w600,
  646. letterSpacing: 1,
  647. ),
  648. ),
  649. ),
  650. ),
  651. ]
  652. ];
  653. return Padding(
  654. padding: EdgeInsets.fromLTRB(16, 8, 16, showLoginButton ? 0 : 12),
  655. child: Column(children: children),
  656. );
  657. }
  658. }
  659. // ── Header 媒体区域(统一走图片管线:本地 WebP 动图 / 远程图片)──
  660. class _HomeHeaderMedia extends StatelessWidget {
  661. const _HomeHeaderMedia({
  662. required this.appHeaders,
  663. required this.onHeaderTap,
  664. });
  665. final List<AppHeaderItem> appHeaders;
  666. final Future<void> Function(AppHeaderItem) onHeaderTap;
  667. @override
  668. Widget build(BuildContext context) {
  669. final cs = Theme.of(context).colorScheme;
  670. final brightness = Theme.of(context).brightness;
  671. Widget placeholder() => Container(
  672. color: cs.surface,
  673. child: Center(
  674. child: Icon(
  675. Icons.image_outlined,
  676. color: cs.onSurface.withAlpha(60),
  677. size: 32,
  678. ),
  679. ),
  680. );
  681. Widget errorBox() => Container(
  682. color: cs.surface,
  683. child: Center(
  684. child: Icon(
  685. Icons.broken_image_outlined,
  686. color: cs.onSurface.withAlpha(80),
  687. size: 28,
  688. ),
  689. ),
  690. );
  691. // 本地兜底:深色/浅色 PNG(与 assets/animations 资源一致)
  692. Widget localFallback() {
  693. final asset = brightness == Brightness.dark
  694. ? 'assets/animations/home_header_dart.png'
  695. : 'assets/animations/home_header_light.png';
  696. return Image.asset(
  697. asset,
  698. fit: BoxFit.cover,
  699. gaplessPlayback: true,
  700. errorBuilder: (_, __, ___) => errorBox(),
  701. );
  702. }
  703. // 接口返回时用远程图片,否则走本地兜底
  704. Widget content;
  705. VoidCallback? onTap;
  706. if (appHeaders.isNotEmpty) {
  707. final item = appHeaders.first;
  708. final url = item.resolveUrl(brightness);
  709. onTap = () => onHeaderTap(item);
  710. if (url.isNotEmpty) {
  711. content = CachedNetworkImage(
  712. imageUrl: url,
  713. fit: BoxFit.cover,
  714. placeholder: (_, __) => placeholder(),
  715. errorWidget: (_, __, ___) => localFallback(),
  716. );
  717. } else {
  718. content = localFallback();
  719. }
  720. } else {
  721. content = localFallback();
  722. }
  723. final media = ClipRRect(
  724. borderRadius: BorderRadius.circular(16),
  725. child: AspectRatio(
  726. aspectRatio: 16 / 9,
  727. child: Container(
  728. color: Colors.black,
  729. child: content,
  730. ),
  731. ),
  732. );
  733. return onTap == null ? media : GestureDetector(onTap: onTap, child: media);
  734. }
  735. }
  736. // ══════════════════════════════════════════════════════════
  737. // 以下是已登录状态下的组件
  738. // ══════════════════════════════════════════════════════════
  739. // ── 已登录:资产卡片 ───────────────────────────────────────
  740. class _AssetCard extends ConsumerWidget {
  741. const _AssetCard({required this.state});
  742. final HomeState state;
  743. @override
  744. Widget build(BuildContext context, WidgetRef ref) {
  745. final cs = Theme.of(context).colorScheme;
  746. final isDark = Theme.of(context).brightness == Brightness.dark;
  747. final assetState = ref.watch(assetProvider);
  748. final obscure = assetState.obscureBalance;
  749. final pnlColor = AppColors.changeColor(state.todayPnl);
  750. final sign = state.todayPnl >= 0 ? '+' : '';
  751. return Container(
  752. margin: const EdgeInsets.fromLTRB(16, 8, 16, 4),
  753. padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
  754. decoration: BoxDecoration(
  755. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  756. borderRadius: BorderRadius.circular(16),
  757. border: isDark
  758. ? null
  759. : Border.all(color: AppColors.lightBorder, width: 0.5),
  760. ),
  761. child: Column(
  762. crossAxisAlignment: CrossAxisAlignment.start,
  763. children: [
  764. Row(
  765. children: [
  766. Text(
  767. AppLocalizations.of(context)!.totalAssetsValue,
  768. style:
  769. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 13),
  770. ),
  771. const SizedBox(width: 6),
  772. GestureDetector(
  773. onTap: () => ref.read(assetProvider.notifier).toggleObscure(),
  774. child: Icon(
  775. obscure
  776. ? Icons.visibility_off_outlined
  777. : Icons.visibility_outlined,
  778. size: 16,
  779. color: cs.onSurface.withAlpha(153),
  780. ),
  781. ),
  782. const Spacer(),
  783. GestureDetector(
  784. onTap: () => context.push('/asset/deposit'),
  785. child: Container(
  786. padding:
  787. const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
  788. decoration: BoxDecoration(
  789. color: AppColors.brand,
  790. borderRadius: BorderRadius.circular(4),
  791. ),
  792. child: Text(
  793. AppLocalizations.of(context)!.recharge,
  794. style: const TextStyle(
  795. color: Colors.black,
  796. fontSize: 12,
  797. fontWeight: FontWeight.w500),
  798. ),
  799. ),
  800. ),
  801. ],
  802. ),
  803. const SizedBox(height: 6),
  804. Row(
  805. crossAxisAlignment: CrossAxisAlignment.end,
  806. children: [
  807. Flexible(
  808. child: Text(
  809. obscure
  810. ? '****'
  811. : formatPrice(state.totalAsset, decimalPlaces: 2),
  812. maxLines: 1,
  813. overflow: TextOverflow.ellipsis,
  814. style: TextStyle(
  815. color: cs.onSurface,
  816. fontSize: 28,
  817. fontWeight: FontWeight.w700,
  818. letterSpacing: -0.5,
  819. ),
  820. ),
  821. ),
  822. const SizedBox(width: 6),
  823. Padding(
  824. padding: const EdgeInsets.only(bottom: 4),
  825. child: Text(
  826. 'USDT',
  827. style: TextStyle(
  828. color: cs.onSurface.withAlpha(153), fontSize: 13),
  829. ),
  830. ),
  831. ],
  832. ),
  833. const SizedBox(height: 6),
  834. Row(
  835. children: [
  836. Text(
  837. AppLocalizations.of(context)!.todayPnl,
  838. style:
  839. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 12),
  840. ),
  841. const SizedBox(width: 8),
  842. Flexible(
  843. child: Text(
  844. obscure
  845. ? '****'
  846. : state.todayPnlRateAvailable
  847. ? '$sign$fiatSymbol${(state.todayPnl * fiatRate).toStringAsFixed(2)} ($sign${state.todayPnlPct.toStringAsFixed(2)}%)'
  848. : '$sign$fiatSymbol${(state.todayPnl * fiatRate).toStringAsFixed(2)} (--)',
  849. maxLines: 1,
  850. overflow: TextOverflow.ellipsis,
  851. style: TextStyle(
  852. color: obscure ? cs.onSurface.withAlpha(153) : pnlColor,
  853. fontSize: 13,
  854. fontWeight: FontWeight.w500,
  855. ),
  856. ),
  857. ),
  858. ],
  859. ),
  860. ],
  861. ),
  862. );
  863. }
  864. }
  865. // ── 快捷入口 ──────────────────────────────────────────────
  866. class _QuickActions extends ConsumerWidget {
  867. const _QuickActions();
  868. static const _actionDefs = [
  869. (
  870. icon: Icons.trending_up,
  871. key: 'perpetual',
  872. route: '/futures/BTCUSDT',
  873. useGo: true
  874. ),
  875. (
  876. icon: Icons.people_alt_outlined,
  877. key: 'copy',
  878. route: '/copy-trading',
  879. useGo: true
  880. ),
  881. (
  882. icon: Icons.account_balance_wallet_outlined,
  883. key: 'recharge',
  884. route: '/asset/deposit',
  885. useGo: false
  886. ),
  887. (
  888. icon: Icons.card_giftcard_outlined,
  889. key: 'invite',
  890. route: '/user/referral',
  891. useGo: false
  892. ),
  893. (
  894. icon: Icons.business_center_outlined,
  895. key: 'broker',
  896. route: '/broker',
  897. useGo: false
  898. ),
  899. (icon: Icons.auto_graph, key: 'ido', route: '/finance/ido', useGo: false),
  900. (
  901. icon: Icons.bar_chart_rounded,
  902. key: 'commodity',
  903. route: '',
  904. useGo: false
  905. ),
  906. (
  907. icon: Icons.candlestick_chart,
  908. key: 'stock',
  909. route: '',
  910. useGo: false
  911. ),
  912. (icon: Icons.currency_exchange, key: 'forex', route: '', useGo: false),
  913. (icon: Icons.apps_outlined, key: 'app', route: '', useGo: false),
  914. ];
  915. Future<void> _onBrokerTap(BuildContext context, WidgetRef ref) async {
  916. await openBrokerEntry(context, ref);
  917. }
  918. static void _showDevelopingDialog(BuildContext context) {
  919. showDialog<void>(
  920. context: context,
  921. barrierDismissible: true,
  922. builder: (dialogContext) => Dialog(
  923. backgroundColor: Colors.white,
  924. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
  925. insetPadding: const EdgeInsets.symmetric(horizontal: 48, vertical: 48),
  926. child: Padding(
  927. padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 28),
  928. child: Column(
  929. mainAxisSize: MainAxisSize.min,
  930. children: [
  931. const Text(
  932. '敬请期待',
  933. style: TextStyle(
  934. color: Colors.black,
  935. fontSize: 16,
  936. fontWeight: FontWeight.w600,
  937. ),
  938. ),
  939. const SizedBox(height: 8),
  940. const Text(
  941. '该功能正在开发中',
  942. style: TextStyle(
  943. color: Color(0xFF666666),
  944. fontSize: 13,
  945. ),
  946. ),
  947. const SizedBox(height: 20),
  948. SizedBox(
  949. width: double.infinity,
  950. child: TextButton(
  951. onPressed: () => Navigator.of(dialogContext).pop(),
  952. style: TextButton.styleFrom(
  953. backgroundColor: Colors.black,
  954. foregroundColor: Colors.white,
  955. shape: RoundedRectangleBorder(
  956. borderRadius: BorderRadius.circular(8),
  957. ),
  958. padding: const EdgeInsets.symmetric(vertical: 12),
  959. ),
  960. child: const Text('确定'),
  961. ),
  962. ),
  963. ],
  964. ),
  965. ),
  966. ),
  967. );
  968. }
  969. @override
  970. Widget build(BuildContext context, WidgetRef ref) {
  971. final l10n = AppLocalizations.of(context)!;
  972. final labelMap = {
  973. 'perpetual': l10n.perpetualFutures,
  974. 'copy': l10n.copyTrading,
  975. 'recharge': l10n.recharge,
  976. 'invite': l10n.inviteFriends,
  977. 'broker': l10n.broker,
  978. 'ido': 'IDO理财',
  979. 'commodity': '大宗贵金属',
  980. 'stock': '股票',
  981. 'forex': '外汇',
  982. 'app': '应用',
  983. };
  984. final semanticsMap = {
  985. 'perpetual': 'home_btn_futures',
  986. 'copy': 'home_btn_copy_trading',
  987. 'recharge': 'home_btn_deposit',
  988. 'invite': 'home_btn_referral',
  989. 'broker': 'home_btn_broker',
  990. 'ido': 'home_btn_ido',
  991. 'commodity': 'home_btn_commodity',
  992. 'stock': 'home_btn_stock',
  993. 'forex': 'home_btn_forex',
  994. 'app': 'home_btn_app',
  995. };
  996. const developingKeys = {'commodity', 'stock', 'forex', 'app'};
  997. List<Widget> buildActionItems() {
  998. return _actionDefs.map((action) {
  999. return _QuickActionItem(
  1000. icon: action.icon,
  1001. label: labelMap[action.key]!,
  1002. semanticsLabel: semanticsMap[action.key]!,
  1003. onTap: developingKeys.contains(action.key)
  1004. ? () => _showDevelopingDialog(context)
  1005. : action.key == 'broker'
  1006. ? () => _onBrokerTap(context, ref)
  1007. : () => action.useGo
  1008. ? context.go(action.route)
  1009. : context.push(action.route),
  1010. );
  1011. }).toList();
  1012. }
  1013. return Padding(
  1014. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 12),
  1015. child: GridView.count(
  1016. crossAxisCount: 5,
  1017. shrinkWrap: true,
  1018. physics: const NeverScrollableScrollPhysics(),
  1019. mainAxisSpacing: 12,
  1020. crossAxisSpacing: 4,
  1021. childAspectRatio: 0.84,
  1022. children: buildActionItems(),
  1023. ),
  1024. );
  1025. }
  1026. }
  1027. class _QuickActionItem extends StatelessWidget {
  1028. const _QuickActionItem({
  1029. required this.icon,
  1030. required this.label,
  1031. required this.semanticsLabel,
  1032. required this.onTap,
  1033. });
  1034. final IconData icon;
  1035. final String label;
  1036. final String semanticsLabel;
  1037. final VoidCallback onTap;
  1038. @override
  1039. Widget build(BuildContext context) {
  1040. final cs = Theme.of(context).colorScheme;
  1041. final isDark = Theme.of(context).brightness == Brightness.dark;
  1042. return Semantics(
  1043. label: semanticsLabel,
  1044. button: true,
  1045. onTap: onTap,
  1046. child: GestureDetector(
  1047. onTap: onTap,
  1048. child: SizedBox(
  1049. width: 62,
  1050. child: Column(
  1051. mainAxisSize: MainAxisSize.min,
  1052. children: [
  1053. Container(
  1054. width: 48,
  1055. height: 48,
  1056. decoration: BoxDecoration(
  1057. color: isDark
  1058. ? AppColors.darkBgTertiary
  1059. : AppColors.lightBgTertiary,
  1060. borderRadius: BorderRadius.circular(14),
  1061. ),
  1062. child: Icon(icon, color: AppColors.brand, size: 22),
  1063. ),
  1064. const SizedBox(height: 6),
  1065. Text(
  1066. label,
  1067. style: TextStyle(
  1068. color: cs.onSurface.withAlpha(153),
  1069. fontSize: 11,
  1070. ),
  1071. textAlign: TextAlign.center,
  1072. ),
  1073. ],
  1074. ),
  1075. ),
  1076. ),
  1077. );
  1078. }
  1079. }
  1080. // ── 行情区块:标题 + Tab ──────────────────────────────────
  1081. class _MarketSectionHeader extends StatelessWidget {
  1082. const _MarketSectionHeader({
  1083. required this.tabIndex,
  1084. required this.notifier,
  1085. this.showFavorites = true,
  1086. });
  1087. final int tabIndex;
  1088. final HomeNotifier notifier;
  1089. final bool showFavorites;
  1090. List<String> _getTabs(BuildContext context) {
  1091. final l10n = AppLocalizations.of(context)!;
  1092. return [l10n.hotTrading, l10n.gainers, l10n.losers, l10n.spotTab];
  1093. }
  1094. static const _tabIndices = [1, 2, 3, 4];
  1095. @override
  1096. Widget build(BuildContext context) {
  1097. final cs = Theme.of(context).colorScheme;
  1098. return Padding(
  1099. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  1100. child: Row(
  1101. children: List.generate(_tabIndices.length, (i) {
  1102. final tabs = _getTabs(context);
  1103. final actualIndex = _tabIndices[i];
  1104. final selected = actualIndex == tabIndex;
  1105. final tabSemantics = [
  1106. 'home_tab_market_hot',
  1107. 'home_tab_market_gainers',
  1108. 'home_tab_market_losers',
  1109. 'home_tab_market_spot'
  1110. ];
  1111. return Semantics(
  1112. label: tabSemantics[i],
  1113. button: true,
  1114. enabled: true,
  1115. onTap: () => notifier.setMarketTab(actualIndex),
  1116. child: GestureDetector(
  1117. onTap: () => notifier.setMarketTab(actualIndex),
  1118. child: Padding(
  1119. padding: const EdgeInsets.only(right: 20),
  1120. child: Column(
  1121. crossAxisAlignment: CrossAxisAlignment.start,
  1122. children: [
  1123. Text(
  1124. tabs[i],
  1125. style: TextStyle(
  1126. fontSize: 14,
  1127. fontWeight:
  1128. selected ? FontWeight.w600 : FontWeight.w400,
  1129. color: selected
  1130. ? cs.onSurface
  1131. : cs.onSurface.withAlpha(153),
  1132. ),
  1133. ),
  1134. const SizedBox(height: 4),
  1135. if (selected)
  1136. Container(height: 2, width: 20, color: AppColors.brand),
  1137. ],
  1138. ),
  1139. ),
  1140. ),
  1141. );
  1142. }),
  1143. ),
  1144. );
  1145. }
  1146. }
  1147. // ── 行情列表表头 ──────────────────────────────────────────
  1148. // ── 首页现货行情 Sliver(tabIndex=4)─────────────────────────
  1149. class _HomeSpotTickerSliver extends ConsumerWidget {
  1150. @override
  1151. Widget build(BuildContext context, WidgetRef ref) {
  1152. final symbols = ref.watch(
  1153. marketProvider.select((s) => s.spotDisplaySymbols.take(10).toList()),
  1154. );
  1155. final spotLoading = ref.watch(
  1156. marketProvider.select((s) => s.spotLoading),
  1157. );
  1158. // 首次进入现货 Tab 时触发加载
  1159. final loaded =
  1160. ref.watch(marketProvider.select((s) => s.spotTickers.isNotEmpty));
  1161. if (!loaded && !spotLoading) {
  1162. Future.microtask(() {
  1163. ref.read(marketProvider.notifier).setMode(MarketMode.spot);
  1164. });
  1165. }
  1166. if (spotLoading || symbols.isEmpty) {
  1167. return SliverToBoxAdapter(
  1168. child: SizedBox(
  1169. height: 80,
  1170. child: const Center(child: CircularProgressIndicator()),
  1171. ),
  1172. );
  1173. }
  1174. return SliverList.builder(
  1175. itemCount: symbols.length,
  1176. itemBuilder: (context, index) {
  1177. final sym = symbols[index];
  1178. return _HomeSpotTickerRow(symbol: sym);
  1179. },
  1180. );
  1181. }
  1182. }
  1183. class _HomeSpotTickerRow extends ConsumerWidget {
  1184. const _HomeSpotTickerRow({required this.symbol});
  1185. final String symbol;
  1186. @override
  1187. Widget build(BuildContext context, WidgetRef ref) {
  1188. final cs = Theme.of(context).colorScheme;
  1189. final ticker = ref.watch(spotTickerProvider(symbol));
  1190. if (ticker == null) return const SizedBox.shrink();
  1191. final changeColor = AppColors.changeColor(ticker.change24h);
  1192. final changeStr = formatChange(ticker.change24h);
  1193. return InkWell(
  1194. onTap: () => context.push('/market/spot/${ticker.symbol}'),
  1195. child: Padding(
  1196. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1197. child: Row(
  1198. children: [
  1199. Expanded(
  1200. flex: kMarketListNameClusterFlex,
  1201. child: Row(
  1202. children: [
  1203. CoinIcon(
  1204. symbol: ticker.baseAsset,
  1205. iconUrl: ticker.icon,
  1206. size: 36,
  1207. borderRadius: 10,
  1208. ),
  1209. const SizedBox(width: 10),
  1210. Expanded(
  1211. child: Column(
  1212. crossAxisAlignment: CrossAxisAlignment.start,
  1213. children: [
  1214. Text(
  1215. formatUsdtPairDisplay(ticker.symbol),
  1216. style: TextStyle(
  1217. color: cs.onSurface,
  1218. fontSize: 14,
  1219. fontWeight: FontWeight.w500,
  1220. ),
  1221. maxLines: 1,
  1222. overflow: TextOverflow.ellipsis,
  1223. ),
  1224. Text(
  1225. AppLocalizations.of(context)!.spot,
  1226. style: TextStyle(
  1227. color: cs.onSurface.withAlpha(153),
  1228. fontSize: 11,
  1229. ),
  1230. ),
  1231. ],
  1232. ),
  1233. ),
  1234. ],
  1235. ),
  1236. ),
  1237. SizedBox(width: kMarketListNameToPriceGap),
  1238. Column(
  1239. crossAxisAlignment: CrossAxisAlignment.end,
  1240. mainAxisSize: MainAxisSize.min,
  1241. children: [
  1242. Text(
  1243. ticker.lastPrice > 0
  1244. ? (ticker.lastPriceStr != null
  1245. ? formatRawPrice(ticker.lastPriceStr!)
  1246. : formatPrice(ticker.lastPrice))
  1247. : '--',
  1248. style: TextStyle(
  1249. color: cs.onSurface,
  1250. fontSize: 14,
  1251. fontWeight: FontWeight.w500,
  1252. fontFeatures: const [FontFeature.tabularFigures()],
  1253. ),
  1254. textAlign: TextAlign.end,
  1255. maxLines: 1,
  1256. overflow: TextOverflow.ellipsis,
  1257. ),
  1258. Text(
  1259. ticker.lastPrice > 0
  1260. ? formatFiatPrice(ticker.lastPrice)
  1261. : '--',
  1262. style: TextStyle(
  1263. color: cs.onSurface.withAlpha(153),
  1264. fontSize: 11,
  1265. fontFeatures: const [FontFeature.tabularFigures()],
  1266. ),
  1267. textAlign: TextAlign.end,
  1268. maxLines: 1,
  1269. overflow: TextOverflow.ellipsis,
  1270. ),
  1271. ],
  1272. ),
  1273. Spacer(flex: kMarketListPriceTailSpacerFlex),
  1274. SizedBox(width: kMarketListPriceToBadgeGap),
  1275. SizedBox(
  1276. width: kMarketListChangeBadgeWidth,
  1277. child: Container(
  1278. height: 34,
  1279. alignment: Alignment.center,
  1280. decoration: BoxDecoration(
  1281. color: changeColor,
  1282. borderRadius: BorderRadius.circular(6),
  1283. ),
  1284. child: Text(
  1285. changeStr,
  1286. style: const TextStyle(
  1287. color: Colors.white,
  1288. fontSize: 13,
  1289. fontWeight: FontWeight.w600,
  1290. fontFeatures: [FontFeature.tabularFigures()],
  1291. ),
  1292. textAlign: TextAlign.center,
  1293. ),
  1294. ),
  1295. ),
  1296. ],
  1297. ),
  1298. ),
  1299. );
  1300. }
  1301. }
  1302. class _MarketListHeader extends StatelessWidget {
  1303. const _MarketListHeader();
  1304. @override
  1305. Widget build(BuildContext context) {
  1306. final cs = Theme.of(context).colorScheme;
  1307. return Padding(
  1308. padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
  1309. child: Row(
  1310. children: [
  1311. const SizedBox(width: 46),
  1312. Expanded(
  1313. flex: kMarketListNameClusterFlex,
  1314. child: Text(
  1315. AppLocalizations.of(context)!.coinNameLabel,
  1316. style: TextStyle(
  1317. color: cs.onSurface.withAlpha(153),
  1318. fontSize: 12,
  1319. ),
  1320. ),
  1321. ),
  1322. SizedBox(width: kMarketListNameToPriceGap),
  1323. Text(
  1324. AppLocalizations.of(context)!.latestPrice,
  1325. textAlign: TextAlign.end,
  1326. style: TextStyle(
  1327. color: cs.onSurface.withAlpha(153),
  1328. fontSize: 12,
  1329. ),
  1330. ),
  1331. Spacer(flex: kMarketListPriceTailSpacerFlex),
  1332. SizedBox(width: kMarketListPriceToBadgeGap),
  1333. SizedBox(
  1334. width: kMarketListChangeBadgeWidth,
  1335. child: Text(
  1336. AppLocalizations.of(context)!.change24h,
  1337. textAlign: TextAlign.center,
  1338. style: TextStyle(
  1339. color: cs.onSurface.withAlpha(153),
  1340. fontSize: 12,
  1341. ),
  1342. ),
  1343. ),
  1344. ],
  1345. ),
  1346. );
  1347. }
  1348. }
  1349. // ── 行情行 ────────────────────────────────────────────────
  1350. class _MarketTickerRow extends StatelessWidget {
  1351. const _MarketTickerRow({
  1352. required this.ticker,
  1353. required this.isFavorite,
  1354. required this.onToggleFavorite,
  1355. required this.onTap,
  1356. });
  1357. final MarketTicker ticker;
  1358. final bool isFavorite;
  1359. final VoidCallback onToggleFavorite;
  1360. final VoidCallback onTap;
  1361. @override
  1362. Widget build(BuildContext context) {
  1363. final cs = Theme.of(context).colorScheme;
  1364. final color = AppColors.changeColor(ticker.change24h);
  1365. final changeStr = formatChange(ticker.change24h);
  1366. final vol = ticker.volume24h;
  1367. final volStr = vol >= 1e9
  1368. ? '${(vol / 1e9).toStringAsFixed(2)}B'
  1369. : '${(vol / 1e6).toStringAsFixed(0)}M';
  1370. return InkWell(
  1371. onTap: onTap,
  1372. child: Padding(
  1373. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1374. child: Row(
  1375. children: [
  1376. Expanded(
  1377. flex: kMarketListNameClusterFlex,
  1378. child: Row(
  1379. children: [
  1380. CoinIcon(
  1381. symbol: ticker.baseAsset,
  1382. iconUrl: ticker.icon,
  1383. size: 36,
  1384. borderRadius: 10),
  1385. const SizedBox(width: 10),
  1386. Expanded(
  1387. child: Column(
  1388. crossAxisAlignment: CrossAxisAlignment.start,
  1389. children: [
  1390. Text(
  1391. formatUsdtPairDisplay(ticker.symbol),
  1392. style: TextStyle(
  1393. color: cs.onSurface,
  1394. fontSize: 14,
  1395. fontWeight: FontWeight.w500,
  1396. ),
  1397. maxLines: 1,
  1398. overflow: TextOverflow.ellipsis,
  1399. ),
  1400. Text(
  1401. ticker.isFutures
  1402. ? '${AppLocalizations.of(context)!.turnover} $volStr'
  1403. : AppLocalizations.of(context)!.spot,
  1404. style: TextStyle(
  1405. color: cs.onSurface.withAlpha(153),
  1406. fontSize: 11,
  1407. ),
  1408. maxLines: 1,
  1409. overflow: TextOverflow.ellipsis,
  1410. ),
  1411. ],
  1412. ),
  1413. ),
  1414. ],
  1415. ),
  1416. ),
  1417. SizedBox(width: kMarketListNameToPriceGap),
  1418. Column(
  1419. crossAxisAlignment: CrossAxisAlignment.end,
  1420. mainAxisSize: MainAxisSize.min,
  1421. children: [
  1422. Text(
  1423. ticker.lastPriceStr != null
  1424. ? formatRawPrice(ticker.lastPriceStr!)
  1425. : formatPrice(ticker.lastPrice,
  1426. decimalPlaces: ticker.pricePrecision),
  1427. style: TextStyle(
  1428. color: cs.onSurface,
  1429. fontSize: 14,
  1430. fontWeight: FontWeight.w500,
  1431. fontFeatures: const [FontFeature.tabularFigures()],
  1432. ),
  1433. textAlign: TextAlign.end,
  1434. maxLines: 1,
  1435. overflow: TextOverflow.ellipsis,
  1436. ),
  1437. Text(
  1438. formatFiatPrice(ticker.lastPrice,
  1439. pricePrecision: ticker.pricePrecision),
  1440. style: TextStyle(
  1441. color: cs.onSurface.withAlpha(153),
  1442. fontSize: 11,
  1443. fontFeatures: const [FontFeature.tabularFigures()],
  1444. ),
  1445. textAlign: TextAlign.end,
  1446. maxLines: 1,
  1447. overflow: TextOverflow.ellipsis,
  1448. ),
  1449. ],
  1450. ),
  1451. Spacer(flex: kMarketListPriceTailSpacerFlex),
  1452. SizedBox(width: kMarketListPriceToBadgeGap),
  1453. SizedBox(
  1454. width: kMarketListChangeBadgeWidth,
  1455. child: Container(
  1456. height: 34,
  1457. alignment: Alignment.center,
  1458. decoration: BoxDecoration(
  1459. color: color,
  1460. borderRadius: BorderRadius.circular(6),
  1461. ),
  1462. child: Text(
  1463. changeStr,
  1464. style: const TextStyle(
  1465. color: Colors.white,
  1466. fontSize: 13,
  1467. fontWeight: FontWeight.w600,
  1468. fontFeatures: [FontFeature.tabularFigures()],
  1469. ),
  1470. textAlign: TextAlign.center,
  1471. ),
  1472. ),
  1473. ),
  1474. ],
  1475. ),
  1476. ),
  1477. );
  1478. }
  1479. }