spot_screen.dart 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  1. import 'dart:async';
  2. import 'dart:math' as math;
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_riverpod/flutter_riverpod.dart';
  6. import 'package:go_router/go_router.dart';
  7. import 'package:intl/intl.dart';
  8. import '../../../core/l10n/app_localizations.dart';
  9. import '../../../core/theme/app_colors.dart';
  10. import '../../../core/utils/number_format.dart';
  11. import '../../../core/utils/spot_order_book_convert.dart';
  12. import '../../../core/utils/symbol_display.dart';
  13. import '../../../core/utils/top_toast.dart';
  14. import '../../../providers/auth_provider.dart';
  15. import '../../../providers/futures_provider.dart';
  16. import '../../../providers/spot_coin_cache_provider.dart';
  17. import '../../../providers/spot_provider.dart';
  18. import '../../widgets/common/app_refresh_indicator.dart';
  19. import '../../widgets/common/app_shimmer.dart';
  20. import '../../widgets/common/coin_icon.dart';
  21. import '../../widgets/common/kline_toolbar_icon.dart';
  22. import '../../widgets/common/symbol_picker_sheet.dart';
  23. /// 现货交易主页(与合约页风格保持一致)
  24. class SpotScreen extends ConsumerStatefulWidget {
  25. const SpotScreen({super.key, required this.symbol});
  26. final String symbol;
  27. @override
  28. ConsumerState<SpotScreen> createState() => _SpotScreenState();
  29. }
  30. class _SpotScreenState extends ConsumerState<SpotScreen> {
  31. late final ScrollController _scroll;
  32. final _orderPanelKey = GlobalKey<_SpotOrderPanelState>();
  33. int _obRowCount = 7;
  34. double _obRowH = 22.0;
  35. double _leftPanelHeight = 520.0;
  36. void _onLeftPanelHeight(double h) {
  37. // 表头、中间价、底部分布条+占比+模式按钮的预留高度(略收紧以便多挤 1 档)
  38. const countFixedH = 128.0;
  39. // 每侧一行约 40px(含行高与视觉间距),比原 44 略紧以填满右侧与左侧对齐
  40. final n = ((h - countFixedH) / 40).floor().clamp(4, 14);
  41. final rh = ((h - countFixedH) / (n * 2)).clamp(18.0, 28.0);
  42. if (n != _obRowCount ||
  43. (rh - _obRowH).abs() > 0.1 ||
  44. (h - _leftPanelHeight).abs() > 1) {
  45. setState(() {
  46. _obRowCount = n;
  47. _obRowH = rh;
  48. _leftPanelHeight = h;
  49. });
  50. }
  51. }
  52. @override
  53. void initState() {
  54. super.initState();
  55. _scroll = ScrollController();
  56. WidgetsBinding.instance.addPostFrameCallback((_) {
  57. if (!mounted) return;
  58. ref.read(spotActiveSymbolProvider.notifier).state = widget.symbol;
  59. ref.read(lastTradingRouteProvider.notifier).state =
  60. '/spot/${widget.symbol}';
  61. });
  62. }
  63. @override
  64. void dispose() {
  65. _scroll.dispose();
  66. super.dispose();
  67. }
  68. Future<void> _pushAndPausePolling(BuildContext context, String path) async {
  69. final notifier = ref.read(spotProvider(widget.symbol).notifier);
  70. notifier.stopPolling();
  71. await context.push(path);
  72. if (mounted) notifier.resumePolling();
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. final symbol = widget.symbol;
  77. final cs = Theme.of(context).colorScheme;
  78. return Scaffold(
  79. appBar: AppBar(
  80. elevation: 0,
  81. toolbarHeight: 44,
  82. titleSpacing: 16,
  83. title: _SegmentedTabHeader(
  84. activeIndex: 0,
  85. onTap: (i) {
  86. if (i == 1) {
  87. final futuresSym = ref.read(futuresActiveSymbolProvider);
  88. context.go(
  89. '/futures/${futuresSym.isNotEmpty ? futuresSym : 'BTCUSDT'}');
  90. }
  91. },
  92. ),
  93. centerTitle: false,
  94. bottom: PreferredSize(
  95. preferredSize: const Size.fromHeight(1),
  96. child: Container(height: 1, color: cs.outline.withAlpha(40)),
  97. ),
  98. actions: [
  99. IconButton(
  100. icon: KlineToolbarIcon(color: cs.onSurface.withAlpha(180)),
  101. onPressed: () =>
  102. _pushAndPausePolling(context, '/market/spot/$symbol'),
  103. padding: const EdgeInsets.symmetric(horizontal: 8),
  104. constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
  105. ),
  106. ],
  107. ),
  108. body: Listener(
  109. onPointerDown: (_) => FocusScope.of(context).unfocus(),
  110. child: Builder(builder: (ctx) {
  111. final isLoading =
  112. ref.watch(spotProvider(symbol).select((s) => s.isLoading));
  113. if (isLoading) return const _SpotShimmer();
  114. return AppRefreshIndicator(
  115. onRefresh: () => ref.read(spotProvider(symbol).notifier).refresh(),
  116. child: SingleChildScrollView(
  117. controller: _scroll,
  118. physics: const ClampingScrollPhysics(),
  119. child: Column(
  120. children: [
  121. Row(
  122. crossAxisAlignment: CrossAxisAlignment.start,
  123. children: [
  124. Expanded(
  125. flex: 55,
  126. child: _SizeReporter(
  127. onHeight: _onLeftPanelHeight,
  128. child: DecoratedBox(
  129. decoration: BoxDecoration(
  130. border: Border(
  131. right: BorderSide(
  132. color: cs.outline.withAlpha(40),
  133. width: 1,
  134. ),
  135. ),
  136. ),
  137. child: _SpotOrderPanel(
  138. key: _orderPanelKey,
  139. symbol: symbol,
  140. ),
  141. ),
  142. ),
  143. ),
  144. Expanded(
  145. flex: 45,
  146. child: SizedBox(
  147. height: _leftPanelHeight,
  148. child: RepaintBoundary(
  149. child: _SpotOrderBookPanel(
  150. symbol: symbol,
  151. rowCount: _obRowCount,
  152. rowHeight: _obRowH,
  153. onPriceTap: (price) => _orderPanelKey.currentState
  154. ?.setBookPrice(price),
  155. ),
  156. ),
  157. ),
  158. ),
  159. ],
  160. ),
  161. const SizedBox(height: 8),
  162. _SpotBottomSection(symbol: symbol),
  163. const SizedBox(height: 16),
  164. ],
  165. ),
  166. ),
  167. );
  168. }),
  169. ),
  170. );
  171. }
  172. }
  173. // ══════════════════════════════════════════════════════════════════════
  174. // 顶部 现货 / 永续合约 切换 Tab(共用:Spot/Futures 标题区均使用此组件)
  175. // ══════════════════════════════════════════════════════════════════════
  176. class _SegmentedTabHeader extends StatelessWidget {
  177. const _SegmentedTabHeader({required this.activeIndex, required this.onTap});
  178. final int activeIndex; // 0=现货 1=合约
  179. final ValueChanged<int> onTap;
  180. @override
  181. Widget build(BuildContext context) {
  182. final cs = Theme.of(context).colorScheme;
  183. final l10n = AppLocalizations.of(context)!;
  184. final items = [l10n.spotTab, l10n.perpetualContract];
  185. return Row(
  186. mainAxisSize: MainAxisSize.min,
  187. children: List.generate(items.length, (i) {
  188. final isActive = i == activeIndex;
  189. return Padding(
  190. padding: EdgeInsets.only(right: i == 0 ? 16 : 0),
  191. child: GestureDetector(
  192. behavior: HitTestBehavior.opaque,
  193. onTap: () => onTap(i),
  194. child: Column(
  195. mainAxisSize: MainAxisSize.min,
  196. children: [
  197. Text(
  198. items[i],
  199. style: TextStyle(
  200. color:
  201. isActive ? cs.onSurface : cs.onSurface.withAlpha(140),
  202. fontSize: 16,
  203. fontWeight: isActive ? FontWeight.w700 : FontWeight.w500,
  204. ),
  205. ),
  206. const SizedBox(height: 4),
  207. Container(
  208. width: 28,
  209. height: 3,
  210. color: isActive ? AppColors.brand : Colors.transparent,
  211. ),
  212. ],
  213. ),
  214. ),
  215. );
  216. }),
  217. );
  218. }
  219. }
  220. // ══════════════════════════════════════════════════════════════════════
  221. // 下单面板
  222. // ══════════════════════════════════════════════════════════════════════
  223. class _SpotOrderPanel extends ConsumerStatefulWidget {
  224. const _SpotOrderPanel({super.key, required this.symbol});
  225. final String symbol;
  226. @override
  227. ConsumerState<_SpotOrderPanel> createState() => _SpotOrderPanelState();
  228. }
  229. class _SpotOrderPanelState extends ConsumerState<_SpotOrderPanel> {
  230. final _priceCtrl = TextEditingController();
  231. final _amountCtrl = TextEditingController();
  232. final _triggerCtrl = TextEditingController();
  233. bool _priceFilled = false;
  234. @override
  235. void initState() {
  236. super.initState();
  237. _amountCtrl.addListener(_onAmountChanged);
  238. }
  239. @override
  240. void dispose() {
  241. _amountCtrl.removeListener(_onAmountChanged);
  242. _priceCtrl.dispose();
  243. _amountCtrl.dispose();
  244. _triggerCtrl.dispose();
  245. super.dispose();
  246. }
  247. // 输入数量时反向同步滑块(仅作展示,避免与滑块冲突)
  248. bool _settingFromSlider = false;
  249. void _onAmountChanged() {
  250. if (_settingFromSlider) return;
  251. final notifier = ref.read(spotProvider(widget.symbol).notifier);
  252. final s = ref.read(spotProvider(widget.symbol));
  253. final v = double.tryParse(_amountCtrl.text.replaceAll(',', '')) ?? 0;
  254. if (v <= 0) {
  255. notifier.setSliderPercent(0);
  256. return;
  257. }
  258. final max = _maxAmount(s);
  259. notifier.setSliderPercent(max > 0 ? (v / max).clamp(0.0, 1.0) : 0);
  260. }
  261. /// 当前用户可下单的"数量上限"(按 effectiveAmountUnit 计)
  262. double _maxAmount(SpotState s) {
  263. final price = _refPrice(s);
  264. if (s.side == SpotSide.buy) {
  265. // 买入:用 USDT 余额
  266. final usdt = s.availableQuote;
  267. if (s.effectiveAmountUnit == SpotAmountUnit.quote) return usdt;
  268. // base 单位:折算为基础币数量(需要参考价)
  269. if (price <= 0) return 0;
  270. return usdt / price;
  271. }
  272. // 卖出:用 base 余额
  273. final base = s.availableBase;
  274. if (s.effectiveAmountUnit == SpotAmountUnit.base) return base;
  275. if (price <= 0) return 0;
  276. return base * price;
  277. }
  278. /// 计算/换算时使用的参考价
  279. /// - 限价 / 计划限价:价格输入框
  280. /// - 计划市价:触发价
  281. /// - 市价:最新价
  282. double _refPrice(SpotState s) {
  283. if (s.orderType == SpotOrderType.limit) {
  284. return double.tryParse(_priceCtrl.text.replaceAll(',', '')) ?? 0;
  285. }
  286. if (s.orderType == SpotOrderType.conditionalMarket) {
  287. return double.tryParse(_triggerCtrl.text.replaceAll(',', '')) ?? 0;
  288. }
  289. return s.lastPrice;
  290. }
  291. void setBookPrice(double price) {
  292. if (!mounted) return;
  293. final s = ref.read(spotProvider(widget.symbol));
  294. final formatted = price.toStringAsFixed(s.pricePrecision);
  295. if (s.orderType == SpotOrderType.conditionalMarket) {
  296. _triggerCtrl.text = formatted;
  297. } else if (s.orderType == SpotOrderType.limit) {
  298. _priceCtrl.text = formatted;
  299. }
  300. }
  301. @override
  302. Widget build(BuildContext context) {
  303. final cs = Theme.of(context).colorScheme;
  304. final l10n = AppLocalizations.of(context)!;
  305. final provider = spotProvider(widget.symbol);
  306. final notifier = ref.read(provider.notifier);
  307. final isLoggedIn = ref.watch(isLoggedInProvider);
  308. // 切换下单类型时清空所有输入
  309. ref.listen(provider.select((s) => s.orderType), (prev, next) {
  310. if (prev == next) return;
  311. _priceFilled = false;
  312. WidgetsBinding.instance.addPostFrameCallback((_) {
  313. if (!mounted) return;
  314. FocusManager.instance.primaryFocus?.unfocus();
  315. _priceCtrl.clear();
  316. _amountCtrl.clear();
  317. _triggerCtrl.clear();
  318. notifier.setSliderPercent(0);
  319. });
  320. });
  321. // 切换买/卖时清空数量与滑块(保留价格)
  322. ref.listen(provider.select((s) => s.side), (prev, next) {
  323. if (prev == next) return;
  324. WidgetsBinding.instance.addPostFrameCallback((_) {
  325. if (!mounted) return;
  326. _amountCtrl.clear();
  327. notifier.setSliderPercent(0);
  328. });
  329. });
  330. // 限价单首次拿到价格时回填
  331. ref.listen<double>(provider.select((s) => s.lastPrice), (_, next) {
  332. if (_priceFilled || next <= 0) return;
  333. _priceFilled = true;
  334. final precision = ref.read(provider).pricePrecision;
  335. WidgetsBinding.instance.addPostFrameCallback((_) {
  336. if (!mounted) return;
  337. if (_priceCtrl.text.isEmpty) {
  338. _priceCtrl.text = next.toStringAsFixed(precision);
  339. }
  340. });
  341. });
  342. final symbolValue = ref.watch(provider.select((s) => s.symbol));
  343. final change = ref.watch(provider.select((s) => s.change24h));
  344. final orderType = ref.watch(provider.select((s) => s.orderType));
  345. final side = ref.watch(provider.select((s) => s.side));
  346. final unit = ref.watch(provider.select((s) => s.effectiveAmountUnit));
  347. final showPriceInput = ref.watch(provider.select((s) => s.showPriceInput));
  348. final showTrigger = ref.watch(provider.select((s) => s.showTriggerPrice));
  349. final lastPrice = ref.watch(provider.select((s) => s.lastPrice));
  350. final availQuote = ref.watch(provider.select((s) => s.availableQuote));
  351. final availBase = ref.watch(provider.select((s) => s.availableBase));
  352. final base = ref.watch(provider.select((s) => s.baseCoin));
  353. final quote = ref.watch(provider.select((s) => s.quoteCoin));
  354. final pricePre = ref.watch(provider.select((s) => s.pricePrecision));
  355. final volPre = ref.watch(provider.select((s) => s.volumePrecision));
  356. final sliderPct = ref.watch(provider.select((s) => s.sliderPercent));
  357. final unitLabel = unit == SpotAmountUnit.quote ? quote : base;
  358. final priceFormatter = _PrecisionInputFormatter(pricePre);
  359. final amountFormatter = _PrecisionInputFormatter(
  360. unit == SpotAmountUnit.quote ? 2 : volPre,
  361. );
  362. return Padding(
  363. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
  364. child: Column(
  365. crossAxisAlignment: CrossAxisAlignment.start,
  366. children: [
  367. GestureDetector(
  368. onTap: () => _showSymbolPicker(context),
  369. behavior: HitTestBehavior.opaque,
  370. child: Padding(
  371. padding: const EdgeInsets.only(bottom: 4),
  372. child: Row(
  373. mainAxisSize: MainAxisSize.min,
  374. children: [
  375. Flexible(
  376. child: Text(
  377. formatUsdtPairDisplay(symbolValue),
  378. overflow: TextOverflow.ellipsis,
  379. style: TextStyle(
  380. color: cs.onSurface,
  381. fontSize: 17,
  382. fontWeight: FontWeight.w700,
  383. ),
  384. ),
  385. ),
  386. Icon(Icons.keyboard_arrow_down,
  387. color: cs.onSurface.withAlpha(153), size: 16),
  388. const SizedBox(width: 6),
  389. Text(
  390. formatChange(change),
  391. style: TextStyle(
  392. color: AppColors.changeColor(change),
  393. fontSize: 12,
  394. fontWeight: FontWeight.w500,
  395. fontFeatures: const [FontFeature.tabularFigures()],
  396. ),
  397. ),
  398. ],
  399. ),
  400. ),
  401. ),
  402. const SizedBox(height: 8),
  403. // 买/卖 Tab
  404. _BuySellTabs(
  405. side: side,
  406. onChanged: notifier.setSide,
  407. ),
  408. const SizedBox(height: 10),
  409. // 订单类型下拉
  410. _SpotOrderTypeDropdown(symbol: widget.symbol),
  411. const SizedBox(height: 8),
  412. // 触发价(仅条件委托)
  413. if (showTrigger) ...[
  414. _LargeInput(
  415. controller: _triggerCtrl,
  416. label: l10n.triggerPrice,
  417. unit: quote,
  418. inputFormatters: [priceFormatter],
  419. ),
  420. const SizedBox(height: 8),
  421. ],
  422. // 价格 输入框 / 市价占位
  423. if (showPriceInput) ...[
  424. _LargeInput(
  425. controller: _priceCtrl,
  426. label: l10n.priceLabel2,
  427. unit: quote,
  428. inputFormatters: [priceFormatter],
  429. ),
  430. const SizedBox(height: 8),
  431. ] else ...[
  432. _MarketPriceBox(label: l10n.marketBest, quote: quote),
  433. const SizedBox(height: 8),
  434. ],
  435. // 数量
  436. _LargeInput(
  437. controller: _amountCtrl,
  438. label: side == SpotSide.buy && orderType != SpotOrderType.limit
  439. ? l10n.amountQuoteLabel
  440. : l10n.quantityLabel,
  441. unit: unitLabel,
  442. // 限价单:买入时 BTC/USDT 可切换;卖出时同样;市价/计划市价单位锁定
  443. showUnitDropdown: orderType == SpotOrderType.limit,
  444. onUnitTap: orderType == SpotOrderType.limit
  445. ? () => _showAmountUnitSheet(context)
  446. : null,
  447. inputFormatters: [amountFormatter],
  448. ),
  449. // 总价(市价时根据滑块/数量计算)
  450. if (orderType != SpotOrderType.limit && lastPrice > 0)
  451. ListenableBuilder(
  452. listenable: _amountCtrl,
  453. builder: (_, __) {
  454. final v =
  455. double.tryParse(_amountCtrl.text.replaceAll(',', '')) ?? 0;
  456. if (v <= 0) return const SizedBox.shrink();
  457. final estUsdt = side == SpotSide.buy ? v : v * lastPrice;
  458. return Padding(
  459. padding: const EdgeInsets.only(top: 6),
  460. child: Column(
  461. crossAxisAlignment: CrossAxisAlignment.start,
  462. children: [
  463. Text(
  464. formatAmount(estUsdt),
  465. style: TextStyle(
  466. color: cs.onSurface,
  467. fontSize: 15,
  468. fontWeight: FontWeight.w700,
  469. fontFeatures: const [FontFeature.tabularFigures()],
  470. ),
  471. ),
  472. Text(
  473. formatFiatPrice(estUsdt),
  474. style: TextStyle(
  475. color: cs.onSurface.withAlpha(120),
  476. fontSize: 11,
  477. ),
  478. ),
  479. ],
  480. ),
  481. );
  482. },
  483. ),
  484. const SizedBox(height: 10),
  485. // 滑块
  486. _PercentSlider(
  487. percent: sliderPct,
  488. onChanged: (pct) {
  489. final s = ref.read(provider);
  490. if (pct > 0 &&
  491. _refPrice(s) <= 0 &&
  492. orderType != SpotOrderType.market) {
  493. showTopToast(
  494. context,
  495. message: orderType == SpotOrderType.conditionalMarket
  496. ? l10n.enterTriggerPrice
  497. : l10n.enterPrice,
  498. backgroundColor: AppColors.fall,
  499. );
  500. return;
  501. }
  502. notifier.setSliderPercent(pct);
  503. _settingFromSlider = true;
  504. if (pct == 0) {
  505. _amountCtrl.clear();
  506. } else {
  507. final max = _maxAmount(s);
  508. final v = max * pct;
  509. final dp =
  510. s.effectiveAmountUnit == SpotAmountUnit.quote ? 2 : volPre;
  511. final factor = math.pow(10, dp).toDouble();
  512. final truncated = (v * factor).floorToDouble() / factor;
  513. _amountCtrl.text = truncated.toStringAsFixed(dp);
  514. }
  515. _settingFromSlider = false;
  516. },
  517. ),
  518. const SizedBox(height: 10),
  519. // 可用(买入展示计价余额,卖出展示基础币余额)
  520. Padding(
  521. padding: const EdgeInsets.symmetric(horizontal: 12),
  522. child: Row(
  523. children: [
  524. Text('${l10n.availableLabel} ',
  525. style: TextStyle(
  526. color: cs.onSurface.withAlpha(153), fontSize: 12)),
  527. Text(
  528. side == SpotSide.buy
  529. ? '${formatAmount(availQuote)} $quote'
  530. : '${formatAmount(availBase)} $base',
  531. style: TextStyle(
  532. color: cs.onSurface,
  533. fontSize: 12,
  534. fontWeight: FontWeight.w600,
  535. ),
  536. ),
  537. const Spacer(),
  538. GestureDetector(
  539. onTap: () async {
  540. final n = ref.read(provider.notifier);
  541. n.stopPolling();
  542. await context.push('/asset/transfer');
  543. if (context.mounted) n.resumePolling();
  544. },
  545. child: Icon(Icons.swap_horiz,
  546. color: cs.onSurface.withAlpha(153), size: 16),
  547. ),
  548. ],
  549. ),
  550. ),
  551. const SizedBox(height: 4),
  552. Builder(builder: (_) {
  553. final isBuy = side == SpotSide.buy;
  554. final String canText;
  555. if (isBuy) {
  556. final qty = lastPrice > 0 ? availQuote / lastPrice : 0.0;
  557. canText = '${formatAmount(qty, decimals: volPre)} $base';
  558. } else {
  559. canText = '${formatAmount(availBase, decimals: volPre)} $base';
  560. }
  561. return Row(
  562. children: [
  563. Text(
  564. isBuy ? l10n.canBuy : l10n.canSell,
  565. style: TextStyle(
  566. color: cs.onSurface.withAlpha(153), fontSize: 12),
  567. ),
  568. const SizedBox(width: 4),
  569. Text(
  570. canText,
  571. style: TextStyle(
  572. color: cs.onSurface,
  573. fontSize: 12,
  574. fontWeight: FontWeight.w600,
  575. ),
  576. ),
  577. ],
  578. );
  579. }),
  580. const SizedBox(height: 12),
  581. // 主操作按钮
  582. SizedBox(
  583. width: double.infinity,
  584. height: 44,
  585. child: ElevatedButton(
  586. onPressed: isLoggedIn
  587. ? () => _placeOrder(context)
  588. : () => context.push('/login'),
  589. style: ElevatedButton.styleFrom(
  590. backgroundColor:
  591. side == SpotSide.buy ? AppColors.rise : AppColors.fall,
  592. shape: RoundedRectangleBorder(
  593. borderRadius: BorderRadius.circular(8),
  594. ),
  595. elevation: 0,
  596. ),
  597. child: Text(
  598. side == SpotSide.buy ? l10n.buyCoin(base) : l10n.sellCoin(base),
  599. style: const TextStyle(
  600. color: Colors.white,
  601. fontSize: 15,
  602. fontWeight: FontWeight.w700,
  603. ),
  604. ),
  605. ),
  606. ),
  607. const SizedBox(height: 12),
  608. ],
  609. ),
  610. );
  611. }
  612. Future<void> _placeOrder(BuildContext context) async {
  613. final notifier = ref.read(spotProvider(widget.symbol).notifier);
  614. final s = ref.read(spotProvider(widget.symbol));
  615. final l10n = AppLocalizations.of(context)!;
  616. final price = double.tryParse(_priceCtrl.text.replaceAll(',', ''));
  617. final inputAmount =
  618. double.tryParse(_amountCtrl.text.replaceAll(',', '')) ?? 0;
  619. if (s.orderType == SpotOrderType.limit && (price == null || price <= 0)) {
  620. showTopToast(context,
  621. message: l10n.enterPrice, backgroundColor: AppColors.fall);
  622. return;
  623. }
  624. if (inputAmount <= 0) {
  625. showTopToast(context,
  626. message: l10n.errEnterAmount, backgroundColor: AppColors.fall);
  627. return;
  628. }
  629. final unit = s.effectiveAmountUnit;
  630. final prep = notifier.prepareAmount(
  631. side: s.side,
  632. type: s.orderType,
  633. inputAmount: inputAmount,
  634. unit: unit,
  635. price: price,
  636. );
  637. if (prep == null) {
  638. showTopToast(context,
  639. message: l10n.errVolumeInsufficient, backgroundColor: AppColors.fall);
  640. return;
  641. }
  642. if (s.orderType == SpotOrderType.conditionalMarket) {
  643. showTopToast(
  644. context,
  645. message: l10n.spotConditionalNotSupported,
  646. backgroundColor: AppColors.fall,
  647. );
  648. return;
  649. }
  650. final err = await notifier.placeOrder(
  651. side: s.side,
  652. type: s.orderType,
  653. price: price,
  654. amount: prep.payload,
  655. );
  656. if (!context.mounted) return;
  657. if (err == null) {
  658. _amountCtrl.clear();
  659. notifier.setSliderPercent(0);
  660. showTopToast(
  661. context,
  662. message: l10n.orderSuccess,
  663. backgroundColor: AppColors.rise,
  664. );
  665. } else {
  666. showTopToast(
  667. context,
  668. message: _resolveSpotError(err, l10n),
  669. backgroundColor: AppColors.fall,
  670. );
  671. }
  672. }
  673. void _showSymbolPicker(BuildContext context) {
  674. FocusScope.of(context).unfocus();
  675. showModalBottomSheet<void>(
  676. context: context,
  677. useRootNavigator: true,
  678. isScrollControlled: true,
  679. backgroundColor: Theme.of(context).colorScheme.surface,
  680. shape: const RoundedRectangleBorder(
  681. borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
  682. ),
  683. builder: (sheetCtx) => SymbolPickerSheet(
  684. currentSymbol: widget.symbol,
  685. initialTab: SymbolPickerTab.spot,
  686. visibleTabs: const [SymbolPickerTab.spot],
  687. onSelected: (newSymbol) {
  688. Navigator.pop(sheetCtx);
  689. context.go('/spot/$newSymbol');
  690. },
  691. onSpotSelected: (newSymbol) {
  692. Navigator.pop(sheetCtx);
  693. context.go('/spot/$newSymbol');
  694. },
  695. ),
  696. );
  697. }
  698. void _showAmountUnitSheet(BuildContext context) {
  699. FocusScope.of(context).unfocus();
  700. final cs = Theme.of(context).colorScheme;
  701. final s = ref.read(spotProvider(widget.symbol));
  702. final notifier = ref.read(spotProvider(widget.symbol).notifier);
  703. final units = [
  704. (SpotAmountUnit.base, s.baseCoin),
  705. (SpotAmountUnit.quote, s.quoteCoin),
  706. ];
  707. showModalBottomSheet<void>(
  708. context: context,
  709. useRootNavigator: true,
  710. backgroundColor: cs.surface,
  711. shape: const RoundedRectangleBorder(
  712. borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
  713. ),
  714. builder: (ctx) => SafeArea(
  715. child: Column(
  716. mainAxisSize: MainAxisSize.min,
  717. children: units
  718. .map(
  719. (u) => GestureDetector(
  720. onTap: () {
  721. notifier.setAmountUnit(u.$1);
  722. _amountCtrl.clear();
  723. notifier.setSliderPercent(0);
  724. Navigator.pop(ctx);
  725. },
  726. child: Padding(
  727. padding: const EdgeInsets.symmetric(
  728. horizontal: 16, vertical: 14),
  729. child: Row(
  730. children: [
  731. Expanded(
  732. child: Text(u.$2,
  733. style: TextStyle(
  734. color: s.amountUnit == u.$1
  735. ? AppColors.brand
  736. : cs.onSurface,
  737. fontSize: 14)),
  738. ),
  739. if (s.amountUnit == u.$1)
  740. const Icon(Icons.check,
  741. color: AppColors.brand, size: 18),
  742. ],
  743. ),
  744. ),
  745. ),
  746. )
  747. .toList(),
  748. ),
  749. ),
  750. );
  751. }
  752. }
  753. // ── 买/卖 Tabs ────────────────────────────────────────
  754. class _BuySellTabs extends StatelessWidget {
  755. const _BuySellTabs({required this.side, required this.onChanged});
  756. final SpotSide side;
  757. final ValueChanged<SpotSide> onChanged;
  758. @override
  759. Widget build(BuildContext context) {
  760. final cs = Theme.of(context).colorScheme;
  761. final isDark = Theme.of(context).brightness == Brightness.dark;
  762. final l10n = AppLocalizations.of(context)!;
  763. final bg = isDark ? AppColors.darkBgTertiary : AppColors.lightBgSecondary;
  764. Widget tab({
  765. required SpotSide value,
  766. required String label,
  767. required Color activeColor,
  768. }) {
  769. final active = side == value;
  770. return Expanded(
  771. child: GestureDetector(
  772. onTap: () => onChanged(value),
  773. behavior: HitTestBehavior.opaque,
  774. child: Container(
  775. height: 36,
  776. decoration: BoxDecoration(
  777. color: active ? activeColor : Colors.transparent,
  778. borderRadius: BorderRadius.circular(6),
  779. ),
  780. alignment: Alignment.center,
  781. child: Text(
  782. label,
  783. style: TextStyle(
  784. color: active ? Colors.white : cs.onSurface.withAlpha(160),
  785. fontSize: 14,
  786. fontWeight: FontWeight.w700,
  787. ),
  788. ),
  789. ),
  790. ),
  791. );
  792. }
  793. return Container(
  794. padding: const EdgeInsets.all(2),
  795. decoration: BoxDecoration(
  796. color: bg,
  797. borderRadius: BorderRadius.circular(8),
  798. ),
  799. child: Row(
  800. children: [
  801. tab(
  802. value: SpotSide.buy,
  803. label: l10n.buyAction,
  804. activeColor: AppColors.rise),
  805. const SizedBox(width: 4),
  806. tab(
  807. value: SpotSide.sell,
  808. label: l10n.sellAction,
  809. activeColor: AppColors.fall),
  810. ],
  811. ),
  812. );
  813. }
  814. }
  815. // ── 订单类型下拉 ──────────────────────────────────────
  816. class _SpotOrderTypeDropdown extends ConsumerWidget {
  817. const _SpotOrderTypeDropdown({required this.symbol});
  818. final String symbol;
  819. @override
  820. Widget build(BuildContext context, WidgetRef ref) {
  821. final cs = Theme.of(context).colorScheme;
  822. final isDark = Theme.of(context).brightness == Brightness.dark;
  823. final orderType =
  824. ref.watch(spotProvider(symbol).select((s) => s.orderType));
  825. final notifier = ref.read(spotProvider(symbol).notifier);
  826. final l10n = AppLocalizations.of(context)!;
  827. const visibleTypes = [SpotOrderType.market, SpotOrderType.limit];
  828. final safeOrderType =
  829. visibleTypes.contains(orderType) ? orderType : SpotOrderType.market;
  830. String labelOf(SpotOrderType t) {
  831. switch (t) {
  832. case SpotOrderType.market:
  833. return l10n.marketOrder;
  834. case SpotOrderType.limit:
  835. return l10n.limitOrder;
  836. case SpotOrderType.conditionalMarket:
  837. return l10n.marketOrder;
  838. }
  839. }
  840. return GestureDetector(
  841. onTap: () {
  842. FocusScope.of(context).unfocus();
  843. showModalBottomSheet<void>(
  844. context: context,
  845. useRootNavigator: true,
  846. backgroundColor: cs.surface,
  847. shape: const RoundedRectangleBorder(
  848. borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
  849. ),
  850. builder: (sheetCtx) => SafeArea(
  851. child: Column(
  852. mainAxisSize: MainAxisSize.min,
  853. children: visibleTypes.map((t) {
  854. final isActive = safeOrderType == t;
  855. return GestureDetector(
  856. onTap: () {
  857. notifier.setOrderType(t);
  858. Navigator.pop(sheetCtx);
  859. },
  860. child: Padding(
  861. padding: const EdgeInsets.symmetric(
  862. horizontal: 16, vertical: 14),
  863. child: Row(
  864. children: [
  865. Expanded(
  866. child: Text(
  867. labelOf(t),
  868. style: TextStyle(
  869. color:
  870. isActive ? AppColors.brand : cs.onSurface,
  871. fontSize: 14),
  872. ),
  873. ),
  874. if (isActive)
  875. const Icon(Icons.check,
  876. color: AppColors.brand, size: 18),
  877. ],
  878. ),
  879. ),
  880. );
  881. }).toList(),
  882. ),
  883. ),
  884. );
  885. },
  886. child: Container(
  887. height: 40,
  888. padding: const EdgeInsets.symmetric(horizontal: 10),
  889. decoration: BoxDecoration(
  890. color:
  891. isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  892. borderRadius: BorderRadius.circular(8),
  893. ),
  894. child: Row(
  895. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  896. children: [
  897. Text(labelOf(safeOrderType),
  898. style: TextStyle(color: cs.onSurface, fontSize: 14)),
  899. Icon(Icons.keyboard_arrow_down,
  900. color: cs.onSurface.withAlpha(153), size: 18),
  901. ],
  902. ),
  903. ),
  904. );
  905. }
  906. }
  907. // ── 市价占位 ──────────────────────────────────────────
  908. class _MarketPriceBox extends StatelessWidget {
  909. const _MarketPriceBox({required this.label, required this.quote});
  910. final String label;
  911. final String quote;
  912. @override
  913. Widget build(BuildContext context) {
  914. final cs = Theme.of(context).colorScheme;
  915. final isDark = Theme.of(context).brightness == Brightness.dark;
  916. return Container(
  917. height: 44,
  918. decoration: BoxDecoration(
  919. color: isDark ? AppColors.darkBgTertiary : AppColors.lightBgSecondary,
  920. borderRadius: BorderRadius.circular(8),
  921. ),
  922. padding: const EdgeInsets.symmetric(horizontal: 12),
  923. child: Row(
  924. children: [
  925. Expanded(
  926. child: Text(
  927. label,
  928. style: TextStyle(
  929. color: cs.onSurface.withAlpha(160),
  930. fontSize: 15,
  931. fontWeight: FontWeight.w500,
  932. ),
  933. ),
  934. ),
  935. Text(quote,
  936. style:
  937. TextStyle(color: cs.onSurface.withAlpha(150), fontSize: 13)),
  938. ],
  939. ),
  940. );
  941. }
  942. }
  943. // ══════════════════════════════════════════════════════════════════════
  944. // 通用输入框(与合约页保持一致风格)
  945. // ══════════════════════════════════════════════════════════════════════
  946. class _LargeInput extends StatefulWidget {
  947. const _LargeInput({
  948. required this.controller,
  949. required this.label,
  950. required this.unit,
  951. this.showUnitDropdown = false,
  952. this.onUnitTap,
  953. this.inputFormatters,
  954. });
  955. final TextEditingController controller;
  956. final String label;
  957. final String unit;
  958. final bool showUnitDropdown;
  959. final VoidCallback? onUnitTap;
  960. final List<TextInputFormatter>? inputFormatters;
  961. @override
  962. State<_LargeInput> createState() => _LargeInputState();
  963. }
  964. class _LargeInputState extends State<_LargeInput>
  965. with SingleTickerProviderStateMixin {
  966. final _focusNode = FocusNode();
  967. late final AnimationController _animCtrl;
  968. late final Animation<double> _curvedAnim;
  969. bool get _isActive =>
  970. _focusNode.hasFocus || widget.controller.text.isNotEmpty;
  971. @override
  972. void initState() {
  973. super.initState();
  974. _animCtrl = AnimationController(
  975. vsync: this,
  976. duration: const Duration(milliseconds: 220),
  977. );
  978. _curvedAnim = CurvedAnimation(
  979. parent: _animCtrl,
  980. curve: const Cubic(0.4, 0.0, 0.2, 1.0),
  981. );
  982. _focusNode.addListener(_onChanged);
  983. widget.controller.addListener(_onChanged);
  984. if (_isActive) _animCtrl.value = 1.0;
  985. }
  986. @override
  987. void didUpdateWidget(_LargeInput oldWidget) {
  988. super.didUpdateWidget(oldWidget);
  989. if (oldWidget.controller != widget.controller) {
  990. oldWidget.controller.removeListener(_onChanged);
  991. widget.controller.addListener(_onChanged);
  992. _animCtrl.value = _isActive ? 1.0 : 0.0;
  993. }
  994. }
  995. void _onChanged() {
  996. if (_isActive) {
  997. _animCtrl.forward();
  998. } else {
  999. _animCtrl.reverse();
  1000. }
  1001. setState(() {});
  1002. }
  1003. @override
  1004. void dispose() {
  1005. _focusNode.removeListener(_onChanged);
  1006. widget.controller.removeListener(_onChanged);
  1007. _focusNode.dispose();
  1008. _animCtrl.dispose();
  1009. super.dispose();
  1010. }
  1011. @override
  1012. Widget build(BuildContext context) {
  1013. final cs = Theme.of(context).colorScheme;
  1014. final isDark = Theme.of(context).brightness == Brightness.dark;
  1015. final unfocusedBg =
  1016. isDark ? AppColors.darkBgTertiary : AppColors.lightBgSecondary;
  1017. final focusedBg = isDark ? AppColors.darkBgSecondary : Colors.white;
  1018. final activeBorder = isDark
  1019. ? AppColors.darkTextPrimary.withAlpha(200)
  1020. : const Color(0xFF383838);
  1021. return GestureDetector(
  1022. behavior: HitTestBehavior.opaque,
  1023. onTap: () => _focusNode.requestFocus(),
  1024. child: Container(
  1025. height: 44,
  1026. padding: const EdgeInsets.symmetric(horizontal: 12),
  1027. decoration: BoxDecoration(
  1028. color: _focusNode.hasFocus ? focusedBg : unfocusedBg,
  1029. borderRadius: BorderRadius.circular(8),
  1030. border: _focusNode.hasFocus
  1031. ? Border.all(color: activeBorder, width: 1.5)
  1032. : null,
  1033. ),
  1034. child: Row(
  1035. crossAxisAlignment: CrossAxisAlignment.center,
  1036. children: [
  1037. Expanded(
  1038. child: AnimatedBuilder(
  1039. animation: _curvedAnim,
  1040. builder: (context, inputChild) {
  1041. final t = _curvedAnim.value;
  1042. final labelSize = 13.0 + (10.0 - 13.0) * t;
  1043. final labelColor = isDark
  1044. ? AppColors.darkTextSecondary
  1045. : AppColors.lightTextSecondary;
  1046. final centerTop = (44.0 - labelSize) / 2;
  1047. const activeTop = 5.0;
  1048. final labelTop = centerTop + (activeTop - centerTop) * t;
  1049. return SizedBox(
  1050. height: 44,
  1051. child: Stack(
  1052. clipBehavior: Clip.none,
  1053. children: [
  1054. Positioned(
  1055. top: labelTop,
  1056. left: 0,
  1057. right: 0,
  1058. child: Text(
  1059. widget.label,
  1060. style: TextStyle(
  1061. color: labelColor,
  1062. fontSize: labelSize,
  1063. height: 1.0,
  1064. ),
  1065. ),
  1066. ),
  1067. Positioned(
  1068. bottom: 5,
  1069. left: 0,
  1070. right: 0,
  1071. child: Opacity(
  1072. opacity: t,
  1073. child: inputChild,
  1074. ),
  1075. ),
  1076. ],
  1077. ),
  1078. );
  1079. },
  1080. child: TextField(
  1081. focusNode: _focusNode,
  1082. controller: widget.controller,
  1083. keyboardType:
  1084. const TextInputType.numberWithOptions(decimal: true),
  1085. inputFormatters: widget.inputFormatters,
  1086. style: TextStyle(
  1087. color: cs.onSurface,
  1088. fontSize: 14,
  1089. fontWeight: FontWeight.w500,
  1090. ),
  1091. decoration: const InputDecoration(
  1092. border: InputBorder.none,
  1093. focusedBorder: InputBorder.none,
  1094. enabledBorder: InputBorder.none,
  1095. filled: false,
  1096. isDense: true,
  1097. contentPadding: EdgeInsets.zero,
  1098. ),
  1099. ),
  1100. ),
  1101. ),
  1102. const SizedBox(width: 6),
  1103. GestureDetector(
  1104. behavior: HitTestBehavior.opaque,
  1105. onTap: widget.showUnitDropdown
  1106. ? () {
  1107. _focusNode.unfocus();
  1108. widget.onUnitTap?.call();
  1109. }
  1110. : null,
  1111. child: Row(
  1112. mainAxisSize: MainAxisSize.min,
  1113. children: [
  1114. Text(widget.unit,
  1115. style: TextStyle(
  1116. color: cs.onSurface.withAlpha(153), fontSize: 13)),
  1117. if (widget.showUnitDropdown)
  1118. Icon(Icons.keyboard_arrow_down,
  1119. color: cs.onSurface.withAlpha(153), size: 14),
  1120. ],
  1121. ),
  1122. ),
  1123. ],
  1124. ),
  1125. ),
  1126. );
  1127. }
  1128. }
  1129. // ══════════════════════════════════════════════════════════════════════
  1130. // 百分比滑动条(与合约页相同的视觉,简化实现)
  1131. // ══════════════════════════════════════════════════════════════════════
  1132. class _PercentSlider extends StatefulWidget {
  1133. const _PercentSlider({required this.percent, required this.onChanged});
  1134. final double percent;
  1135. final ValueChanged<double> onChanged;
  1136. @override
  1137. State<_PercentSlider> createState() => _PercentSliderState();
  1138. }
  1139. class _PercentSliderState extends State<_PercentSlider> {
  1140. static const _stops = [0.0, 0.25, 0.5, 0.75, 1.0];
  1141. static const _thumbSize = 18.0;
  1142. @override
  1143. Widget build(BuildContext context) {
  1144. final cs = Theme.of(context).colorScheme;
  1145. final pct = widget.percent.clamp(0.0, 1.0);
  1146. return Column(
  1147. crossAxisAlignment: CrossAxisAlignment.stretch,
  1148. children: [
  1149. SizedBox(
  1150. height: _thumbSize,
  1151. child: LayoutBuilder(builder: (_, constraints) {
  1152. final w = constraints.maxWidth;
  1153. const r = _thumbSize / 2;
  1154. final trackW = w - _thumbSize;
  1155. final thumbX = r + trackW * pct;
  1156. return GestureDetector(
  1157. behavior: HitTestBehavior.opaque,
  1158. onHorizontalDragUpdate: (d) {
  1159. final newPct =
  1160. ((thumbX + d.delta.dx - r) / trackW).clamp(0.0, 1.0);
  1161. widget.onChanged(newPct);
  1162. },
  1163. onTapDown: (d) {
  1164. final tapPct =
  1165. ((d.localPosition.dx - r) / trackW).clamp(0.0, 1.0);
  1166. HapticFeedback.selectionClick();
  1167. widget.onChanged(tapPct);
  1168. },
  1169. child: Stack(
  1170. children: [
  1171. Positioned(
  1172. top: (_thumbSize - 3) / 2,
  1173. left: r,
  1174. right: r,
  1175. height: 3,
  1176. child: Container(
  1177. decoration: BoxDecoration(
  1178. color: cs.outline.withAlpha(50),
  1179. borderRadius: BorderRadius.circular(2),
  1180. ),
  1181. ),
  1182. ),
  1183. Positioned(
  1184. top: (_thumbSize - 3) / 2,
  1185. left: r,
  1186. width: thumbX - r,
  1187. height: 3,
  1188. child: Container(
  1189. decoration: BoxDecoration(
  1190. color: AppColors.brand,
  1191. borderRadius: BorderRadius.circular(2),
  1192. ),
  1193. ),
  1194. ),
  1195. for (final p in _stops)
  1196. Positioned(
  1197. left: r + trackW * p - 3,
  1198. top: (_thumbSize - 6) / 2,
  1199. child: Container(
  1200. width: 6,
  1201. height: 6,
  1202. decoration: BoxDecoration(
  1203. shape: BoxShape.circle,
  1204. color: p <= pct
  1205. ? AppColors.brand
  1206. : cs.outline.withAlpha(80),
  1207. ),
  1208. ),
  1209. ),
  1210. Positioned(
  1211. left: thumbX - r,
  1212. top: 0,
  1213. width: _thumbSize,
  1214. height: _thumbSize,
  1215. child: Container(
  1216. decoration: BoxDecoration(
  1217. color: Colors.white,
  1218. shape: BoxShape.circle,
  1219. border: Border.all(color: AppColors.brand, width: 2.5),
  1220. boxShadow: [
  1221. BoxShadow(
  1222. color: Colors.black.withAlpha(70),
  1223. blurRadius: 5,
  1224. spreadRadius: 1,
  1225. offset: const Offset(0, 1.5),
  1226. ),
  1227. ],
  1228. ),
  1229. ),
  1230. ),
  1231. ],
  1232. ),
  1233. );
  1234. }),
  1235. ),
  1236. const SizedBox(height: 4),
  1237. LayoutBuilder(builder: (_, constraints) {
  1238. final w = constraints.maxWidth;
  1239. const r = _thumbSize / 2;
  1240. final trackW = w - _thumbSize;
  1241. final tickInterval = trackW / (_stops.length - 1);
  1242. final btnW = (tickInterval - 6).clamp(20.0, 64.0);
  1243. return SizedBox(
  1244. height: 22,
  1245. child: Stack(
  1246. children: [
  1247. for (final p in _stops)
  1248. Positioned(
  1249. left: (r + trackW * p - btnW / 2).clamp(0.0, w - btnW),
  1250. top: 0,
  1251. width: btnW,
  1252. height: 22,
  1253. child: Builder(builder: (_) {
  1254. final selected = (pct - p).abs() < 0.001;
  1255. return GestureDetector(
  1256. onTap: () {
  1257. HapticFeedback.selectionClick();
  1258. widget.onChanged(p);
  1259. },
  1260. child: Container(
  1261. decoration: BoxDecoration(
  1262. color: selected
  1263. ? AppColors.brand
  1264. : cs.outline.withAlpha(25),
  1265. borderRadius: BorderRadius.circular(4),
  1266. ),
  1267. alignment: Alignment.center,
  1268. child: Text(
  1269. '${(p * 100).toInt()}%',
  1270. style: TextStyle(
  1271. color: selected
  1272. ? Colors.black
  1273. : cs.onSurface.withAlpha(102),
  1274. fontSize: 10,
  1275. fontWeight: FontWeight.w500,
  1276. ),
  1277. ),
  1278. ),
  1279. );
  1280. }),
  1281. ),
  1282. ],
  1283. ),
  1284. );
  1285. }),
  1286. ],
  1287. );
  1288. }
  1289. }
  1290. // ══════════════════════════════════════════════════════════════════════
  1291. // 盘口(布局与合约页 _OrderBookPanel 一致:列标题、深度条、买卖占比、模式切换)
  1292. // ══════════════════════════════════════════════════════════════════════
  1293. class _SpotOrderBookPanel extends ConsumerStatefulWidget {
  1294. const _SpotOrderBookPanel({
  1295. required this.symbol,
  1296. required this.rowCount,
  1297. required this.rowHeight,
  1298. this.onPriceTap,
  1299. });
  1300. final String symbol;
  1301. final int rowCount;
  1302. final double rowHeight;
  1303. final ValueChanged<double>? onPriceTap;
  1304. @override
  1305. ConsumerState<_SpotOrderBookPanel> createState() =>
  1306. _SpotOrderBookPanelState();
  1307. }
  1308. class _SpotOrderBookPanelState extends ConsumerState<_SpotOrderBookPanel> {
  1309. /// 0=双向 1=仅卖 2=仅买
  1310. int _bookMode = 0;
  1311. /// 0=depth0(最少小数位) 1=depth1(中间) 2=depth2(最多小数位)
  1312. int _depthStep = 2;
  1313. static String _precisionLabel(int precision) {
  1314. if (precision <= 0) return '1';
  1315. return '0.${'0' * (precision - 1)}1';
  1316. }
  1317. void _showDepthStepSheet(BuildContext context) {
  1318. final cs = Theme.of(context).colorScheme;
  1319. final state = ref.read(spotProvider(widget.symbol));
  1320. final options = [
  1321. (0, state.depth0Pre),
  1322. (1, state.depth1Pre),
  1323. (2, state.depth2Pre),
  1324. ];
  1325. showModalBottomSheet<void>(
  1326. context: context,
  1327. useRootNavigator: true,
  1328. backgroundColor: cs.surface,
  1329. shape: const RoundedRectangleBorder(
  1330. borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
  1331. ),
  1332. builder: (ctx) => SafeArea(
  1333. child: Column(
  1334. mainAxisSize: MainAxisSize.min,
  1335. children: options.map((opt) {
  1336. final isActive = _depthStep == opt.$1;
  1337. return GestureDetector(
  1338. onTap: () {
  1339. setState(() => _depthStep = opt.$1);
  1340. Navigator.pop(ctx);
  1341. },
  1342. child: Padding(
  1343. padding:
  1344. const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
  1345. child: Row(
  1346. children: [
  1347. Expanded(
  1348. child: Text(
  1349. _precisionLabel(opt.$2),
  1350. style: TextStyle(
  1351. color: isActive ? AppColors.brand : cs.onSurface,
  1352. fontSize: 14,
  1353. ),
  1354. ),
  1355. ),
  1356. if (isActive)
  1357. const Icon(Icons.check, color: AppColors.brand, size: 18),
  1358. ],
  1359. ),
  1360. ),
  1361. );
  1362. }).toList(),
  1363. ),
  1364. ),
  1365. );
  1366. }
  1367. @override
  1368. Widget build(BuildContext context) {
  1369. final cs = Theme.of(context).colorScheme;
  1370. final l10n = AppLocalizations.of(context)!;
  1371. final state = ref.watch(spotProvider(widget.symbol));
  1372. final rawAsks = state.orderBookAsks;
  1373. final rawBids = state.orderBookBids;
  1374. final n = widget.rowCount;
  1375. final depthPrecision = _depthStep == 0
  1376. ? state.depth0Pre
  1377. : _depthStep == 1
  1378. ? state.depth1Pre
  1379. : state.depth2Pre;
  1380. // 合并档位后按价排序:后端买卖盘均为「价高→价低」;分桶合并可能打乱顺序,需恢复单调性
  1381. final aggregatedAsks = aggregateSpotDepthLevels(rawAsks, depthPrecision)
  1382. ..sort((a, b) => spotDepthP(b).compareTo(spotDepthP(a)));
  1383. final aggregatedBids = aggregateSpotDepthLevels(rawBids, depthPrecision)
  1384. ..sort((a, b) => spotDepthP(b).compareTo(spotDepthP(a)));
  1385. // 精度聚合后买一 >= 卖一时,丢弃两端交叉档位
  1386. removeCrossingDepthLevels(aggregatedAsks, aggregatedBids);
  1387. final askTake = _bookMode == 2 ? 0 : (_bookMode == 1 ? n * 2 : n);
  1388. final bidTake = _bookMode == 1 ? 0 : (_bookMode == 2 ? n * 2 : n);
  1389. // 卖盘:取最接近成交价的 n 档(聚合后价高→价低序列的末尾 n 条)
  1390. final askSlice = askTake > 0 && aggregatedAsks.isNotEmpty
  1391. ? aggregatedAsks.sublist(math.max(0, aggregatedAsks.length - askTake))
  1392. : <Map<String, dynamic>>[];
  1393. // 展示须自上而下「价高→价低」(远离中间价 → 靠近卖一);若切片呈升序则反转
  1394. final List<Map<String, dynamic>> askRows = askSlice.length >= 2 &&
  1395. spotDepthP(askSlice.first) < spotDepthP(askSlice.last)
  1396. ? askSlice.reversed.toList()
  1397. : List<Map<String, dynamic>>.from(askSlice);
  1398. // 买盘:价高在前,首条即最优买 → 取前 n 档,首行紧贴中间价
  1399. final bidSlice = bidTake > 0
  1400. ? aggregatedBids.take(bidTake).toList()
  1401. : <Map<String, dynamic>>[];
  1402. final bidRows = bidSlice;
  1403. double maxQ = 0.001;
  1404. double totalAsk = 0, totalBid = 0;
  1405. for (final a in askRows) {
  1406. final q = spotDepthQ(a);
  1407. totalAsk += q;
  1408. if (q > maxQ) maxQ = q;
  1409. }
  1410. for (final b in bidRows) {
  1411. final q = spotDepthQ(b);
  1412. totalBid += q;
  1413. if (q > maxQ) maxQ = q;
  1414. }
  1415. final totalDepth = totalAsk + totalBid;
  1416. final bidRatio = totalDepth > 0 ? totalBid / totalDepth : 0.5;
  1417. final askRatio = 1.0 - bidRatio;
  1418. final labelStyle =
  1419. TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 11);
  1420. return Padding(
  1421. padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
  1422. child: Column(
  1423. crossAxisAlignment: CrossAxisAlignment.start,
  1424. mainAxisSize: MainAxisSize.max,
  1425. children: [
  1426. Row(
  1427. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  1428. children: [
  1429. Flexible(
  1430. child: Text(
  1431. l10n.priceUsdt,
  1432. overflow: TextOverflow.ellipsis,
  1433. style: labelStyle,
  1434. ),
  1435. ),
  1436. const SizedBox(width: 4),
  1437. Flexible(
  1438. child: Text(
  1439. l10n.amountLabel2(state.baseCoin),
  1440. overflow: TextOverflow.ellipsis,
  1441. textAlign: TextAlign.end,
  1442. style: labelStyle,
  1443. ),
  1444. ),
  1445. ],
  1446. ),
  1447. const SizedBox(height: 4),
  1448. if (_bookMode != 2)
  1449. for (var i = 0; i < askRows.length; i++)
  1450. spotDepthP(askRows[i]) > 0
  1451. ? _SpotBookRow(
  1452. key: ValueKey('ask_$i'),
  1453. isSell: true,
  1454. price: spotDepthP(askRows[i]),
  1455. qty: spotDepthQ(askRows[i]),
  1456. maxQ: maxQ,
  1457. pricePrecision: depthPrecision,
  1458. volumePrecision: state.volumePrecision,
  1459. rowHeight: widget.rowHeight,
  1460. onTap: widget.onPriceTap != null
  1461. ? () => widget.onPriceTap!(spotDepthP(askRows[i]))
  1462. : null,
  1463. )
  1464. : _SpotBookRowPlaceholder(
  1465. key: ValueKey('ask_ph_$i'),
  1466. rowHeight: widget.rowHeight,
  1467. ),
  1468. Padding(
  1469. padding: const EdgeInsets.symmetric(vertical: 5),
  1470. child: FittedBox(
  1471. fit: BoxFit.scaleDown,
  1472. alignment: Alignment.centerLeft,
  1473. child: Row(
  1474. mainAxisSize: MainAxisSize.min,
  1475. crossAxisAlignment: CrossAxisAlignment.end,
  1476. children: [
  1477. Text(
  1478. state.lastPriceStr != null
  1479. ? formatRawPrice(state.lastPriceStr!)
  1480. : formatPrice(state.lastPrice),
  1481. maxLines: 1,
  1482. overflow: TextOverflow.clip,
  1483. style: TextStyle(
  1484. color: cs.onSurface,
  1485. fontSize: 16,
  1486. fontWeight: FontWeight.w700,
  1487. fontFeatures: const [FontFeature.tabularFigures()],
  1488. ),
  1489. ),
  1490. const SizedBox(width: 4),
  1491. Text(
  1492. formatFiatPrice(state.lastPrice),
  1493. maxLines: 1,
  1494. overflow: TextOverflow.clip,
  1495. style: TextStyle(
  1496. color: cs.onSurface.withAlpha(153),
  1497. fontSize: 10,
  1498. fontFeatures: const [FontFeature.tabularFigures()],
  1499. ),
  1500. ),
  1501. ],
  1502. ),
  1503. ),
  1504. ),
  1505. if (_bookMode != 1)
  1506. for (var i = 0; i < bidRows.length; i++)
  1507. spotDepthP(bidRows[i]) > 0
  1508. ? _SpotBookRow(
  1509. key: ValueKey('bid_$i'),
  1510. isSell: false,
  1511. price: spotDepthP(bidRows[i]),
  1512. qty: spotDepthQ(bidRows[i]),
  1513. maxQ: maxQ,
  1514. pricePrecision: depthPrecision,
  1515. volumePrecision: state.volumePrecision,
  1516. rowHeight: widget.rowHeight,
  1517. onTap: widget.onPriceTap != null
  1518. ? () => widget.onPriceTap!(spotDepthP(bidRows[i]))
  1519. : null,
  1520. )
  1521. : _SpotBookRowPlaceholder(
  1522. key: ValueKey('bid_ph_$i'),
  1523. rowHeight: widget.rowHeight,
  1524. ),
  1525. // 不用 Spacer,留白留在底部
  1526. const SizedBox(height: 6),
  1527. ClipRRect(
  1528. borderRadius: BorderRadius.circular(2),
  1529. child: Row(
  1530. children: [
  1531. Flexible(
  1532. flex: (bidRatio * 100).round().clamp(1, 99),
  1533. child: Container(
  1534. height: 4,
  1535. color: AppColors.rise.withAlpha(180),
  1536. ),
  1537. ),
  1538. Flexible(
  1539. flex: (askRatio * 100).round().clamp(1, 99),
  1540. child: Container(
  1541. height: 4,
  1542. color: AppColors.fall.withAlpha(180),
  1543. ),
  1544. ),
  1545. ],
  1546. ),
  1547. ),
  1548. Row(
  1549. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  1550. children: [
  1551. Text(
  1552. '${(bidRatio * 100).toStringAsFixed(2)}%',
  1553. style: const TextStyle(color: AppColors.rise, fontSize: 10),
  1554. ),
  1555. Text(
  1556. '${(askRatio * 100).toStringAsFixed(2)}%',
  1557. style: const TextStyle(color: AppColors.fall, fontSize: 10),
  1558. ),
  1559. ],
  1560. ),
  1561. const SizedBox(height: 2),
  1562. Row(
  1563. children: [
  1564. GestureDetector(
  1565. onTap: () => _showDepthStepSheet(context),
  1566. child: Container(
  1567. height: 20,
  1568. padding: const EdgeInsets.symmetric(horizontal: 4),
  1569. alignment: Alignment.center,
  1570. decoration: BoxDecoration(
  1571. border: Border.all(color: cs.outline.withAlpha(80)),
  1572. borderRadius: BorderRadius.circular(4),
  1573. ),
  1574. child: Text(
  1575. _precisionLabel(depthPrecision),
  1576. style: TextStyle(
  1577. color: cs.onSurface.withAlpha(180),
  1578. fontSize: 10,
  1579. fontWeight: FontWeight.w600,
  1580. ),
  1581. ),
  1582. ),
  1583. ),
  1584. const SizedBox(width: 4),
  1585. const Spacer(),
  1586. GestureDetector(
  1587. onTap: () => setState(() => _bookMode = (_bookMode + 1) % 3),
  1588. child: Container(
  1589. width: 28,
  1590. height: 20,
  1591. padding:
  1592. const EdgeInsets.symmetric(horizontal: 3, vertical: 2),
  1593. decoration: BoxDecoration(
  1594. border: Border.all(color: cs.outline.withAlpha(80)),
  1595. borderRadius: BorderRadius.circular(4),
  1596. ),
  1597. child: _SpotBookModeIcon(mode: _bookMode),
  1598. ),
  1599. ),
  1600. ],
  1601. ),
  1602. ],
  1603. ),
  1604. );
  1605. }
  1606. }
  1607. /// 与合约页订单簿模式图标一致
  1608. class _SpotBookModeIcon extends StatelessWidget {
  1609. const _SpotBookModeIcon({required this.mode});
  1610. final int mode;
  1611. @override
  1612. Widget build(BuildContext context) {
  1613. const sellColor = AppColors.fall;
  1614. const buyColor = AppColors.rise;
  1615. const emptyColor = Color(0xFFCCCCCC);
  1616. const lineH = 2.0;
  1617. const gap = 1.5;
  1618. Widget line(Color color) => Container(
  1619. height: lineH,
  1620. decoration: BoxDecoration(
  1621. color: color,
  1622. borderRadius: BorderRadius.circular(1),
  1623. ),
  1624. );
  1625. final List<Widget> lines;
  1626. if (mode == 0) {
  1627. lines = [
  1628. line(sellColor),
  1629. SizedBox(height: gap),
  1630. line(sellColor),
  1631. SizedBox(height: gap),
  1632. line(buyColor),
  1633. SizedBox(height: gap),
  1634. line(buyColor),
  1635. ];
  1636. } else if (mode == 1) {
  1637. lines = [
  1638. line(sellColor),
  1639. SizedBox(height: gap),
  1640. line(sellColor),
  1641. SizedBox(height: gap),
  1642. line(sellColor),
  1643. SizedBox(height: gap),
  1644. line(emptyColor),
  1645. ];
  1646. } else {
  1647. lines = [
  1648. line(emptyColor),
  1649. SizedBox(height: gap),
  1650. line(buyColor),
  1651. SizedBox(height: gap),
  1652. line(buyColor),
  1653. SizedBox(height: gap),
  1654. line(buyColor),
  1655. ];
  1656. }
  1657. return Column(
  1658. mainAxisAlignment: MainAxisAlignment.center,
  1659. crossAxisAlignment: CrossAxisAlignment.stretch,
  1660. children: lines,
  1661. );
  1662. }
  1663. }
  1664. class _SpotBookRowPlaceholder extends StatelessWidget {
  1665. const _SpotBookRowPlaceholder({super.key, this.rowHeight = 22.0});
  1666. final double rowHeight;
  1667. @override
  1668. Widget build(BuildContext context) {
  1669. return AppShimmer(
  1670. child: SizedBox(
  1671. height: rowHeight,
  1672. child: Row(
  1673. children: [
  1674. Expanded(
  1675. child: Align(
  1676. alignment: Alignment.centerLeft,
  1677. child: shimmerBox(60, 10),
  1678. ),
  1679. ),
  1680. Expanded(
  1681. child: Align(
  1682. alignment: Alignment.centerRight,
  1683. child: shimmerBox(50, 10),
  1684. ),
  1685. ),
  1686. ],
  1687. ),
  1688. ),
  1689. );
  1690. }
  1691. }
  1692. class _SpotBookRow extends StatelessWidget {
  1693. const _SpotBookRow({
  1694. super.key,
  1695. required this.isSell,
  1696. required this.price,
  1697. required this.qty,
  1698. required this.maxQ,
  1699. required this.pricePrecision,
  1700. required this.volumePrecision,
  1701. required this.rowHeight,
  1702. this.onTap,
  1703. });
  1704. final bool isSell;
  1705. final double price;
  1706. final double qty;
  1707. final double maxQ;
  1708. final int pricePrecision;
  1709. final int volumePrecision;
  1710. final double rowHeight;
  1711. final VoidCallback? onTap;
  1712. @override
  1713. Widget build(BuildContext context) {
  1714. final cs = Theme.of(context).colorScheme;
  1715. final color = isSell ? AppColors.fall : AppColors.rise;
  1716. return GestureDetector(
  1717. onTap: onTap,
  1718. behavior: HitTestBehavior.opaque,
  1719. child: SizedBox(
  1720. height: rowHeight,
  1721. child: LayoutBuilder(
  1722. builder: (_, c) {
  1723. final ratio = (qty / (maxQ <= 0 ? 1 : maxQ)).clamp(0.0, 1.0);
  1724. return Stack(
  1725. children: [
  1726. Positioned(
  1727. right: 0,
  1728. top: 1,
  1729. bottom: 1,
  1730. child: AnimatedContainer(
  1731. duration: const Duration(milliseconds: 300),
  1732. curve: Curves.easeOut,
  1733. width: c.maxWidth * ratio,
  1734. decoration: BoxDecoration(
  1735. color: color.withAlpha(38),
  1736. borderRadius: BorderRadius.circular(2),
  1737. ),
  1738. ),
  1739. ),
  1740. Padding(
  1741. padding:
  1742. const EdgeInsets.symmetric(horizontal: 2, vertical: 1),
  1743. child: Row(
  1744. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  1745. children: [
  1746. Text(
  1747. price > 0
  1748. ? price.toStringAsFixed(pricePrecision)
  1749. : '--',
  1750. style: TextStyle(
  1751. color: color,
  1752. fontSize: 13,
  1753. fontWeight: FontWeight.w500,
  1754. fontFeatures: const [FontFeature.tabularFigures()],
  1755. ),
  1756. ),
  1757. Text(
  1758. qty > 0 ? qty.toStringAsFixed(volumePrecision) : '--',
  1759. style: TextStyle(
  1760. color: cs.onSurface,
  1761. fontSize: 13,
  1762. fontFeatures: const [FontFeature.tabularFigures()],
  1763. ),
  1764. ),
  1765. ],
  1766. ),
  1767. ),
  1768. ],
  1769. );
  1770. },
  1771. ),
  1772. ),
  1773. );
  1774. }
  1775. }
  1776. // ══════════════════════════════════════════════════════════════════════
  1777. // 底部:当前委托 / 资产
  1778. // ══════════════════════════════════════════════════════════════════════
  1779. // PageView 本身会 clip,所以 OverflowBox 超出部分不会显示。
  1780. class _MeasureSize extends StatefulWidget {
  1781. const _MeasureSize({required this.child, required this.onSize});
  1782. final Widget child;
  1783. final ValueChanged<double> onSize;
  1784. @override
  1785. State<_MeasureSize> createState() => _MeasureSizeState();
  1786. }
  1787. class _MeasureSizeState extends State<_MeasureSize> {
  1788. final _key = GlobalKey();
  1789. double? _last;
  1790. @override
  1791. Widget build(BuildContext context) {
  1792. WidgetsBinding.instance.addPostFrameCallback((_) {
  1793. if (!mounted) return;
  1794. final box = _key.currentContext?.findRenderObject() as RenderBox?;
  1795. if (box == null || !box.hasSize) return;
  1796. final h = box.size.height;
  1797. if (h != _last) {
  1798. _last = h;
  1799. widget.onSize(h);
  1800. }
  1801. });
  1802. return OverflowBox(
  1803. alignment: Alignment.topLeft,
  1804. minHeight: 0,
  1805. maxHeight: double.infinity,
  1806. child: KeyedSubtree(key: _key, child: widget.child),
  1807. );
  1808. }
  1809. }
  1810. class _SpotBottomSection extends ConsumerStatefulWidget {
  1811. const _SpotBottomSection({required this.symbol});
  1812. final String symbol;
  1813. @override
  1814. ConsumerState<_SpotBottomSection> createState() => _SpotBottomSectionState();
  1815. }
  1816. class _SpotBottomSectionState extends ConsumerState<_SpotBottomSection> {
  1817. late PageController _pageController;
  1818. bool _programmaticSwitch = false;
  1819. final _tabHeights = [420.0, 280.0]; // orders, assets
  1820. static int _tabToIndex(SpotTab tab) => tab == SpotTab.orders ? 0 : 1;
  1821. static SpotTab _indexToTab(int index) =>
  1822. index == 0 ? SpotTab.orders : SpotTab.assets;
  1823. @override
  1824. void initState() {
  1825. super.initState();
  1826. final initial = ref.read(spotProvider(widget.symbol)).activeTab;
  1827. _pageController = PageController(initialPage: _tabToIndex(initial));
  1828. }
  1829. @override
  1830. void dispose() {
  1831. _pageController.dispose();
  1832. super.dispose();
  1833. }
  1834. void _onTabTap(SpotTab tab) {
  1835. _programmaticSwitch = true;
  1836. ref.read(spotProvider(widget.symbol).notifier).setActiveTab(tab);
  1837. _pageController.animateToPage(
  1838. _tabToIndex(tab),
  1839. duration: const Duration(milliseconds: 280),
  1840. curve: Curves.easeOut,
  1841. );
  1842. }
  1843. @override
  1844. Widget build(BuildContext context) {
  1845. final cs = Theme.of(context).colorScheme;
  1846. final l10n = AppLocalizations.of(context)!;
  1847. final symbol = widget.symbol;
  1848. final provider = spotProvider(symbol);
  1849. final activeTab = ref.watch(provider.select((s) => s.activeTab));
  1850. final ordersCount =
  1851. ref.watch(provider.select((s) => s.displayOrders.length));
  1852. final isLoggedIn = ref.watch(isLoggedInProvider);
  1853. return Container(
  1854. decoration: BoxDecoration(
  1855. border: Border(top: BorderSide(color: cs.outline)),
  1856. ),
  1857. child: Column(
  1858. crossAxisAlignment: CrossAxisAlignment.stretch,
  1859. children: [
  1860. Padding(
  1861. padding: const EdgeInsets.symmetric(horizontal: 12),
  1862. child: Row(
  1863. children: [
  1864. _SpotBottomTab(
  1865. label: l10n.currentOrdersTab(ordersCount),
  1866. active: activeTab == SpotTab.orders,
  1867. onTap: () => _onTabTap(SpotTab.orders),
  1868. ),
  1869. const SizedBox(width: 16),
  1870. _SpotBottomTab(
  1871. label: l10n.assetsTab,
  1872. active: activeTab == SpotTab.assets,
  1873. onTap: () => _onTabTap(SpotTab.assets),
  1874. ),
  1875. const Spacer(),
  1876. AnimatedSwitcher(
  1877. duration: const Duration(milliseconds: 200),
  1878. child: activeTab == SpotTab.orders && isLoggedIn
  1879. ? _OrdersToolbar(
  1880. key: const ValueKey('orders_toolbar'),
  1881. symbol: symbol,
  1882. )
  1883. : const SizedBox.shrink(key: ValueKey('no_toolbar')),
  1884. ),
  1885. GestureDetector(
  1886. onTap: () {
  1887. if (!isLoggedIn) {
  1888. context.push('/login');
  1889. return;
  1890. }
  1891. final n = ref.read(spotProvider(symbol).notifier);
  1892. n.stopPolling();
  1893. context.push('/spot/$symbol/history').then((_) {
  1894. if (context.mounted) n.resumePolling();
  1895. });
  1896. },
  1897. child: Icon(Icons.access_time,
  1898. color: cs.onSurface.withAlpha(153), size: 18),
  1899. ),
  1900. ],
  1901. ),
  1902. ),
  1903. // 滑动内容区
  1904. SizedBox(
  1905. height: _tabHeights.reduce(math.max).clamp(100.0, 2000.0),
  1906. child: RepaintBoundary(
  1907. child: PageView(
  1908. controller: _pageController,
  1909. physics: const ClampingScrollPhysics(),
  1910. onPageChanged: (index) {
  1911. if (_programmaticSwitch) {
  1912. _programmaticSwitch = false;
  1913. return;
  1914. }
  1915. ref
  1916. .read(spotProvider(symbol).notifier)
  1917. .setActiveTab(_indexToTab(index));
  1918. },
  1919. children: [
  1920. _MeasureSize(
  1921. onSize: (h) {
  1922. if (_tabHeights[0] != h)
  1923. setState(() => _tabHeights[0] = h);
  1924. },
  1925. child: _SpotOrdersContent(symbol: symbol),
  1926. ),
  1927. _MeasureSize(
  1928. onSize: (h) {
  1929. if (_tabHeights[1] != h)
  1930. setState(() => _tabHeights[1] = h);
  1931. },
  1932. child: _SpotAssetsContent(symbol: symbol),
  1933. ),
  1934. ],
  1935. ),
  1936. ),
  1937. ),
  1938. ],
  1939. ),
  1940. );
  1941. }
  1942. }
  1943. class _OrdersToolbar extends ConsumerWidget {
  1944. const _OrdersToolbar({super.key, required this.symbol});
  1945. final String symbol;
  1946. @override
  1947. Widget build(BuildContext context, WidgetRef ref) {
  1948. final cs = Theme.of(context).colorScheme;
  1949. final l10n = AppLocalizations.of(context)!;
  1950. final provider = spotProvider(symbol);
  1951. final notifier = ref.read(provider.notifier);
  1952. final state = ref.watch(provider);
  1953. final orders = state.displayOrders;
  1954. final isLoggedIn = ref.watch(isLoggedInProvider);
  1955. if (!isLoggedIn || !orders.any((o) => o.isPending)) {
  1956. return const SizedBox.shrink();
  1957. }
  1958. return GestureDetector(
  1959. onTap: () async {
  1960. final err = await notifier.cancelAll();
  1961. if (!context.mounted) return;
  1962. if (err != null) {
  1963. showTopToast(
  1964. context,
  1965. message: _resolveSpotError(err, l10n),
  1966. backgroundColor: AppColors.fall,
  1967. );
  1968. } else {
  1969. showTopToast(
  1970. context,
  1971. message: l10n.cancelSuccess,
  1972. backgroundColor: AppColors.rise,
  1973. );
  1974. }
  1975. },
  1976. child: Container(
  1977. margin: const EdgeInsets.only(right: 8),
  1978. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
  1979. decoration: BoxDecoration(
  1980. color: cs.inverseSurface,
  1981. borderRadius: BorderRadius.circular(4),
  1982. ),
  1983. child: Text(
  1984. l10n.cancelAll,
  1985. style: TextStyle(
  1986. color: cs.onInverseSurface,
  1987. fontSize: 11,
  1988. fontWeight: FontWeight.w500,
  1989. ),
  1990. ),
  1991. ),
  1992. );
  1993. }
  1994. }
  1995. class _SpotOrdersContent extends ConsumerWidget {
  1996. const _SpotOrdersContent({required this.symbol});
  1997. final String symbol;
  1998. @override
  1999. Widget build(BuildContext context, WidgetRef ref) {
  2000. final cs = Theme.of(context).colorScheme;
  2001. final l10n = AppLocalizations.of(context)!;
  2002. final provider = spotProvider(symbol);
  2003. final notifier = ref.read(provider.notifier);
  2004. final state = ref.watch(provider);
  2005. final orders = state.displayOrders;
  2006. final isLoggedIn = ref.watch(isLoggedInProvider);
  2007. return Column(
  2008. crossAxisAlignment: CrossAxisAlignment.stretch,
  2009. children: [
  2010. Padding(
  2011. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
  2012. child: Row(
  2013. children: [
  2014. GestureDetector(
  2015. behavior: HitTestBehavior.opaque,
  2016. onTap: notifier.toggleHideOtherSymbols,
  2017. child: Row(
  2018. children: [
  2019. _SpotCheckBox(checked: state.hideOtherSymbols),
  2020. const SizedBox(width: 4),
  2021. Text(l10n.hideOtherSymbols,
  2022. style: TextStyle(
  2023. color: cs.onSurface.withAlpha(153), fontSize: 11)),
  2024. ],
  2025. ),
  2026. ),
  2027. ],
  2028. ),
  2029. ),
  2030. if (!isLoggedIn)
  2031. _LoginPlaceholder()
  2032. else if (orders.isEmpty)
  2033. _EmptyHint(text: l10n.noOpenOrders)
  2034. else
  2035. ListView.builder(
  2036. padding: EdgeInsets.zero,
  2037. shrinkWrap: true,
  2038. physics: const NeverScrollableScrollPhysics(),
  2039. itemCount: orders.length,
  2040. itemBuilder: (_, i) =>
  2041. _SpotOrderRow(symbol: symbol, order: orders[i]),
  2042. ),
  2043. ],
  2044. );
  2045. }
  2046. }
  2047. class _SpotAssetsContent extends ConsumerWidget {
  2048. const _SpotAssetsContent({required this.symbol});
  2049. final String symbol;
  2050. @override
  2051. Widget build(BuildContext context, WidgetRef ref) {
  2052. final provider = spotProvider(symbol);
  2053. final state = ref.watch(provider);
  2054. final isLoggedIn = ref.watch(isLoggedInProvider);
  2055. final l10n = AppLocalizations.of(context)!;
  2056. if (!isLoggedIn) return _LoginPlaceholder();
  2057. if (state.wallets.isEmpty) return _EmptyHint(text: l10n.noAssets);
  2058. return ListView.builder(
  2059. padding: EdgeInsets.zero,
  2060. shrinkWrap: true,
  2061. physics: const NeverScrollableScrollPhysics(),
  2062. itemCount: state.wallets.length,
  2063. itemBuilder: (_, i) => _SpotWalletRow(asset: state.wallets[i]),
  2064. );
  2065. }
  2066. }
  2067. class _SpotBottomTab extends StatelessWidget {
  2068. const _SpotBottomTab({
  2069. required this.label,
  2070. required this.active,
  2071. required this.onTap,
  2072. });
  2073. final String label;
  2074. final bool active;
  2075. final VoidCallback onTap;
  2076. @override
  2077. Widget build(BuildContext context) {
  2078. final cs = Theme.of(context).colorScheme;
  2079. final labelColor = active ? cs.onSurface : cs.onSurface.withAlpha(153);
  2080. return GestureDetector(
  2081. onTap: onTap,
  2082. behavior: HitTestBehavior.opaque,
  2083. child: Container(
  2084. padding: const EdgeInsets.symmetric(vertical: 10),
  2085. decoration: BoxDecoration(
  2086. border: Border(
  2087. bottom: BorderSide(
  2088. color: active ? AppColors.brand : Colors.transparent,
  2089. width: 2,
  2090. ),
  2091. ),
  2092. ),
  2093. child: Text(
  2094. label,
  2095. style: TextStyle(
  2096. color: labelColor,
  2097. fontSize: 13,
  2098. fontWeight: active ? FontWeight.w600 : FontWeight.w400,
  2099. ),
  2100. ),
  2101. ),
  2102. );
  2103. }
  2104. }
  2105. class _SpotCheckBox extends StatelessWidget {
  2106. const _SpotCheckBox({required this.checked});
  2107. final bool checked;
  2108. @override
  2109. Widget build(BuildContext context) {
  2110. final cs = Theme.of(context).colorScheme;
  2111. return Container(
  2112. width: 14,
  2113. height: 14,
  2114. decoration: BoxDecoration(
  2115. borderRadius: BorderRadius.circular(3),
  2116. color: checked ? AppColors.brand : Colors.transparent,
  2117. border: Border.all(
  2118. color: checked ? AppColors.brand : cs.onSurface.withAlpha(153),
  2119. width: 1,
  2120. ),
  2121. ),
  2122. child: checked
  2123. ? const Icon(Icons.check, size: 10, color: Colors.white)
  2124. : null,
  2125. );
  2126. }
  2127. }
  2128. class _SpotOrderRow extends ConsumerWidget {
  2129. const _SpotOrderRow({required this.symbol, required this.order});
  2130. final String symbol;
  2131. final SpotOrder order;
  2132. @override
  2133. Widget build(BuildContext context, WidgetRef ref) {
  2134. final cs = Theme.of(context).colorScheme;
  2135. final l10n = AppLocalizations.of(context)!;
  2136. final notifier = ref.read(spotProvider(symbol).notifier);
  2137. final pricePre =
  2138. ref.watch(spotProvider(symbol).select((s) => s.pricePrecision));
  2139. final volPre =
  2140. ref.watch(spotProvider(symbol).select((s) => s.volumePrecision));
  2141. final sideColor =
  2142. order.side == SpotSide.buy ? AppColors.rise : AppColors.fall;
  2143. final sideLabel =
  2144. order.side == SpotSide.buy ? l10n.buyAction : l10n.sellAction;
  2145. final typeLabel =
  2146. order.type == SpotOrderType.limit ? l10n.limitOrder : l10n.marketOrder;
  2147. final symDisplay = order.symbol.replaceAll('/', '');
  2148. final priceDisplay = order.type == SpotOrderType.market && order.price <= 0
  2149. ? l10n.marketPrice
  2150. : formatAmount(order.price, decimals: pricePre);
  2151. return Container(
  2152. padding: const EdgeInsets.fromLTRB(12, 10, 12, 10),
  2153. decoration: BoxDecoration(
  2154. border: Border(
  2155. bottom: BorderSide(
  2156. color: Theme.of(context).scaffoldBackgroundColor,
  2157. width: 6,
  2158. ),
  2159. ),
  2160. ),
  2161. child: Column(
  2162. crossAxisAlignment: CrossAxisAlignment.start,
  2163. children: [
  2164. Row(
  2165. children: [
  2166. Text(
  2167. symDisplay,
  2168. style: TextStyle(
  2169. color: cs.onSurface,
  2170. fontSize: 13,
  2171. fontWeight: FontWeight.w700,
  2172. ),
  2173. ),
  2174. const SizedBox(width: 8),
  2175. _Tag(text: typeLabel, color: cs.onSurface.withAlpha(180)),
  2176. const SizedBox(width: 4),
  2177. _Tag(text: sideLabel, color: sideColor, filled: true),
  2178. const Spacer(),
  2179. if (order.isPending)
  2180. GestureDetector(
  2181. onTap: () async {
  2182. final err = await notifier.cancelOrder(order);
  2183. if (!context.mounted) return;
  2184. if (err != null) {
  2185. showTopToast(context,
  2186. message: _resolveSpotError(err, l10n),
  2187. backgroundColor: AppColors.fall);
  2188. } else {
  2189. showTopToast(context,
  2190. message: l10n.cancelSuccess,
  2191. backgroundColor: AppColors.rise);
  2192. }
  2193. },
  2194. child: Container(
  2195. padding:
  2196. const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
  2197. decoration: BoxDecoration(
  2198. border: Border.all(color: cs.outline.withAlpha(80)),
  2199. borderRadius: BorderRadius.circular(4),
  2200. ),
  2201. child: Text(
  2202. l10n.cancelLabel,
  2203. style: TextStyle(color: cs.onSurface, fontSize: 11),
  2204. ),
  2205. ),
  2206. ),
  2207. ],
  2208. ),
  2209. const SizedBox(height: 8),
  2210. _spotDataLine(
  2211. context,
  2212. label:
  2213. '${l10n.orderPriceLabel}(${_baseCoin(order.symbol, quote: true)})',
  2214. value: priceDisplay,
  2215. ),
  2216. const SizedBox(height: 3),
  2217. _spotDataLine(
  2218. context,
  2219. label: '${l10n.tradedDealAmount}(${_baseCoin(order.symbol)})',
  2220. value:
  2221. '${formatAmount(order.tradedAmount, decimals: volPre)} / ${formatAmount(order.amount, decimals: volPre)}',
  2222. ),
  2223. if (order.createTime != null) ...[
  2224. const SizedBox(height: 3),
  2225. _spotDataLine(
  2226. context,
  2227. label: l10n.orderTime,
  2228. value: DateFormat('yyyy-MM-dd HH:mm:ss')
  2229. .format(order.createTime!.toLocal()),
  2230. ),
  2231. ],
  2232. ],
  2233. ),
  2234. );
  2235. }
  2236. }
  2237. String _baseCoin(String symbol, {bool quote = false}) {
  2238. final s = symbol.replaceAll('/', '').toUpperCase();
  2239. const quotes = ['USDT', 'USDC', 'BUSD', 'TUSD'];
  2240. for (final q in quotes) {
  2241. if (s.endsWith(q) && s.length > q.length) {
  2242. return quote ? q : s.substring(0, s.length - q.length);
  2243. }
  2244. }
  2245. return quote ? 'USDT' : s;
  2246. }
  2247. Widget _spotDataLine(
  2248. BuildContext context, {
  2249. required String label,
  2250. required String value,
  2251. }) {
  2252. final cs = Theme.of(context).colorScheme;
  2253. return Row(
  2254. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  2255. children: [
  2256. Text(
  2257. label,
  2258. style: TextStyle(color: cs.onSurface.withAlpha(153), fontSize: 11),
  2259. ),
  2260. Text(
  2261. value,
  2262. style: TextStyle(
  2263. color: cs.onSurface,
  2264. fontSize: 12,
  2265. fontFeatures: const [FontFeature.tabularFigures()],
  2266. ),
  2267. ),
  2268. ],
  2269. );
  2270. }
  2271. class _SpotWalletRow extends ConsumerWidget {
  2272. const _SpotWalletRow({required this.asset});
  2273. final SpotWalletAsset asset;
  2274. @override
  2275. Widget build(BuildContext context, WidgetRef ref) {
  2276. final cs = Theme.of(context).colorScheme;
  2277. final l10n = AppLocalizations.of(context)!;
  2278. // 与「资产 → 现货」完全一致:lookupSpotCoinConfig / spotCoinIconUrl
  2279. final mapState = ref.watch(spotCoinCacheProvider);
  2280. final coinCfg = lookupSpotCoinConfig(mapState, asset.coin);
  2281. final iconUrl = spotCoinIconUrl(mapState, asset.coin);
  2282. final decimals = coinCfg?.assetDisplayDecimals ?? 2;
  2283. return Container(
  2284. padding: const EdgeInsets.fromLTRB(12, 14, 12, 14),
  2285. decoration: BoxDecoration(
  2286. border: Border(
  2287. bottom: BorderSide(color: cs.outline.withAlpha(40), width: 0.6),
  2288. ),
  2289. ),
  2290. child: Column(
  2291. crossAxisAlignment: CrossAxisAlignment.start,
  2292. children: [
  2293. // 币种图标 + 名称
  2294. Row(
  2295. children: [
  2296. CoinIcon(
  2297. symbol: asset.coin,
  2298. iconUrl: iconUrl,
  2299. size: 40,
  2300. shape: BoxShape.circle,
  2301. ),
  2302. const SizedBox(width: 10),
  2303. Text(asset.coin,
  2304. style: TextStyle(
  2305. color: cs.onSurface,
  2306. fontSize: 16,
  2307. fontWeight: FontWeight.w700)),
  2308. ],
  2309. ),
  2310. const SizedBox(height: 14),
  2311. // 三列数据:资产余额 / 可用 / 不可用
  2312. Row(
  2313. children: [
  2314. Expanded(
  2315. child: _WalletCell(
  2316. label: l10n.assetBalance,
  2317. value: formatAmount(asset.total, decimals: decimals),
  2318. align: CrossAxisAlignment.start,
  2319. ),
  2320. ),
  2321. Expanded(
  2322. child: _WalletCell(
  2323. label: l10n.availableLabel,
  2324. value: formatAmount(asset.balance, decimals: decimals),
  2325. align: CrossAxisAlignment.center,
  2326. ),
  2327. ),
  2328. Expanded(
  2329. child: _WalletCell(
  2330. label: l10n.unavailableLabel,
  2331. value: formatAmount(asset.frozenBalance, decimals: decimals),
  2332. align: CrossAxisAlignment.end,
  2333. ),
  2334. ),
  2335. ],
  2336. ),
  2337. ],
  2338. ),
  2339. );
  2340. }
  2341. }
  2342. class _WalletCell extends StatelessWidget {
  2343. const _WalletCell(
  2344. {required this.label, required this.value, required this.align});
  2345. final String label;
  2346. final String value;
  2347. final CrossAxisAlignment align;
  2348. @override
  2349. Widget build(BuildContext context) {
  2350. final cs = Theme.of(context).colorScheme;
  2351. final textAlign = align == CrossAxisAlignment.start
  2352. ? TextAlign.left
  2353. : align == CrossAxisAlignment.end
  2354. ? TextAlign.right
  2355. : TextAlign.center;
  2356. return Column(
  2357. crossAxisAlignment: align,
  2358. children: [
  2359. Text(label,
  2360. style: TextStyle(color: cs.onSurface.withAlpha(140), fontSize: 11),
  2361. textAlign: textAlign),
  2362. const SizedBox(height: 4),
  2363. Text(value,
  2364. style: TextStyle(
  2365. color: cs.onSurface,
  2366. fontSize: 13,
  2367. fontWeight: FontWeight.w600,
  2368. fontFeatures: const [FontFeature.tabularFigures()],
  2369. ),
  2370. textAlign: textAlign),
  2371. ],
  2372. );
  2373. }
  2374. }
  2375. class _Tag extends StatelessWidget {
  2376. const _Tag({required this.text, required this.color, this.filled = false});
  2377. final String text;
  2378. final Color color;
  2379. final bool filled;
  2380. @override
  2381. Widget build(BuildContext context) {
  2382. final isDark = Theme.of(context).brightness == Brightness.dark;
  2383. return Container(
  2384. padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
  2385. decoration: BoxDecoration(
  2386. color: filled ? color.withAlpha(isDark ? 55 : 35) : Colors.transparent,
  2387. border: Border.all(color: color.withAlpha(160), width: 0.5),
  2388. borderRadius: BorderRadius.circular(3),
  2389. ),
  2390. child: Text(
  2391. text,
  2392. style: TextStyle(
  2393. color: color,
  2394. fontSize: 9,
  2395. fontWeight: FontWeight.w500,
  2396. ),
  2397. ),
  2398. );
  2399. }
  2400. }
  2401. class _EmptyHint extends StatelessWidget {
  2402. const _EmptyHint({required this.text});
  2403. final String text;
  2404. @override
  2405. Widget build(BuildContext context) {
  2406. final cs = Theme.of(context).colorScheme;
  2407. return Container(
  2408. padding: const EdgeInsets.symmetric(vertical: 36),
  2409. alignment: Alignment.center,
  2410. child: Text(text,
  2411. style: TextStyle(color: cs.onSurface.withAlpha(140), fontSize: 13)),
  2412. );
  2413. }
  2414. }
  2415. class _LoginPlaceholder extends StatelessWidget {
  2416. @override
  2417. Widget build(BuildContext context) {
  2418. final cs = Theme.of(context).colorScheme;
  2419. final l10n = AppLocalizations.of(context)!;
  2420. return Container(
  2421. padding: const EdgeInsets.symmetric(vertical: 36),
  2422. alignment: Alignment.center,
  2423. child: Column(
  2424. children: [
  2425. Text(l10n.loginPrompt,
  2426. style:
  2427. TextStyle(color: cs.onSurface.withAlpha(140), fontSize: 13)),
  2428. const SizedBox(height: 10),
  2429. OutlinedButton(
  2430. onPressed: () => context.push('/login'),
  2431. child: Text(l10n.loginText),
  2432. ),
  2433. ],
  2434. ),
  2435. );
  2436. }
  2437. }
  2438. // ══════════════════════════════════════════════════════════════════════
  2439. // 输入精度限制
  2440. // ══════════════════════════════════════════════════════════════════════
  2441. class _PrecisionInputFormatter extends TextInputFormatter {
  2442. _PrecisionInputFormatter(this.decimalRange);
  2443. final int decimalRange;
  2444. @override
  2445. TextEditingValue formatEditUpdate(
  2446. TextEditingValue oldValue, TextEditingValue newValue) {
  2447. final t = newValue.text;
  2448. if (t.isEmpty) return newValue;
  2449. if (decimalRange <= 0) {
  2450. // 整数:保留数字
  2451. if (RegExp(r'^[0-9]+$').hasMatch(t)) return newValue;
  2452. return oldValue;
  2453. }
  2454. final pattern = RegExp(r'^\d*\.?\d{0,' + decimalRange.toString() + r'}$');
  2455. if (pattern.hasMatch(t)) return newValue;
  2456. return oldValue;
  2457. }
  2458. }
  2459. // ══════════════════════════════════════════════════════════════════════
  2460. // 骨架屏
  2461. // ══════════════════════════════════════════════════════════════════════
  2462. class _SpotShimmer extends StatelessWidget {
  2463. const _SpotShimmer();
  2464. @override
  2465. Widget build(BuildContext context) {
  2466. return AppShimmer(
  2467. child: SingleChildScrollView(
  2468. physics: const NeverScrollableScrollPhysics(),
  2469. child: Column(
  2470. children: [
  2471. // 上半:左侧下单区 + 右侧盘口
  2472. SizedBox(
  2473. height: 400,
  2474. child: Row(
  2475. crossAxisAlignment: CrossAxisAlignment.stretch,
  2476. children: [
  2477. // 左侧下单区骨架
  2478. Expanded(
  2479. flex: 55,
  2480. child: Padding(
  2481. padding: const EdgeInsets.all(12),
  2482. child: Column(
  2483. crossAxisAlignment: CrossAxisAlignment.start,
  2484. children: [
  2485. // 买/卖 tab
  2486. Row(children: [
  2487. shimmerBox(70, 30, radius: 6),
  2488. const SizedBox(width: 8),
  2489. shimmerBox(70, 30, radius: 6),
  2490. ]),
  2491. const SizedBox(height: 12),
  2492. // 价格输入框
  2493. shimmerFill(44, radius: 8),
  2494. const SizedBox(height: 10),
  2495. // 数量输入框
  2496. shimmerFill(44, radius: 8),
  2497. const SizedBox(height: 10),
  2498. // 滑块
  2499. shimmerFill(20, radius: 10),
  2500. const SizedBox(height: 16),
  2501. // 下单按钮
  2502. shimmerFill(44, radius: 8),
  2503. const SizedBox(height: 16),
  2504. // 可用/可买数据行
  2505. ...List.generate(
  2506. 3,
  2507. (_) => Padding(
  2508. padding: const EdgeInsets.only(bottom: 8),
  2509. child: Row(
  2510. mainAxisAlignment:
  2511. MainAxisAlignment.spaceBetween,
  2512. children: [
  2513. shimmerBox(60, 11),
  2514. shimmerBox(70, 11),
  2515. ],
  2516. ),
  2517. )),
  2518. ],
  2519. ),
  2520. ),
  2521. ),
  2522. // 右侧盘口骨架
  2523. Expanded(
  2524. flex: 45,
  2525. child: Padding(
  2526. padding: const EdgeInsets.symmetric(
  2527. horizontal: 8, vertical: 12),
  2528. child: Column(
  2529. children: [
  2530. // 盘口 header
  2531. Row(
  2532. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  2533. children: [
  2534. shimmerBox(40, 11),
  2535. shimmerBox(60, 11),
  2536. ],
  2537. ),
  2538. const SizedBox(height: 8),
  2539. // 盘口行
  2540. ...List.generate(
  2541. 12,
  2542. (_) => Padding(
  2543. padding:
  2544. const EdgeInsets.symmetric(vertical: 4),
  2545. child: Row(
  2546. children: [
  2547. Expanded(
  2548. child: shimmerBox(
  2549. double.infinity, 11)),
  2550. const SizedBox(width: 6),
  2551. Expanded(
  2552. child: shimmerBox(
  2553. double.infinity, 11)),
  2554. ],
  2555. ),
  2556. )),
  2557. ],
  2558. ),
  2559. ),
  2560. ),
  2561. ],
  2562. ),
  2563. ),
  2564. // 下半:委托/资产区
  2565. Padding(
  2566. padding: const EdgeInsets.all(12),
  2567. child: Column(
  2568. crossAxisAlignment: CrossAxisAlignment.start,
  2569. children: [
  2570. // tab 行
  2571. Row(children: [
  2572. shimmerBox(80, 28, radius: 6),
  2573. const SizedBox(width: 8),
  2574. shimmerBox(60, 28, radius: 6),
  2575. ]),
  2576. const SizedBox(height: 16),
  2577. Center(child: shimmerBox(120, 14)),
  2578. ],
  2579. ),
  2580. ),
  2581. ],
  2582. ),
  2583. ),
  2584. );
  2585. }
  2586. }
  2587. class _SizeReporter extends StatefulWidget {
  2588. const _SizeReporter({required this.child, required this.onHeight});
  2589. final Widget child;
  2590. final ValueChanged<double> onHeight;
  2591. @override
  2592. State<_SizeReporter> createState() => _SizeReporterState();
  2593. }
  2594. class _SizeReporterState extends State<_SizeReporter> {
  2595. final _key = GlobalKey();
  2596. void _report() {
  2597. final ctx = _key.currentContext;
  2598. if (ctx == null) return;
  2599. final box = ctx.findRenderObject() as RenderBox?;
  2600. if (box == null || !box.hasSize) return;
  2601. widget.onHeight(box.size.height);
  2602. }
  2603. @override
  2604. Widget build(BuildContext context) {
  2605. WidgetsBinding.instance.addPostFrameCallback((_) => _report());
  2606. return KeyedSubtree(key: _key, child: widget.child);
  2607. }
  2608. }
  2609. // ══════════════════════════════════════════════════════════════════════
  2610. // 错误码 → 本地化
  2611. // ══════════════════════════════════════════════════════════════════════
  2612. String _resolveSpotError(String err, AppLocalizations l10n) {
  2613. switch (err) {
  2614. case 'errEnterPrice':
  2615. return l10n.enterPrice;
  2616. case 'errEnterAmount':
  2617. return l10n.errEnterAmount;
  2618. case 'errEnterTriggerPrice':
  2619. return l10n.enterTriggerPrice;
  2620. case 'errInvalidOrderId':
  2621. return l10n.errInvalidOrderId;
  2622. case 'errVolumeInsufficient':
  2623. return l10n.errVolumeInsufficient;
  2624. case 'errNoOrdersToCancel':
  2625. return l10n.errNoOrdersToCancel;
  2626. case 'errTimeout':
  2627. return l10n.errTimeout;
  2628. case 'errNetworkError':
  2629. return l10n.errNetworkError;
  2630. case 'errConditionalNotSupported':
  2631. return l10n.spotConditionalNotSupported;
  2632. default:
  2633. return err;
  2634. }
  2635. }