feat(kline): 首次加载时让最新K线居中显示

- 新增 _scrollToCenter() 方法,计算让最新K线居中的滚动位置
- 初始化时调用 _scrollToCenter() 替代 _scrollToEnd()
- 如果K线总宽度小于屏幕宽度,不滚动,从左开始显示
- 保留 _scrollToEnd() 方法供刷新按钮等场景使用

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-19 18:03:23 -08:00
parent 8326f8c35c
commit a97e0b51b8
1 changed files with 28 additions and 3 deletions

View File

@ -83,13 +83,38 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
_candleWidth = idealWidth.clamp(_minCandleWidth, _maxCandleWidth);
_prevCandleWidth = _candleWidth;
//
_scrollToEnd();
// K 线
_scrollToCenter();
}
/// 使 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线宽度
final lastKlineCenter = (widget.klines.length - 0.5) * _candleWidth;
// = K线中心 -
final targetScroll = lastKlineCenter - _chartWidth / 2;
//
final maxScroll = totalWidth - _chartWidth;
_scrollX = targetScroll.clamp(0.0, maxScroll);
}
}
///
void _scrollToEnd() {
if (widget.klines.isEmpty || _chartWidth == 0) return;
//
final totalWidth = widget.klines.length * _candleWidth;
final maxScroll = math.max(0.0, totalWidth - _chartWidth);
_scrollX = maxScroll;