From b93a8111ae202022825c4c15a431ee892f2ce002 Mon Sep 17 00:00:00 2001 From: imaNNeoFighT Date: Wed, 11 Dec 2019 00:53:28 +0330 Subject: [PATCH] fixed calculating chart size for touch handling in *_chart.dart files, #126. --- CHANGELOG.md | 3 ++ lib/src/chart/bar_chart/bar_chart.dart | 49 +++++++++++++++--- lib/src/chart/line_chart/line_chart.dart | 49 +++++++++++++++--- lib/src/chart/pie_chart/pie_chart.dart | 48 ++++++++++++++--- .../chart/scatter_chart/scatter_chart.dart | 51 ++++++++++++++++--- 5 files changed, 175 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 194198f9d..6477d0aec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## +* fixed calculating size for handling touches bug, #126 + ## 0.5.2 * drawing titles using targetData instead of animating data, fixed issue #130. diff --git a/lib/src/chart/bar_chart/bar_chart.dart b/lib/src/chart/bar_chart/bar_chart.dart index cc5192eed..fc7e19623 100644 --- a/lib/src/chart/bar_chart/bar_chart.dart +++ b/lib/src/chart/bar_chart/bar_chart.dart @@ -31,18 +31,27 @@ class BarChartState extends AnimatedWidgetBaseState { Widget build(BuildContext context) { final BarChartData showingData = _getDate(); - final Size chartSize = _getChartSize(); final BarTouchData touchData = showingData.barTouchData; return GestureDetector( onLongPressStart: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) async { + onLongPressEnd: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -50,20 +59,35 @@ class BarChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () async { + onPanCancel: () { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) async { + onPanEnd: (DragEndDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { @@ -71,6 +95,11 @@ class BarChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -78,6 +107,11 @@ class BarChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -132,9 +166,12 @@ class BarChartState extends AnimatedWidgetBaseState { Size _getChartSize() { if (_chartKey.currentContext != null) { final RenderBox containerRenderBox = _chartKey.currentContext.findRenderObject(); - return containerRenderBox.constraints.biggest; + if (containerRenderBox.hasSize) { + return containerRenderBox.size; + } + return null; } else { - return getDefaultSize(context); + return null; } } diff --git a/lib/src/chart/line_chart/line_chart.dart b/lib/src/chart/line_chart/line_chart.dart index e1c0cdc7e..64e32b628 100644 --- a/lib/src/chart/line_chart/line_chart.dart +++ b/lib/src/chart/line_chart/line_chart.dart @@ -36,18 +36,27 @@ class LineChartState extends AnimatedWidgetBaseState { Widget build(BuildContext context) { final LineChartData showingData = _getDate(); - final Size chartSize = _getChartSize(); final LineTouchData touchData = showingData.lineTouchData; return GestureDetector( onLongPressStart: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) async { + onLongPressEnd: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -55,20 +64,35 @@ class LineChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () async { + onPanCancel: () { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) async { + onPanEnd: (DragEndDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { @@ -76,6 +100,11 @@ class LineChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -83,6 +112,11 @@ class LineChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -132,9 +166,12 @@ class LineChartState extends AnimatedWidgetBaseState { Size _getChartSize() { if (_chartKey.currentContext != null) { final RenderBox containerRenderBox = _chartKey.currentContext.findRenderObject(); - return containerRenderBox.constraints.biggest; + if (containerRenderBox.hasSize) { + return containerRenderBox.size; + } + return null; } else { - return getDefaultSize(context); + return null; } } diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index 0ee6ae951..6b90553d6 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -32,18 +32,26 @@ class PieChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { final PieChartData showingData = _getDate(); - final Size chartSize = _getChartSize(); final PieTouchData touchData = showingData.pieTouchData; return GestureDetector( onLongPressStart: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) async { + onLongPressEnd: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -51,20 +59,35 @@ class PieChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () async { + onPanCancel: () { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) async { + onPanEnd: (DragEndDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { @@ -72,6 +95,11 @@ class PieChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -79,6 +107,11 @@ class PieChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -108,9 +141,12 @@ class PieChartState extends AnimatedWidgetBaseState { Size _getChartSize() { if (_chartKey.currentContext != null) { final RenderBox containerRenderBox = _chartKey.currentContext.findRenderObject(); - return containerRenderBox.constraints.biggest; + if (containerRenderBox.hasSize) { + return containerRenderBox.size; + } + return null; } else { - return getDefaultSize(context); + return null; } } diff --git a/lib/src/chart/scatter_chart/scatter_chart.dart b/lib/src/chart/scatter_chart/scatter_chart.dart index 30ffdd094..79929c9d8 100644 --- a/lib/src/chart/scatter_chart/scatter_chart.dart +++ b/lib/src/chart/scatter_chart/scatter_chart.dart @@ -32,18 +32,27 @@ class ScatterChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { final ScatterChartData showingData = _getDate(); - final Size chartSize = _getChartSize(); final ScatterTouchData touchData = showingData.scatterTouchData; return GestureDetector( onLongPressStart: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) async { + onLongPressEnd: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -51,20 +60,35 @@ class ScatterChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () async { + onPanCancel: () { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) async { + onPanEnd: (DragEndDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { @@ -72,6 +96,11 @@ class ScatterChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -79,6 +108,11 @@ class ScatterChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { + final Size chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final ScatterTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { @@ -87,7 +121,7 @@ class ScatterChartState extends AnimatedWidgetBaseState { }, child: CustomPaint( key: _chartKey, - size: chartSize, + size: getDefaultSize(context), painter: ScatterChartPainter( _withTouchedIndicators(_scatterChartDataTween.evaluate(animation)), _withTouchedIndicators(showingData), @@ -122,9 +156,12 @@ class ScatterChartState extends AnimatedWidgetBaseState { Size _getChartSize() { if (_chartKey.currentContext != null) { final RenderBox containerRenderBox = _chartKey.currentContext.findRenderObject(); - return containerRenderBox.constraints.biggest; + if (containerRenderBox.hasSize) { + return containerRenderBox.size; + } + return null; } else { - return getDefaultSize(context); + return null; } }