Skip to content

Commit

Permalink
Handled click in radar_chart.
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 19, 2021
1 parent ea72041 commit 7fafe31
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
18 changes: 17 additions & 1 deletion lib/src/chart/radar_chart/radar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,27 @@ class RadarTouchResponse extends BaseTouchResponse {
/// If touch happens, [RadarChart] processes it internally and passes out a [RadarTouchResponse]
/// that contains a [touchedSpot], it gives you information about the touched spot.
/// [touchInput] is the type of happened touch.
/// [clickHappened] will be true, if we detect a click event.
RadarTouchResponse(
RadarTouchedSpot? touchedSpot,
PointerEvent touchInput,
bool clickHappened,
) : touchedSpot = touchedSpot,
super(touchInput);
super(touchInput, clickHappened);

/// Copies current [RadarTouchResponse] to a new [RadarTouchResponse],
/// and replaces provided values.
RadarTouchResponse copyWith({
RadarTouchedSpot? touchedSpot,
PointerEvent? touchInput,
bool? clickHappened,
}) {
return RadarTouchResponse(
touchedSpot ?? this.touchedSpot,
touchInput ?? this.touchInput,
clickHappened ?? this.clickHappened,
);
}
}

/// It gives you information about the touched spot.
Expand Down
6 changes: 2 additions & 4 deletions lib/src/chart/radar_chart/radar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,9 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {
});
}

RadarTouchResponse handleTouch(
RadarTouchedSpot? handleTouch(
PointerEvent touchInput, Size size, PaintHolder<RadarChartData> holder) {
final touchedSpot =
_getNearestTouchSpot(size, touchInput.localPosition, dataSetsPosition, holder);
return RadarTouchResponse(touchedSpot, touchInput);
return _getNearestTouchSpot(size, touchInput.localPosition, dataSetsPosition, holder);
}

double _radarCenterY(Size size) => size.height / 2.0;
Expand Down
25 changes: 24 additions & 1 deletion lib/src/chart/radar_chart/radar_chart_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class RenderRadarChart extends RenderBox {
return PaintHolder(data, targetData, textScale);
}

RadarTouchedSpot? _lastTouchedSpot;

@override
void performLayout() {
size = computeDryLayout(constraints);
Expand All @@ -98,6 +100,27 @@ class RenderRadarChart extends RenderBox {

@override
void handleEvent(PointerEvent event, covariant BoxHitTestEntry entry) {
_touchCallback?.call(_painter.handleTouch(event, size, paintHolder));
if (_touchCallback == null) {
return;
}
var response = RadarTouchResponse(null, event, false);

var touchedSpot = _painter.handleTouch(event, size, paintHolder);
if (touchedSpot == null) {
_touchCallback!.call(response);
return;
}
response = response.copyWith(touchedSpot: touchedSpot,);

if (event is PointerDownEvent) {
_lastTouchedSpot = touchedSpot;
} else if (event is PointerUpEvent) {
if (_lastTouchedSpot == touchedSpot) {
response = response.copyWith(clickHappened: true);
}
_lastTouchedSpot = null;
}

_touchCallback!.call(response);
}
}

0 comments on commit 7fafe31

Please sign in to comment.