Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 707cfdb

Browse files
committed
added enableNormalTouch in TouchData and set true as default,
used to show or not show touch default interactions.
1 parent 4a9704c commit 707cfdb

File tree

10 files changed

+45
-28
lines changed

10 files changed

+45
-28
lines changed

example/lib/pie_chart/samples/pie_chart_sample2.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class PieChart2State extends State {
7070

7171
pieTouchedResultStreamController = StreamController();
7272
pieTouchedResultStreamController.stream.distinct().listen((details) {
73-
print(details);
7473
if (details == null) {
7574
return;
7675
}

lib/fl_chart.dart

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,13 @@ class _FlChartState extends State<FlChart> {
5757
Widget build(BuildContext context) {
5858
return GestureDetector(
5959
onLongPressStart: (d) {
60-
_touchInputNotifier.value = FlLongPressStart(
61-
_globalToLocal(context, d.globalPosition),
62-
);
60+
_touchInputNotifier.value = FlLongPressStart(d.localPosition);
6361
},
6462
onLongPressEnd: (d) {
65-
_touchInputNotifier.value = FlLongPressEnd(
66-
_globalToLocal(context, d.globalPosition),
67-
);
63+
_touchInputNotifier.value = FlLongPressEnd(d.localPosition);
6864
},
6965
onLongPressMoveUpdate: (d) {
70-
_touchInputNotifier.value = FlLongPressMoveUpdate(
71-
_globalToLocal(context, d.globalPosition),
72-
);
66+
_touchInputNotifier.value = FlLongPressMoveUpdate(d.localPosition);
7367
},
7468
onPanCancel: () {
7569
_touchInputNotifier.value = FlPanEnd(Offset.zero);
@@ -92,11 +86,6 @@ class _FlChartState extends State<FlChart> {
9286
);
9387
}
9488

95-
Offset _globalToLocal(BuildContext context, Offset globalPosition) {
96-
final RenderBox box = context.findRenderObject();
97-
return box.globalToLocal(globalPosition);
98-
}
99-
10089
@override
10190
void dispose() {
10291
super.dispose();

lib/src/chart/bar_chart/bar_chart_data.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,12 @@ class BarTouchData extends FlTouchData {
226226

227227
const BarTouchData({
228228
bool enabled = true,
229+
bool enableNormalTouch = true,
229230
this.touchTooltipData = const TouchTooltipData(),
230231
this.touchExtraThreshold = const EdgeInsets.all(4),
231232
this.allowTouchBarBackDraw = false,
232233
StreamSink<BarTouchResponse> touchResponseSink,
233-
}) : super(enabled, touchResponseSink);
234+
}) : super(enabled, touchResponseSink, enableNormalTouch);
234235

235236
}
236237

lib/src/chart/base/axis_chart/axis_chart_painter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ abstract class AxisChartPainter<D extends AxisChartData> extends BaseChartPainte
115115
}
116116

117117
void drawTouchTooltip(Canvas canvas, Size viewSize, TouchTooltipData tooltipData, List<TouchedSpot> touchedSpots) {
118-
119-
if (touchInputNotifier.value is FlLongPressEnd) {
118+
119+
if (!shouldDrawTouch()) {
120120
return;
121121
}
122122

lib/src/chart/base/base_chart/base_chart_data.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ class FlTouchData {
5151
/// determines enable or disable the touch in the chart
5252
final bool enabled;
5353

54+
/// determines that charts should respond to normal touch events or not
55+
final bool enableNormalTouch;
56+
5457
/// chart notifies the touch responses through this [StreamSink]
5558
/// in form of a [BaseTouchResponse] class
5659
final StreamSink<BaseTouchResponse> touchResponseSink;
5760

58-
const FlTouchData(this.enabled, this.touchResponseSink);
61+
const FlTouchData(this.enabled, this.touchResponseSink, this.enableNormalTouch);
5962
}
6063

6164

lib/src/chart/base/base_chart/base_chart_painter.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,22 @@ abstract class BaseChartPainter<D extends BaseChartData> extends CustomPainter {
100100
/// we should use this to offset our y axis when we drawing the chart,
101101
/// and the height space we can use to draw chart is[getChartUsableDrawSize.height]
102102
double getTopOffsetDrawSize() => 0;
103+
104+
/// checks that the touchInput is eligible to draw,
105+
/// and child painters can use this function to check then draw their default touch behaviors.
106+
bool shouldDrawTouch() {
107+
if (touchInputNotifier == null || shouldDrawTouch == null) {
108+
return false;
109+
}
110+
111+
if (touchInputNotifier.value is FlLongPressEnd || touchInputNotifier.value is FlPanEnd) {
112+
return false;
113+
}
114+
115+
if (touchInputNotifier.value is FlTouchNormapInput && !data.touchData.enableNormalTouch) {
116+
return false;
117+
}
118+
119+
return true;
120+
}
103121
}

lib/src/chart/base/base_chart/touch_input.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ abstract class FlTouchInput {
99
Offset getOffset();
1010
}
1111

12-
class FlLongPressStart extends FlTouchInput {
12+
abstract class FlTouchLongInput extends FlTouchInput {}
13+
14+
class FlLongPressStart extends FlTouchLongInput {
1315

1416
final Offset localPosition;
1517

@@ -22,7 +24,7 @@ class FlLongPressStart extends FlTouchInput {
2224

2325
}
2426

25-
class FlLongPressMoveUpdate extends FlTouchInput {
27+
class FlLongPressMoveUpdate extends FlTouchLongInput {
2628

2729
final Offset localPosition;
2830

@@ -35,7 +37,7 @@ class FlLongPressMoveUpdate extends FlTouchInput {
3537

3638
}
3739

38-
class FlLongPressEnd extends FlTouchInput {
40+
class FlLongPressEnd extends FlTouchLongInput {
3941

4042
final Offset localPosition;
4143

@@ -48,7 +50,10 @@ class FlLongPressEnd extends FlTouchInput {
4850

4951
}
5052

51-
class FlPanStart extends FlTouchInput {
53+
54+
abstract class FlTouchNormapInput extends FlTouchInput {}
55+
56+
class FlPanStart extends FlTouchNormapInput {
5257

5358
final Offset localPosition;
5459

@@ -61,7 +66,7 @@ class FlPanStart extends FlTouchInput {
6166

6267
}
6368

64-
class FlPanMoveUpdate extends FlTouchInput {
69+
class FlPanMoveUpdate extends FlTouchNormapInput {
6570

6671
final Offset localPosition;
6772

@@ -74,7 +79,7 @@ class FlPanMoveUpdate extends FlTouchInput {
7479

7580
}
7681

77-
class FlPanEnd extends FlTouchInput {
82+
class FlPanEnd extends FlTouchNormapInput {
7883

7984
final Offset localPosition;
8085

lib/src/chart/line_chart/line_chart_data.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ class LineTouchData extends FlTouchData {
332332

333333
const LineTouchData({
334334
bool enabled = true,
335+
bool enableNormalTouch = true,
335336
this.touchTooltipData = const TouchTooltipData(),
336337
this.getTouchedSpotIndicator = defaultTouchedIndicators,
337338
this.touchSpotThreshold = 10,
338339
StreamSink<LineTouchResponse> touchResponseSink,
339-
}) : super(enabled, touchResponseSink);
340+
}) : super(enabled, touchResponseSink, enableNormalTouch);
340341

341342
}
342343

lib/src/chart/line_chart/line_chart_painter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class LineChartPainter extends AxisChartPainter {
378378
}
379379

380380
void drawTouchedSpotsIndicator(Canvas canvas, Size viewSize, List<LineTouchedSpot> lineTouchedSpots) {
381-
if (touchInputNotifier.value is FlLongPressEnd) {
381+
if (!shouldDrawTouch()) {
382382
return;
383383
}
384384

lib/src/chart/pie_chart/pie_chart_data.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ class PieTouchData extends FlTouchData {
103103

104104
const PieTouchData({
105105
bool enabled = true,
106+
bool enableNormalTouch = true,
106107
StreamSink<PieTouchResponse> touchResponseStreamSink,
107-
}) : super(enabled, touchResponseStreamSink);
108+
}) : super(enabled, touchResponseStreamSink, enableNormalTouch);
108109
}
109110

110111
/// holds the data of touch response on the [PieChart]

0 commit comments

Comments
 (0)