diff --git a/lib/src/interactive_chart.dart b/lib/src/interactive_chart.dart index e48c70b..6f05619 100644 --- a/lib/src/interactive_chart.dart +++ b/lib/src/interactive_chart.dart @@ -2,6 +2,7 @@ import 'dart:math'; import 'package:flutter/gestures.dart'; import 'package:flutter/widgets.dart'; +import 'package:interactive_chart/src/x_axis_offset_details.dart'; import 'package:intl/intl.dart' as intl; import 'candle_data.dart'; @@ -60,6 +61,11 @@ class InteractiveChart extends StatefulWidget { /// This provides the width of a candlestick at the current zoom level. final ValueChanged? onCandleResize; + /// Optional event fired when the user moves the chart along the X axis. + /// + /// Provides X-axis offset details. + final ValueChanged? onXOffsetChanged; + const InteractiveChart({ Key? key, required this.candles, @@ -70,6 +76,7 @@ class InteractiveChart extends StatefulWidget { this.overlayInfo, this.onTap, this.onCandleResize, + this.onXOffsetChanged, }) : this.style = style ?? const ChartStyle(), assert(candles.length >= 3, "InteractiveChart requires 3 or more CandleData"), @@ -252,7 +259,8 @@ class _InteractiveChartState extends State { final zoomAdjustment = (currCount - prevCount) * candleWidth; final focalPointFactor = focalPoint.dx / w; startOffset -= zoomAdjustment * focalPointFactor; - startOffset = startOffset.clamp(0, _getMaxStartOffset(w, candleWidth)); + final maxStartOffset = _getMaxStartOffset(w, candleWidth); + startOffset = startOffset.clamp(0, maxStartOffset); // Fire candle width resize event if (candleWidth != _candleWidth) { widget.onCandleResize?.call(candleWidth); @@ -262,6 +270,13 @@ class _InteractiveChartState extends State { _candleWidth = candleWidth; _startOffset = startOffset; }); + + if (_prevStartOffset != startOffset) { + widget.onXOffsetChanged?.call(XAxisOffsetDetails( + offset: startOffset, + maxOffset: maxStartOffset, + )); + } } _handleResize(double w) { diff --git a/lib/src/x_axis_offset_details.dart b/lib/src/x_axis_offset_details.dart new file mode 100644 index 0000000..8d47683 --- /dev/null +++ b/lib/src/x_axis_offset_details.dart @@ -0,0 +1,25 @@ +class XAxisOffsetDetails { + XAxisOffsetDetails({ + required this.offset, + required this.maxOffset, + }); + + final double offset; + final double maxOffset; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is XAxisOffsetDetails && + runtimeType == other.runtimeType && + offset == other.offset && + maxOffset == other.maxOffset; + + @override + int get hashCode => offset.hashCode ^ maxOffset.hashCode; + + @override + String toString() { + return 'XAxisOffsetDetails{offset: $offset, maxOffset: $maxOffset}'; + } +}