home_screen.dart 48 KB

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