Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added detectScale to FlTouchData flag and scale gesture detection option #1721

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ class BackgroundBarChartRodData with EquatableMixin {
class BarTouchData extends FlTouchData<BarTouchResponse> with EquatableMixin {
/// You can disable or enable the touch system using [enabled] flag,
///
/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
///
/// [touchCallback] notifies you about the happened touch/pointer events.
/// It gives you a [FlTouchEvent] which is the happened event such as [FlPointerHoverEvent], [FlTapUpEvent], ...
/// It also gives you a [BarTouchResponse] which contains information
Expand All @@ -603,6 +606,7 @@ class BarTouchData extends FlTouchData<BarTouchResponse> with EquatableMixin {
/// on [BarChartRodData.backDrawRodData] too (by default it only works on the main rods).
BarTouchData({
bool? enabled,
bool? detectScale,
BaseTouchCallback<BarTouchResponse>? touchCallback,
MouseCursorResolver<BarTouchResponse>? mouseCursorResolver,
Duration? longPressDuration,
Expand All @@ -619,6 +623,7 @@ class BarTouchData extends FlTouchData<BarTouchResponse> with EquatableMixin {
touchCallback,
mouseCursorResolver,
longPressDuration,
detectScale ?? false,
);

/// Configs of how touch tooltip popup.
Expand Down
5 changes: 5 additions & 0 deletions lib/src/chart/base/base_chart/base_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ abstract class FlTouchData<R extends BaseTouchResponse> with EquatableMixin {
this.touchCallback,
this.mouseCursorResolver,
this.longPressDuration,
this.detectScale,
);

/// You can disable or enable the touch system using [enabled] flag,
final bool enabled;

/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
final bool detectScale;

/// [touchCallback] notifies you about the happened touch/pointer events.
/// It gives you a [FlTouchEvent] which is the happened event such as [FlPointerHoverEvent], [FlTapUpEvent], ...
/// It also gives you a [BaseTouchResponse] which is the chart specific type and contains information
Expand Down
27 changes: 27 additions & 0 deletions lib/src/chart/base/base_chart/fl_touch_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ abstract class FlTouchEvent {
}
}

class FlScaleStartEvent extends FlTouchEvent {
const FlScaleStartEvent(this.details);

final ScaleStartDetails details;

@override
Offset get localPosition => details.focalPoint;
}

class FlScaleUpdateEvent extends FlTouchEvent {
const FlScaleUpdateEvent(this.details);

final ScaleUpdateDetails details;

@override
Offset get localPosition => details.focalPoint;
}

class FlScaleEndEvent extends FlTouchEvent {
const FlScaleEndEvent(this.details);

final ScaleEndDetails details;

@override
Offset get localPosition => Offset.zero;
}

/// When a pointer has contacted the screen and might begin to move
///
/// The [details] object provides the position of the touch.
Expand Down
62 changes: 41 additions & 21 deletions lib/src/chart/base/base_chart/render_base_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ abstract class RenderBaseChart<R extends BaseTouchResponse> extends RenderBox
_touchCallback = value?.touchCallback;
_mouseCursorResolver = value?.mouseCursorResolver;
_longPressDuration = value?.longPressDuration;
_detectScale = value?.detectScale ?? false;
}

BaseTouchCallback<R>? _touchCallback;
MouseCursorResolver<R>? _mouseCursorResolver;
Duration? _longPressDuration;
bool _detectScale = false;

MouseCursor _latestMouseCursor = MouseCursor.defer;

late bool _validForMouseTracker;

/// Recognizes pan gestures, such as onDown, onStart, onUpdate, onCancel, ...
late PanGestureRecognizer _panGestureRecognizer;
// late ScaleGestureRecognizer _scaleGestureRecognizer;

// if [detectScale] is true, we use ScaleGestureRecognizer, otherwise we use PanGestureRecognizer
// Recognizes pan gestures, such as onDown, onStart, onUpdate, onCancel, ...
// or alternatively detects scale gestures, such as onStart, onUpdate, onEnd, ...
late OneSequenceGestureRecognizer _panOrScaleGestureRecognizer;

/// Recognizes tap gestures, such as onTapDown, onTapCancel and onTapUp
late TapGestureRecognizer _tapGestureRecognizer;
Expand All @@ -50,23 +56,37 @@ abstract class RenderBaseChart<R extends BaseTouchResponse> extends RenderBox

/// Initializes our recognizers and implement their callbacks.
void initGestureRecognizers() {
_panGestureRecognizer = PanGestureRecognizer();
_panGestureRecognizer
..onDown = (dragDownDetails) {
_notifyTouchEvent(FlPanDownEvent(dragDownDetails));
}
..onStart = (dragStartDetails) {
_notifyTouchEvent(FlPanStartEvent(dragStartDetails));
}
..onUpdate = (dragUpdateDetails) {
_notifyTouchEvent(FlPanUpdateEvent(dragUpdateDetails));
}
..onCancel = () {
_notifyTouchEvent(const FlPanCancelEvent());
}
..onEnd = (dragEndDetails) {
_notifyTouchEvent(FlPanEndEvent(dragEndDetails));
};
if (_detectScale) {
final scaleGestureRecognizer = ScaleGestureRecognizer()
..onStart = (details) {
_notifyTouchEvent(FlScaleStartEvent(details));
}
..onUpdate = (details) {
_notifyTouchEvent(FlScaleUpdateEvent(details));
}
..onEnd = (details) {
_notifyTouchEvent(FlScaleEndEvent(details));
};
_panOrScaleGestureRecognizer = scaleGestureRecognizer;
} else {
final panGestureRecognizer = PanGestureRecognizer()
..onDown = (dragDownDetails) {
_notifyTouchEvent(FlPanDownEvent(dragDownDetails));
}
..onStart = (dragStartDetails) {
_notifyTouchEvent(FlPanStartEvent(dragStartDetails));
}
..onUpdate = (dragUpdateDetails) {
_notifyTouchEvent(FlPanUpdateEvent(dragUpdateDetails));
}
..onCancel = () {
_notifyTouchEvent(const FlPanCancelEvent());
}
..onEnd = (dragEndDetails) {
_notifyTouchEvent(FlPanEndEvent(dragEndDetails));
};
_panOrScaleGestureRecognizer = panGestureRecognizer;
}

_tapGestureRecognizer = TapGestureRecognizer();
_tapGestureRecognizer
Expand All @@ -88,7 +108,7 @@ abstract class RenderBaseChart<R extends BaseTouchResponse> extends RenderBox
}
..onLongPressMoveUpdate = (longPressMoveUpdateDetails) {
_notifyTouchEvent(
FlLongPressMoveUpdate(longPressMoveUpdateDetails),
FlLongPressMoveUpdate(longPreqssMoveUpdateDetails),
);
}
..onLongPressEnd = (longPressEndDetails) =>
Expand Down Expand Up @@ -124,7 +144,7 @@ abstract class RenderBaseChart<R extends BaseTouchResponse> extends RenderBox
if (event is PointerDownEvent) {
_longPressGestureRecognizer.addPointer(event);
_tapGestureRecognizer.addPointer(event);
_panGestureRecognizer.addPointer(event);
_panOrScaleGestureRecognizer.addPointer(event);
} else if (event is PointerHoverEvent) {
_notifyTouchEvent(FlPointerHoverEvent(event));
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ abstract class FlLineLabel with EquatableMixin {
class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
/// You can disable or enable the touch system using [enabled] flag,
///
/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
///
/// [touchCallback] notifies you about the happened touch/pointer events.
/// It gives you a [FlTouchEvent] which is the happened event such as [FlPointerHoverEvent], [FlTapUpEvent], ...
/// It also gives you a [LineTouchResponse] which contains information
Expand All @@ -873,6 +876,7 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
/// If you need to have a distance threshold for handling touches, use [touchSpotThreshold].
const LineTouchData({
bool enabled = true,
bool detectScale = false,
BaseTouchCallback<LineTouchResponse>? touchCallback,
MouseCursorResolver<LineTouchResponse>? mouseCursorResolver,
Duration? longPressDuration,
Expand All @@ -888,6 +892,7 @@ class LineTouchData extends FlTouchData<LineTouchResponse> with EquatableMixin {
touchCallback,
mouseCursorResolver,
longPressDuration,
detectScale,
);

/// Configs of how touch tooltip popup.
Expand Down
5 changes: 5 additions & 0 deletions lib/src/chart/pie_chart/pie_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ class PieChartSectionData {
class PieTouchData extends FlTouchData<PieTouchResponse> with EquatableMixin {
/// You can disable or enable the touch system using [enabled] flag,
///
/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
///
/// [touchCallback] notifies you about the happened touch/pointer events.
/// It gives you a [FlTouchEvent] which is the happened event such as [FlPointerHoverEvent], [FlTapUpEvent], ...
/// It also gives you a [PieTouchResponse] which contains information
Expand All @@ -305,6 +308,7 @@ class PieTouchData extends FlTouchData<PieTouchResponse> with EquatableMixin {
/// based on the provided [FlTouchEvent] and [PieTouchResponse]
PieTouchData({
bool? enabled,
bool? detectScale,
BaseTouchCallback<PieTouchResponse>? touchCallback,
MouseCursorResolver<PieTouchResponse>? mouseCursorResolver,
Duration? longPressDuration,
Expand All @@ -313,6 +317,7 @@ class PieTouchData extends FlTouchData<PieTouchResponse> with EquatableMixin {
touchCallback,
mouseCursorResolver,
longPressDuration,
detectScale ?? false,
);

/// Used for equality check, see [EquatableMixin].
Expand Down
8 changes: 8 additions & 0 deletions lib/src/chart/radar_chart/radar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ class RadarTouchData extends FlTouchData<RadarTouchResponse>
with EquatableMixin {
/// You can disable or enable the touch system using [enabled] flag,
///
/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
///
/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
///
/// [touchCallback] notifies you about the happened touch/pointer events.
/// It gives you a [FlTouchEvent] which is the happened event such as [FlPointerHoverEvent], [FlTapUpEvent], ...
/// It also gives you a [RadarTouchResponse] which contains information
Expand All @@ -391,6 +397,7 @@ class RadarTouchData extends FlTouchData<RadarTouchResponse>
/// based on the provided [FlTouchEvent] and [RadarTouchResponse]
RadarTouchData({
bool? enabled,
bool? detectScale = false,
BaseTouchCallback<RadarTouchResponse>? touchCallback,
MouseCursorResolver<RadarTouchResponse>? mouseCursorResolver,
Duration? longPressDuration,
Expand All @@ -401,6 +408,7 @@ class RadarTouchData extends FlTouchData<RadarTouchResponse>
touchCallback,
mouseCursorResolver,
longPressDuration,
detectScale ?? false,
);

/// we find the nearest spots on touched position based on this threshold
Expand Down
5 changes: 5 additions & 0 deletions lib/src/chart/scatter_chart/scatter_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ class ScatterTouchData extends FlTouchData<ScatterTouchResponse>
with EquatableMixin {
/// You can disable or enable the touch system using [enabled] flag,
///
/// You can disable or enable the scale detection using [detectScale] flag,
/// detecting scale gesture will disable pan gesture detection
///
/// [touchCallback] notifies you about the happened touch/pointer events.
/// It gives you a [FlTouchEvent] which is the happened event such as [FlPointerHoverEvent], [FlTapUpEvent], ...
/// It also gives you a [ScatterTouchResponse] which contains information
Expand All @@ -278,6 +281,7 @@ class ScatterTouchData extends FlTouchData<ScatterTouchResponse>
/// If you need to have a distance threshold for handling touches, use [touchSpotThreshold].
ScatterTouchData({
bool? enabled,
bool? detectScale,
BaseTouchCallback<ScatterTouchResponse>? touchCallback,
MouseCursorResolver<ScatterTouchResponse>? mouseCursorResolver,
Duration? longPressDuration,
Expand All @@ -292,6 +296,7 @@ class ScatterTouchData extends FlTouchData<ScatterTouchResponse>
touchCallback,
mouseCursorResolver,
longPressDuration,
detectScale ?? false,
);

/// show a tooltip on touched spots
Expand Down