@@ -2,6 +2,7 @@ import 'dart:math';
22
33import 'package:flutter/gestures.dart' ;
44import 'package:flutter/widgets.dart' ;
5+ import 'package:interactive_chart/src/x_axis_offset_details.dart' ;
56import 'package:intl/intl.dart' as intl;
67
78import 'candle_data.dart' ;
@@ -60,6 +61,11 @@ class InteractiveChart extends StatefulWidget {
6061 /// This provides the width of a candlestick at the current zoom level.
6162 final ValueChanged <double >? onCandleResize;
6263
64+ /// Optional event fired when the user moves the chart along the X axis.
65+ ///
66+ /// Provides X-axis offset details.
67+ final ValueChanged <XAxisOffsetDetails >? onXOffsetChanged;
68+
6369 const InteractiveChart ({
6470 Key ? key,
6571 required this .candles,
@@ -70,6 +76,7 @@ class InteractiveChart extends StatefulWidget {
7076 this .overlayInfo,
7177 this .onTap,
7278 this .onCandleResize,
79+ this .onXOffsetChanged,
7380 }) : this .style = style ?? const ChartStyle (),
7481 assert (candles.length >= 3 ,
7582 "InteractiveChart requires 3 or more CandleData" ),
@@ -252,7 +259,8 @@ class _InteractiveChartState extends State<InteractiveChart> {
252259 final zoomAdjustment = (currCount - prevCount) * candleWidth;
253260 final focalPointFactor = focalPoint.dx / w;
254261 startOffset -= zoomAdjustment * focalPointFactor;
255- startOffset = startOffset.clamp (0 , _getMaxStartOffset (w, candleWidth));
262+ final maxStartOffset = _getMaxStartOffset (w, candleWidth);
263+ startOffset = startOffset.clamp (0 , maxStartOffset);
256264 // Fire candle width resize event
257265 if (candleWidth != _candleWidth) {
258266 widget.onCandleResize? .call (candleWidth);
@@ -262,6 +270,13 @@ class _InteractiveChartState extends State<InteractiveChart> {
262270 _candleWidth = candleWidth;
263271 _startOffset = startOffset;
264272 });
273+
274+ if (_prevStartOffset != startOffset) {
275+ widget.onXOffsetChanged? .call (XAxisOffsetDetails (
276+ offset: startOffset,
277+ maxOffset: maxStartOffset,
278+ ));
279+ }
265280 }
266281
267282 _handleResize (double w) {
0 commit comments