fix(kline): 切换周期时重置起始索引,修复初始显示位置
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
5d22e34288
commit
c993fdfb2d
|
|
@ -121,6 +121,7 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
|
||||||
// 当 K 线数据变化时,重新初始化
|
// 当 K 线数据变化时,重新初始化
|
||||||
if (oldWidget.klines.length != widget.klines.length) {
|
if (oldWidget.klines.length != widget.klines.length) {
|
||||||
_initialized = false;
|
_initialized = false;
|
||||||
|
_displayStartIndex = 0; // 重置起始索引
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -572,11 +573,21 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据 _displayStartIndex 取数据
|
// 如果还未初始化,先计算正确的起始索引(让最新K线在中心)
|
||||||
final int endIndex = math.min(_displayStartIndex + fullScreenCount, widget.klines.length);
|
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(
|
return _VisibleData(
|
||||||
klines: widget.klines.sublist(_displayStartIndex, endIndex),
|
klines: widget.klines.sublist(startIndex, endIndex),
|
||||||
startIndex: _displayStartIndex,
|
startIndex: startIndex,
|
||||||
candleWidth: _candleWidth,
|
candleWidth: _candleWidth,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue