fix(kline): simplify pan logic - scrollX now always controls view position

- Removed _userHasPanned flag and special handling
- _scrollX is initialized in _scrollToCenter() at startup
- Pan gesture directly modifies _scrollX
- _getVisibleData() always uses _scrollX to calculate visible range

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-19 21:35:31 -08:00
parent 7ae58e98e6
commit 48ba72ce89
1 changed files with 25 additions and 50 deletions

View File

@ -64,10 +64,6 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
bool _showCrossLine = false;
int _crossLineIndex = -1;
//
bool _userHasPanned = false;
int _panStartIndex = 0; //
@override
void initState() {
super.initState();
@ -98,25 +94,23 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
///
///
/// - K线从左边开始排列
/// - <=
/// - <=
/// - K线在屏幕中心
void _scrollToCenter() {
if (widget.klines.isEmpty || _chartWidth == 0) return;
final totalWidth = widget.klines.length * _candleWidth;
// K线
final int halfScreenCount = (_chartWidth / 2 / _candleWidth).ceil();
if (totalWidth <= _chartWidth) {
//
if (widget.klines.length <= halfScreenCount) {
//
_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);
// 使K线在屏幕中心
// startIndex = klines.length - halfScreenCount
// scrollX = startIndex * candleWidth
final int startIndex = widget.klines.length - halfScreenCount;
_scrollX = startIndex * _candleWidth;
}
}
@ -134,7 +128,6 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
// K 线
if (oldWidget.klines.length != widget.klines.length) {
_initialized = false;
_userHasPanned = false; //
_scrollX = 0;
}
}
@ -507,13 +500,6 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
_prevCandleWidth = _candleWidth;
_startFocalPoint = details.focalPoint;
_isScaling = details.pointerCount > 1;
// _scrollX
if (!_userHasPanned && !_isScaling) {
final int halfScreenCount = (_chartWidth / 2 / _candleWidth).ceil();
final int startIndex = math.max(0, widget.klines.length - halfScreenCount);
_scrollX = startIndex * _candleWidth;
}
_startScrollX = _scrollX;
}
@ -527,14 +513,13 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
final focalRatio = _startFocalPoint!.dx / _chartWidth;
final oldFocalIndex = (_startScrollX + _startFocalPoint!.dx) / _prevCandleWidth;
final newFocalX = oldFocalIndex * newCandleWidth;
_scrollX = (newFocalX - focalRatio * _chartWidth).clamp(
0.0,
math.max(0.0, widget.klines.length * newCandleWidth - _chartWidth),
);
// maxScroll
final int halfScreenCount = (_chartWidth / 2 / newCandleWidth).ceil();
final double maxScroll = math.max(0.0, (widget.klines.length - halfScreenCount) * newCandleWidth);
_scrollX = (newFocalX - focalRatio * _chartWidth).clamp(0.0, maxScroll);
}
_candleWidth = newCandleWidth;
_userHasPanned = true; //
} else if (_startFocalPoint != null) {
//
// dx > 0 dx < 0
@ -544,7 +529,6 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
final double maxScroll = math.max(0.0, (widget.klines.length - halfScreenCount) * _candleWidth);
// dx>0scrollX
_scrollX = (_startScrollX - dx).clamp(0.0, maxScroll);
_userHasPanned = true;
// DEBUG:
debugPrint('Pan: dx=$dx, scrollX=$_scrollX, maxScroll=$maxScroll, startIndex=${(_scrollX / _candleWidth).floor()}');
@ -592,6 +576,8 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
return _VisibleData(klines: [], startIndex: 0, candleWidth: _candleWidth);
}
// K线
final int fullScreenCount = (chartWidth / _candleWidth).ceil() + 1;
// K线
final int halfScreenCount = (chartWidth / 2 / _candleWidth).ceil();
@ -604,26 +590,15 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
);
}
//
if (_userHasPanned) {
// _scrollX
final int fullScreenCount = (chartWidth / _candleWidth).ceil() + 1;
final int startIndex = (_scrollX / _candleWidth).floor().clamp(0, widget.klines.length - 1);
final int endIndex = math.min(startIndex + fullScreenCount, widget.klines.length);
return _VisibleData(
klines: widget.klines.sublist(startIndex, endIndex),
startIndex: startIndex,
candleWidth: _candleWidth,
);
} else {
// halfScreenCount
final int startIndex = widget.klines.length - halfScreenCount;
return _VisibleData(
klines: widget.klines.sublist(startIndex),
startIndex: startIndex,
candleWidth: _candleWidth,
);
}
// _scrollX
final int startIndex = (_scrollX / _candleWidth).floor().clamp(0, math.max(0, widget.klines.length - 1));
final int endIndex = math.min(startIndex + fullScreenCount, widget.klines.length);
return _VisibleData(
klines: widget.klines.sublist(startIndex, endIndex),
startIndex: startIndex,
candleWidth: _candleWidth,
);
}
int _getVisibleCount() {