home_screen.dart 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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'};
  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 == 'app'
  1006. ? () => context.push('/app')
  1007. : action.key == 'broker'
  1008. ? () => _onBrokerTap(context, ref)
  1009. : () => action.useGo
  1010. ? context.go(action.route)
  1011. : context.push(action.route),
  1012. );
  1013. }).toList();
  1014. }
  1015. return Padding(
  1016. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 12),
  1017. child: GridView.count(
  1018. crossAxisCount: 5,
  1019. shrinkWrap: true,
  1020. physics: const NeverScrollableScrollPhysics(),
  1021. mainAxisSpacing: 12,
  1022. crossAxisSpacing: 4,
  1023. childAspectRatio: 0.84,
  1024. children: buildActionItems(),
  1025. ),
  1026. );
  1027. }
  1028. }
  1029. class _QuickActionItem extends StatelessWidget {
  1030. const _QuickActionItem({
  1031. required this.icon,
  1032. required this.label,
  1033. required this.semanticsLabel,
  1034. required this.onTap,
  1035. });
  1036. final IconData icon;
  1037. final String label;
  1038. final String semanticsLabel;
  1039. final VoidCallback onTap;
  1040. @override
  1041. Widget build(BuildContext context) {
  1042. final cs = Theme.of(context).colorScheme;
  1043. final isDark = Theme.of(context).brightness == Brightness.dark;
  1044. return Semantics(
  1045. label: semanticsLabel,
  1046. button: true,
  1047. onTap: onTap,
  1048. child: GestureDetector(
  1049. onTap: onTap,
  1050. child: SizedBox(
  1051. width: 62,
  1052. child: Column(
  1053. mainAxisSize: MainAxisSize.min,
  1054. children: [
  1055. Container(
  1056. width: 48,
  1057. height: 48,
  1058. decoration: BoxDecoration(
  1059. color: isDark
  1060. ? AppColors.darkBgTertiary
  1061. : AppColors.lightBgTertiary,
  1062. borderRadius: BorderRadius.circular(14),
  1063. ),
  1064. child: Icon(icon, color: AppColors.brand, size: 22),
  1065. ),
  1066. const SizedBox(height: 6),
  1067. Text(
  1068. label == "应用" ? '$label >' : label,
  1069. style: TextStyle(
  1070. color: cs.onSurface.withAlpha(153),
  1071. fontSize: 11,
  1072. ),
  1073. textAlign: TextAlign.center,
  1074. ),
  1075. ],
  1076. ),
  1077. ),
  1078. ),
  1079. );
  1080. }
  1081. }
  1082. // ── 行情区块:标题 + Tab ──────────────────────────────────
  1083. class _MarketSectionHeader extends StatelessWidget {
  1084. const _MarketSectionHeader({
  1085. required this.tabIndex,
  1086. required this.notifier,
  1087. this.showFavorites = true,
  1088. });
  1089. final int tabIndex;
  1090. final HomeNotifier notifier;
  1091. final bool showFavorites;
  1092. List<String> _getTabs(BuildContext context) {
  1093. final l10n = AppLocalizations.of(context)!;
  1094. return [l10n.hotTrading, l10n.gainers, l10n.losers, l10n.spotTab];
  1095. }
  1096. static const _tabIndices = [1, 2, 3, 4];
  1097. @override
  1098. Widget build(BuildContext context) {
  1099. final cs = Theme.of(context).colorScheme;
  1100. return Padding(
  1101. padding: const EdgeInsets.fromLTRB(16, 20, 16, 0),
  1102. child: Row(
  1103. children: List.generate(_tabIndices.length, (i) {
  1104. final tabs = _getTabs(context);
  1105. final actualIndex = _tabIndices[i];
  1106. final selected = actualIndex == tabIndex;
  1107. final tabSemantics = [
  1108. 'home_tab_market_hot',
  1109. 'home_tab_market_gainers',
  1110. 'home_tab_market_losers',
  1111. 'home_tab_market_spot'
  1112. ];
  1113. return Semantics(
  1114. label: tabSemantics[i],
  1115. button: true,
  1116. enabled: true,
  1117. onTap: () => notifier.setMarketTab(actualIndex),
  1118. child: GestureDetector(
  1119. onTap: () => notifier.setMarketTab(actualIndex),
  1120. child: Padding(
  1121. padding: const EdgeInsets.only(right: 20),
  1122. child: Column(
  1123. crossAxisAlignment: CrossAxisAlignment.start,
  1124. children: [
  1125. Text(
  1126. tabs[i],
  1127. style: TextStyle(
  1128. fontSize: 14,
  1129. fontWeight:
  1130. selected ? FontWeight.w600 : FontWeight.w400,
  1131. color: selected
  1132. ? cs.onSurface
  1133. : cs.onSurface.withAlpha(153),
  1134. ),
  1135. ),
  1136. const SizedBox(height: 4),
  1137. if (selected)
  1138. Container(height: 2, width: 20, color: AppColors.brand),
  1139. ],
  1140. ),
  1141. ),
  1142. ),
  1143. );
  1144. }),
  1145. ),
  1146. );
  1147. }
  1148. }
  1149. // ── 行情列表表头 ──────────────────────────────────────────
  1150. // ── 首页现货行情 Sliver(tabIndex=4)─────────────────────────
  1151. class _HomeSpotTickerSliver extends ConsumerWidget {
  1152. @override
  1153. Widget build(BuildContext context, WidgetRef ref) {
  1154. final symbols = ref.watch(
  1155. marketProvider.select((s) => s.spotDisplaySymbols.take(10).toList()),
  1156. );
  1157. final spotLoading = ref.watch(
  1158. marketProvider.select((s) => s.spotLoading),
  1159. );
  1160. // 首次进入现货 Tab 时触发加载
  1161. final loaded =
  1162. ref.watch(marketProvider.select((s) => s.spotTickers.isNotEmpty));
  1163. if (!loaded && !spotLoading) {
  1164. Future.microtask(() {
  1165. ref.read(marketProvider.notifier).setMode(MarketMode.spot);
  1166. });
  1167. }
  1168. if (spotLoading || symbols.isEmpty) {
  1169. return SliverToBoxAdapter(
  1170. child: SizedBox(
  1171. height: 80,
  1172. child: const Center(child: CircularProgressIndicator()),
  1173. ),
  1174. );
  1175. }
  1176. return SliverList.builder(
  1177. itemCount: symbols.length,
  1178. itemBuilder: (context, index) {
  1179. final sym = symbols[index];
  1180. return _HomeSpotTickerRow(symbol: sym);
  1181. },
  1182. );
  1183. }
  1184. }
  1185. class _HomeSpotTickerRow extends ConsumerWidget {
  1186. const _HomeSpotTickerRow({required this.symbol});
  1187. final String symbol;
  1188. @override
  1189. Widget build(BuildContext context, WidgetRef ref) {
  1190. final cs = Theme.of(context).colorScheme;
  1191. final ticker = ref.watch(spotTickerProvider(symbol));
  1192. if (ticker == null) return const SizedBox.shrink();
  1193. final changeColor = AppColors.changeColor(ticker.change24h);
  1194. final changeStr = formatChange(ticker.change24h);
  1195. return InkWell(
  1196. onTap: () => context.push('/market/spot/${ticker.symbol}'),
  1197. child: Padding(
  1198. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1199. child: Row(
  1200. children: [
  1201. Expanded(
  1202. flex: kMarketListNameClusterFlex,
  1203. child: Row(
  1204. children: [
  1205. CoinIcon(
  1206. symbol: ticker.baseAsset,
  1207. iconUrl: ticker.icon,
  1208. size: 36,
  1209. borderRadius: 10,
  1210. ),
  1211. const SizedBox(width: 10),
  1212. Expanded(
  1213. child: Column(
  1214. crossAxisAlignment: CrossAxisAlignment.start,
  1215. children: [
  1216. Text(
  1217. formatUsdtPairDisplay(ticker.symbol),
  1218. style: TextStyle(
  1219. color: cs.onSurface,
  1220. fontSize: 14,
  1221. fontWeight: FontWeight.w500,
  1222. ),
  1223. maxLines: 1,
  1224. overflow: TextOverflow.ellipsis,
  1225. ),
  1226. Text(
  1227. AppLocalizations.of(context)!.spot,
  1228. style: TextStyle(
  1229. color: cs.onSurface.withAlpha(153),
  1230. fontSize: 11,
  1231. ),
  1232. ),
  1233. ],
  1234. ),
  1235. ),
  1236. ],
  1237. ),
  1238. ),
  1239. SizedBox(width: kMarketListNameToPriceGap),
  1240. Column(
  1241. crossAxisAlignment: CrossAxisAlignment.end,
  1242. mainAxisSize: MainAxisSize.min,
  1243. children: [
  1244. Text(
  1245. ticker.lastPrice > 0
  1246. ? (ticker.lastPriceStr != null
  1247. ? formatRawPrice(ticker.lastPriceStr!)
  1248. : formatPrice(ticker.lastPrice))
  1249. : '--',
  1250. style: TextStyle(
  1251. color: cs.onSurface,
  1252. fontSize: 14,
  1253. fontWeight: FontWeight.w500,
  1254. fontFeatures: const [FontFeature.tabularFigures()],
  1255. ),
  1256. textAlign: TextAlign.end,
  1257. maxLines: 1,
  1258. overflow: TextOverflow.ellipsis,
  1259. ),
  1260. Text(
  1261. ticker.lastPrice > 0
  1262. ? formatFiatPrice(ticker.lastPrice)
  1263. : '--',
  1264. style: TextStyle(
  1265. color: cs.onSurface.withAlpha(153),
  1266. fontSize: 11,
  1267. fontFeatures: const [FontFeature.tabularFigures()],
  1268. ),
  1269. textAlign: TextAlign.end,
  1270. maxLines: 1,
  1271. overflow: TextOverflow.ellipsis,
  1272. ),
  1273. ],
  1274. ),
  1275. Spacer(flex: kMarketListPriceTailSpacerFlex),
  1276. SizedBox(width: kMarketListPriceToBadgeGap),
  1277. SizedBox(
  1278. width: kMarketListChangeBadgeWidth,
  1279. child: Container(
  1280. height: 34,
  1281. alignment: Alignment.center,
  1282. decoration: BoxDecoration(
  1283. color: changeColor,
  1284. borderRadius: BorderRadius.circular(6),
  1285. ),
  1286. child: Text(
  1287. changeStr,
  1288. style: const TextStyle(
  1289. color: Colors.white,
  1290. fontSize: 13,
  1291. fontWeight: FontWeight.w600,
  1292. fontFeatures: [FontFeature.tabularFigures()],
  1293. ),
  1294. textAlign: TextAlign.center,
  1295. ),
  1296. ),
  1297. ),
  1298. ],
  1299. ),
  1300. ),
  1301. );
  1302. }
  1303. }
  1304. class _MarketListHeader extends StatelessWidget {
  1305. const _MarketListHeader();
  1306. @override
  1307. Widget build(BuildContext context) {
  1308. final cs = Theme.of(context).colorScheme;
  1309. return Padding(
  1310. padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
  1311. child: Row(
  1312. children: [
  1313. const SizedBox(width: 46),
  1314. Expanded(
  1315. flex: kMarketListNameClusterFlex,
  1316. child: Text(
  1317. AppLocalizations.of(context)!.coinNameLabel,
  1318. style: TextStyle(
  1319. color: cs.onSurface.withAlpha(153),
  1320. fontSize: 12,
  1321. ),
  1322. ),
  1323. ),
  1324. SizedBox(width: kMarketListNameToPriceGap),
  1325. Text(
  1326. AppLocalizations.of(context)!.latestPrice,
  1327. textAlign: TextAlign.end,
  1328. style: TextStyle(
  1329. color: cs.onSurface.withAlpha(153),
  1330. fontSize: 12,
  1331. ),
  1332. ),
  1333. Spacer(flex: kMarketListPriceTailSpacerFlex),
  1334. SizedBox(width: kMarketListPriceToBadgeGap),
  1335. SizedBox(
  1336. width: kMarketListChangeBadgeWidth,
  1337. child: Text(
  1338. AppLocalizations.of(context)!.change24h,
  1339. textAlign: TextAlign.center,
  1340. style: TextStyle(
  1341. color: cs.onSurface.withAlpha(153),
  1342. fontSize: 12,
  1343. ),
  1344. ),
  1345. ),
  1346. ],
  1347. ),
  1348. );
  1349. }
  1350. }
  1351. // ── 行情行 ────────────────────────────────────────────────
  1352. class _MarketTickerRow extends StatelessWidget {
  1353. const _MarketTickerRow({
  1354. required this.ticker,
  1355. required this.isFavorite,
  1356. required this.onToggleFavorite,
  1357. required this.onTap,
  1358. });
  1359. final MarketTicker ticker;
  1360. final bool isFavorite;
  1361. final VoidCallback onToggleFavorite;
  1362. final VoidCallback onTap;
  1363. @override
  1364. Widget build(BuildContext context) {
  1365. final cs = Theme.of(context).colorScheme;
  1366. final color = AppColors.changeColor(ticker.change24h);
  1367. final changeStr = formatChange(ticker.change24h);
  1368. final vol = ticker.volume24h;
  1369. final volStr = vol >= 1e9
  1370. ? '${(vol / 1e9).toStringAsFixed(2)}B'
  1371. : '${(vol / 1e6).toStringAsFixed(0)}M';
  1372. return InkWell(
  1373. onTap: onTap,
  1374. child: Padding(
  1375. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  1376. child: Row(
  1377. children: [
  1378. Expanded(
  1379. flex: kMarketListNameClusterFlex,
  1380. child: Row(
  1381. children: [
  1382. CoinIcon(
  1383. symbol: ticker.baseAsset,
  1384. iconUrl: ticker.icon,
  1385. size: 36,
  1386. borderRadius: 10),
  1387. const SizedBox(width: 10),
  1388. Expanded(
  1389. child: Column(
  1390. crossAxisAlignment: CrossAxisAlignment.start,
  1391. children: [
  1392. Text(
  1393. formatUsdtPairDisplay(ticker.symbol),
  1394. style: TextStyle(
  1395. color: cs.onSurface,
  1396. fontSize: 14,
  1397. fontWeight: FontWeight.w500,
  1398. ),
  1399. maxLines: 1,
  1400. overflow: TextOverflow.ellipsis,
  1401. ),
  1402. Text(
  1403. ticker.isFutures
  1404. ? '${AppLocalizations.of(context)!.turnover} $volStr'
  1405. : AppLocalizations.of(context)!.spot,
  1406. style: TextStyle(
  1407. color: cs.onSurface.withAlpha(153),
  1408. fontSize: 11,
  1409. ),
  1410. maxLines: 1,
  1411. overflow: TextOverflow.ellipsis,
  1412. ),
  1413. ],
  1414. ),
  1415. ),
  1416. ],
  1417. ),
  1418. ),
  1419. SizedBox(width: kMarketListNameToPriceGap),
  1420. Column(
  1421. crossAxisAlignment: CrossAxisAlignment.end,
  1422. mainAxisSize: MainAxisSize.min,
  1423. children: [
  1424. Text(
  1425. ticker.lastPriceStr != null
  1426. ? formatRawPrice(ticker.lastPriceStr!)
  1427. : formatPrice(ticker.lastPrice,
  1428. decimalPlaces: ticker.pricePrecision),
  1429. style: TextStyle(
  1430. color: cs.onSurface,
  1431. fontSize: 14,
  1432. fontWeight: FontWeight.w500,
  1433. fontFeatures: const [FontFeature.tabularFigures()],
  1434. ),
  1435. textAlign: TextAlign.end,
  1436. maxLines: 1,
  1437. overflow: TextOverflow.ellipsis,
  1438. ),
  1439. Text(
  1440. formatFiatPrice(ticker.lastPrice,
  1441. pricePrecision: ticker.pricePrecision),
  1442. style: TextStyle(
  1443. color: cs.onSurface.withAlpha(153),
  1444. fontSize: 11,
  1445. fontFeatures: const [FontFeature.tabularFigures()],
  1446. ),
  1447. textAlign: TextAlign.end,
  1448. maxLines: 1,
  1449. overflow: TextOverflow.ellipsis,
  1450. ),
  1451. ],
  1452. ),
  1453. Spacer(flex: kMarketListPriceTailSpacerFlex),
  1454. SizedBox(width: kMarketListPriceToBadgeGap),
  1455. SizedBox(
  1456. width: kMarketListChangeBadgeWidth,
  1457. child: Container(
  1458. height: 34,
  1459. alignment: Alignment.center,
  1460. decoration: BoxDecoration(
  1461. color: color,
  1462. borderRadius: BorderRadius.circular(6),
  1463. ),
  1464. child: Text(
  1465. changeStr,
  1466. style: const TextStyle(
  1467. color: Colors.white,
  1468. fontSize: 13,
  1469. fontWeight: FontWeight.w600,
  1470. fontFeatures: [FontFeature.tabularFigures()],
  1471. ),
  1472. textAlign: TextAlign.center,
  1473. ),
  1474. ),
  1475. ),
  1476. ],
  1477. ),
  1478. ),
  1479. );
  1480. }
  1481. }