Skip to content

Commit

Permalink
Migrate scatter_chart.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 3, 2021
1 parent ca52c3b commit 8a3491a
Showing 1 changed file with 35 additions and 53 deletions.
88 changes: 35 additions & 53 deletions lib/src/chart/scatter_chart/scatter_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class ScatterChart extends ImplicitlyAnimatedWidget {
class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
/// we handle under the hood animations (implicit animations) via this tween,
/// it lerps between the old [ScatterChartData] to the new one.
ScatterChartDataTween _scatterChartDataTween;
late ScatterChartDataTween _scatterChartDataTween;

TouchHandler<ScatterTouchResponse> _touchHandler;
TouchHandler<ScatterTouchResponse>? _touchHandler;

final GlobalKey _chartKey = GlobalKey();

Expand All @@ -43,84 +43,70 @@ class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
return GestureDetector(
onLongPressStart: (d) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final response = _touchHandler!.handleTouch(FlLongPressStart(d.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
onLongPressEnd: (d) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final response = _touchHandler!.handleTouch(FlLongPressEnd(d.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
onLongPressMoveUpdate: (d) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response =
_touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
_touchHandler!.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
onPanCancel: () {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response = _touchHandler?.handleTouch(
final response = _touchHandler!.handleTouch(
FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
touchData.touchCallback?.call(response);
},
onPanEnd: (DragEndDetails details) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response =
_touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
_touchHandler!.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize);
touchData.touchCallback?.call(response);
},
onPanDown: (DragDownDetails details) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final response = _touchHandler!.handleTouch(FlPanStart(details.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
onPanUpdate: (DragUpdateDetails details) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final response =
_touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
_touchHandler!.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
child: CustomPaint(
key: _chartKey,
Expand All @@ -139,15 +125,7 @@ class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
);
}

bool _canHandleTouch(ScatterTouchResponse response, ScatterTouchData touchData) {
return response != null && touchData != null && touchData.touchCallback != null;
}

ScatterChartData _withTouchedIndicators(ScatterChartData scatterChartData) {
if (scatterChartData == null) {
return scatterChartData;
}

if (!scatterChartData.scatterTouchData.enabled ||
!scatterChartData.scatterTouchData.handleBuiltInTouches) {
return scatterChartData;
Expand All @@ -158,9 +136,12 @@ class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
);
}

Size _getChartSize() {
final RenderBox containerRenderBox = _chartKey.currentContext?.findRenderObject();
if (containerRenderBox != null && containerRenderBox.hasSize) {
Size? _getChartSize() {
final containerRenderBox = _chartKey.currentContext?.findRenderObject();
if (containerRenderBox == null || containerRenderBox is! RenderBox) {
return null;
}
if (containerRenderBox.hasSize) {
return containerRenderBox.size;
}
return null;
Expand All @@ -177,9 +158,7 @@ class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
}

void _handleBuiltInTouch(ScatterTouchResponse touchResponse) {
if (widget.data.scatterTouchData.touchCallback != null) {
widget.data.scatterTouchData.touchCallback(touchResponse);
}
widget.data.scatterTouchData.touchCallback?.call(touchResponse);

if (touchResponse.touchInput is FlPanStart ||
touchResponse.touchInput is FlPanMoveUpdate ||
Expand All @@ -200,7 +179,10 @@ class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
_scatterChartDataTween = visitor(
_scatterChartDataTween,
_getData(),
(dynamic value) => ScatterChartDataTween(begin: value),
);
(dynamic value) => ScatterChartDataTween(
begin: value,
end: widget.data
),
) as ScatterChartDataTween;
}
}

0 comments on commit 8a3491a

Please sign in to comment.