From a15ab7600f42fe34b572e63a66b681039d70d173 Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 19 Jan 2026 20:26:20 -0800 Subject: [PATCH] =?UTF-8?q?fix(kline):=20=E4=BF=AE=E6=AD=A3K=E7=BA=BF?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BB=8E=E5=B7=A6?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=8E=92=E5=88=97=EF=BC=8C=E6=9C=80=E6=96=B0?= =?UTF-8?q?K=E7=BA=BF=E5=B1=85=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.5 --- .../kline_chart/kline_chart_widget.dart | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/frontend/mining-app/lib/presentation/widgets/kline_chart/kline_chart_widget.dart b/frontend/mining-app/lib/presentation/widgets/kline_chart/kline_chart_widget.dart index a567eeef..32c9d7ec 100644 --- a/frontend/mining-app/lib/presentation/widgets/kline_chart/kline_chart_widget.dart +++ b/frontend/mining-app/lib/presentation/widgets/kline_chart/kline_chart_widget.dart @@ -70,39 +70,43 @@ class _KlineChartWidgetState extends State { // 初始化会在 LayoutBuilder 中完成 } - /// 初始化 K 线宽度,让所有 K 线占满屏幕 + /// 初始化 K 线宽度和滚动位置 + /// + /// 逻辑: + /// - K线从左边开始排列 + /// - 数据不够时,保持默认宽度,有多少显示多少 + /// - 数据量大时,计算滚动位置让最新K线在屏幕中心 void _initializeCandleWidth(double chartWidth) { if (_initialized || widget.klines.isEmpty || chartWidth == 0) return; _initialized = true; _chartWidth = chartWidth; - // 计算让所有 K 线占满屏幕的宽度 - final idealWidth = chartWidth / widget.klines.length; - // 限制在合理范围内 - _candleWidth = idealWidth.clamp(_minCandleWidth, _maxCandleWidth); + // 保持默认K线宽度,不根据数据量缩放 + // _candleWidth 保持初始值 8.0 _prevCandleWidth = _candleWidth; - // 首次加载时让最新 K 线居中显示 + // 计算滚动位置,让最新K线在屏幕中心 _scrollToCenter(); } - /// 滚动使最新 K 线居中显示 + /// 滚动使最新 K 线在屏幕中心显示 /// /// 计算逻辑: - /// - 如果 K 线总宽度小于屏幕宽度,不滚动,从左开始显示 - /// - 否则计算让最新 K 线中心位于屏幕中央的滚动位置 + /// - K线从左边开始排列 + /// - 数据不够时(总宽度 <= 屏幕宽度),不滚动,有多少显示多少 + /// - 数据量大时,计算滚动位置让最新K线在屏幕中心 void _scrollToCenter() { if (widget.klines.isEmpty || _chartWidth == 0) return; final totalWidth = widget.klines.length * _candleWidth; if (totalWidth <= _chartWidth) { - // K 线总宽度小于等于屏幕宽度,不需要滚动,从左开始显示 + // 数据不够,不滚动,从左开始显示 _scrollX = 0; } else { - // 计算让最新 K 线居中的滚动位置 - // 最新 K 线的中心位置 = (K线数量 - 0.5) * 单根K线宽度 + // 数据量大,计算让最新K线在屏幕中心的滚动位置 + // 最新K线的中心位置 = (K线数量 - 0.5) * 单根K线宽度 final lastKlineCenter = (widget.klines.length - 0.5) * _candleWidth; // 目标滚动位置 = 最新K线中心 - 屏幕宽度的一半 final targetScroll = lastKlineCenter - _chartWidth / 2;