fix(kline): 切换周期时重置起始索引,修复初始显示位置

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-19 21:09:45 -08:00
parent 5d22e34288
commit c993fdfb2d
1 changed files with 15 additions and 4 deletions

View File

@ -121,6 +121,7 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
// K 线
if (oldWidget.klines.length != widget.klines.length) {
_initialized = false;
_displayStartIndex = 0; //
}
}
@ -572,11 +573,21 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
);
}
// _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,
);
}