From c993fdfb2d0673a044e0c07b6077fd528d6a8559 Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 19 Jan 2026 21:09:45 -0800 Subject: [PATCH] =?UTF-8?q?fix(kline):=20=E5=88=87=E6=8D=A2=E5=91=A8?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=87=8D=E7=BD=AE=E8=B5=B7=E5=A7=8B=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=88=9D=E5=A7=8B=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E4=BD=8D=E7=BD=AE?= 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 | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 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 6ec0eb4e..02f65954 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 @@ -121,6 +121,7 @@ class _KlineChartWidgetState extends State { // 当 K 线数据变化时,重新初始化 if (oldWidget.klines.length != widget.klines.length) { _initialized = false; + _displayStartIndex = 0; // 重置起始索引 } } @@ -572,11 +573,21 @@ class _KlineChartWidgetState extends State { ); } - // 根据 _displayStartIndex 取数据 - final int endIndex = math.min(_displayStartIndex + fullScreenCount, widget.klines.length); + // 如果还未初始化,先计算正确的起始索引(让最新K线在中心) + int startIndex = _displayStartIndex; + if (!_initialized) { + final int halfScreenCount = (chartWidth / 2 / _candleWidth).ceil(); + startIndex = math.max(0, widget.klines.length - halfScreenCount); + } + + // 确保 startIndex 在有效范围内 + final int maxStartIndex = math.max(0, widget.klines.length - fullScreenCount); + startIndex = startIndex.clamp(0, maxStartIndex); + + final int endIndex = math.min(startIndex + fullScreenCount, widget.klines.length); return _VisibleData( - klines: widget.klines.sublist(_displayStartIndex, endIndex), - startIndex: _displayStartIndex, + klines: widget.klines.sublist(startIndex, endIndex), + startIndex: startIndex, candleWidth: _candleWidth, ); }