Skip to content

Commit be5db97

Browse files
committed
[PR fluttercandies#25 feature] Add 'onXOffsetChanged' event
1 parent 01362dd commit be5db97

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/src/interactive_chart.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:math';
22

33
import 'package:flutter/gestures.dart';
44
import 'package:flutter/widgets.dart';
5+
import 'package:interactive_chart/src/x_axis_offset_details.dart';
56
import 'package:intl/intl.dart' as intl;
67

78
import '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) {

lib/src/x_axis_offset_details.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class XAxisOffsetDetails {
2+
XAxisOffsetDetails({
3+
required this.offset,
4+
required this.maxOffset,
5+
});
6+
7+
final double offset;
8+
final double maxOffset;
9+
10+
@override
11+
bool operator ==(Object other) =>
12+
identical(this, other) ||
13+
other is XAxisOffsetDetails &&
14+
runtimeType == other.runtimeType &&
15+
offset == other.offset &&
16+
maxOffset == other.maxOffset;
17+
18+
@override
19+
int get hashCode => offset.hashCode ^ maxOffset.hashCode;
20+
21+
@override
22+
String toString() {
23+
return 'XAxisOffsetDetails{offset: $offset, maxOffset: $maxOffset}';
24+
}
25+
}

0 commit comments

Comments
 (0)