Skip to content

Commit

Permalink
Migrate line_chart.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 3, 2021
1 parent 0d550f8 commit 2c95bca
Showing 1 changed file with 50 additions and 77 deletions.
127 changes: 50 additions & 77 deletions lib/src/chart/line_chart/line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class LineChart extends ImplicitlyAnimatedWidget {
class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
/// we handle under the hood animations (implicit animations) via this tween,
/// it lerps between the old [LineChartData] to the new one.
LineChartDataTween _lineChartDataTween;
late LineChartDataTween _lineChartDataTween;

TouchHandler _touchHandler;
TouchHandler<LineTouchResponse>? _touchHandler;

final GlobalKey _chartKey = GlobalKey();

Expand All @@ -46,124 +46,101 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
return MouseRegion(
onEnter: (e) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

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

final LineTouchResponse response =
_touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final response = _touchHandler!.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize);
touchData.touchCallback?.call(response);
},
onHover: (e) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final LineTouchResponse response =
_touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final response =
_touchHandler!.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
child: GestureDetector(
onLongPressStart: (d) {
final chartSize = _getChartSize();
if (chartSize == null) {
if (chartSize == null || _touchHandler == null) {
return;
}

final LineTouchResponse 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 LineTouchResponse 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 LineTouchResponse response =
_touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final 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 LineTouchResponse 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 LineTouchResponse response =
_touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final 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 LineTouchResponse 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 LineTouchResponse response =
_touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize);
if (_canHandleTouch(response, touchData)) {
touchData.touchCallback(response);
}
final response =
_touchHandler!.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize);
touchData.touchCallback?.call(response);
},
child: CustomPaint(
key: _chartKey,
Expand All @@ -179,15 +156,7 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
);
}

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

LineChartData _withTouchedIndicators(LineChartData lineChartData) {
if (lineChartData == null) {
return lineChartData;
}

if (!lineChartData.lineTouchData.enabled || !lineChartData.lineTouchData.handleBuiltInTouches) {
return lineChartData;
}
Expand All @@ -203,9 +172,12 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
);
}

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 @@ -222,9 +194,7 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
}

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

if (touchResponse.touchInput is FlPanStart ||
touchResponse.touchInput is FlPanMoveUpdate ||
Expand Down Expand Up @@ -257,7 +227,10 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
_lineChartDataTween = visitor(
_lineChartDataTween,
_getData(),
(dynamic value) => LineChartDataTween(begin: value),
);
(dynamic value) => LineChartDataTween(
begin: value,
end: widget.data
),
) as LineChartDataTween;
}
}

0 comments on commit 2c95bca

Please sign in to comment.