Skip to content

Commit

Permalink
Add draggable callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
G33kFreak committed Oct 1, 2023
1 parent 60e9e4a commit f42ba99
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
23 changes: 20 additions & 3 deletions lib/src/chart/line_chart/line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
/// but we need to keep the provided callback to notify it too.
BaseTouchCallback<LineTouchResponse>? _providedTouchCallback;

DragSpotUpdateFinishedCallback? _dragSpotUpdatedCallback;
DragSpotUpdateCallback? _dragSpotUpdateFinishedCallback;
DragSpotUpdateCallback? _dragSpotUpdateCallback;
DragSpotUpdateCallback? _dragSpotUpdateStartedCallback;

final List<ShowingTooltipIndicators> _showingTouchedTooltips = [];

Expand Down Expand Up @@ -112,7 +114,8 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
final lineTouchData = widget.data.lineTouchData;
if (lineTouchData.enabled && lineTouchData.handleBuiltInTouches) {
_providedTouchCallback = lineTouchData.touchCallback;
_dragSpotUpdatedCallback = lineTouchData.dragSpotUpdateFinishedCallback;
_dragSpotUpdateFinishedCallback =
lineTouchData.dragSpotUpdateFinishedCallback;
return widget.data.copyWith(
lineBarsData: _lineBarsData,
lineTouchData: widget.data.lineTouchData.copyWith(
Expand All @@ -137,7 +140,7 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
if (event is FlPanEndEvent || event is FlLongPressEnd) {
if (_draggingSpotIndexes != null) {
final (barIndex, spotIndex) = _draggingSpotIndexes!;
_dragSpotUpdatedCallback?.call(
_dragSpotUpdateFinishedCallback?.call(
UpdatedDragSpotsData(
barIndex,
spotIndex,
Expand All @@ -157,6 +160,13 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
_lineBarsData[barIndex].spots[spotIndex] =
touchResponse!.touchedAxesPoint!;
});
_dragSpotUpdateCallback?.call(
UpdatedDragSpotsData(
barIndex,
spotIndex,
_lineBarsData[barIndex].spots,
),
);
}

_providedTouchCallback?.call(event, touchResponse);
Expand Down Expand Up @@ -200,6 +210,13 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
_draggingSpotIndexes = (barIndex, spotIndex);
});
}
_dragSpotUpdateStartedCallback?.call(
UpdatedDragSpotsData(
barIndex,
spotIndex,
_lineBarsData[barIndex].spots,
),
);
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
MouseCursorResolver<LineTouchResponse>? mouseCursorResolver,
Duration? longPressDuration,
this.dragSpotUpdateFinishedCallback,
this.dragSpotUpdateCallback,
this.dragSpotUpdateStartedCallback,
this.touchTooltipData = const LineTouchTooltipData(),
this.getTouchedSpotIndicator = defaultTouchedIndicators,
this.touchSpotThreshold = 10,
Expand All @@ -1098,7 +1100,14 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
/// Configs of how touch indicator looks like.
final GetTouchedSpotIndicator getTouchedSpotIndicator;

final DragSpotUpdateFinishedCallback? dragSpotUpdateFinishedCallback;
/// Action when dragging of draggable spot is finished.
final DragSpotUpdateCallback? dragSpotUpdateFinishedCallback;

/// Action when draggable spot has been updated.
final DragSpotUpdateCallback? dragSpotUpdateCallback;

/// Action when dragging of draggable spot has been started
final DragSpotUpdateCallback? dragSpotUpdateStartedCallback;

/// Distance threshold to handle the touch event.
final double touchSpotThreshold;
Expand All @@ -1124,7 +1133,9 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
bool? enabled,
BaseTouchCallback<LineTouchResponse>? touchCallback,
MouseCursorResolver<LineTouchResponse>? mouseCursorResolver,
DragSpotUpdateFinishedCallback? dragSpotUpdateFinishedCallback,
DragSpotUpdateCallback? dragSpotUpdateFinishedCallback,
DragSpotUpdateCallback? dragSpotUpdateCallback,
DragSpotUpdateCallback? dragSpotUpdateStartedCallback,
Duration? longPressDuration,
LineTouchTooltipData? touchTooltipData,
GetTouchedSpotIndicator? getTouchedSpotIndicator,
Expand All @@ -1140,6 +1151,10 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
mouseCursorResolver: mouseCursorResolver ?? this.mouseCursorResolver,
dragSpotUpdateFinishedCallback:
dragSpotUpdateFinishedCallback ?? this.dragSpotUpdateFinishedCallback,
dragSpotUpdateCallback:
dragSpotUpdateCallback ?? this.dragSpotUpdateCallback,
dragSpotUpdateStartedCallback:
dragSpotUpdateStartedCallback ?? this.dragSpotUpdateStartedCallback,
longPressDuration: longPressDuration ?? this.longPressDuration,
touchTooltipData: touchTooltipData ?? this.touchTooltipData,
getTouchedSpotIndicator:
Expand Down Expand Up @@ -1167,6 +1182,8 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
getTouchLineStart,
getTouchLineEnd,
dragSpotUpdateFinishedCallback,
dragSpotUpdateCallback,
dragSpotUpdateStartedCallback,
];
}

Expand All @@ -1193,7 +1210,7 @@ typedef CalculateTouchDistance = double Function(
Offset spotPixelCoordinates,
);

typedef DragSpotUpdateFinishedCallback = void Function(UpdatedDragSpotsData);
typedef DragSpotUpdateCallback = void Function(UpdatedDragSpotsData);

/// Default distanceCalculator only considers distance on x axis
double _xDistance(Offset touchPoint, Offset spotPixelCoordinates) {
Expand Down

0 comments on commit f42ba99

Please sign in to comment.