Skip to content

Commit 4f454be

Browse files
author
Renzo Olivares
committed
Add offsetFromOrigin and localOffsetFromOrigin to DragUpdateDetails similar to LongPressMoveUpdateDetails, eliminates the need to hold the state of lastDragStartDetails
1 parent e5d0df5 commit 4f454be

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

packages/flutter/lib/src/gestures/drag_details.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,17 @@ class DragUpdateDetails {
140140
this.primaryDelta,
141141
required this.globalPosition,
142142
Offset? localPosition,
143+
this.offsetFromOrigin = Offset.zero,
144+
Offset? localOffsetFromOrigin,
143145
}) : assert(delta != null),
144146
assert(
145147
primaryDelta == null
146148
|| (primaryDelta == delta.dx && delta.dy == 0.0)
147149
|| (primaryDelta == delta.dy && delta.dx == 0.0),
148150
),
149-
localPosition = localPosition ?? globalPosition;
151+
assert(offsetFromOrigin != null),
152+
localPosition = localPosition ?? globalPosition,
153+
localOffsetFromOrigin = localOffsetFromOrigin ?? offsetFromOrigin;
150154

151155
/// Recorded timestamp of the source pointer event that triggered the drag
152156
/// event.
@@ -191,6 +195,16 @@ class DragUpdateDetails {
191195
/// Defaults to [globalPosition] if not specified in the constructor.
192196
final Offset localPosition;
193197

198+
/// A delta offset from the point where the drag initially contacted
199+
/// the screen to the point where the pointer is currently located (the
200+
/// present [globalPosition]) when this callback is triggered.
201+
final Offset offsetFromOrigin;
202+
203+
/// A local delta offset from the point where the drag initially contacted
204+
/// the screen to the point where the pointer is currently located (the
205+
/// present [localPosition]) when this callback is triggered.
206+
final Offset localOffsetFromOrigin;
207+
194208
@override
195209
String toString() => '${objectRuntimeType(this, 'DragUpdateDetails')}($delta)';
196210
}

packages/flutter/lib/src/gestures/monodrag.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer {
479479
primaryDelta: primaryDelta,
480480
globalPosition: globalPosition,
481481
localPosition: localPosition,
482+
offsetFromOrigin: globalPosition - _initialPosition.global,
483+
localOffsetFromOrigin: localPosition! - _initialPosition.local,
482484
);
483485
invokeCallback<void>('onUpdate', () => onUpdate!(details));
484486
}

packages/flutter/lib/src/gestures/tap_and_drag.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer {
4242

4343
GestureTapAndDragCancelCallback? onCancel;
4444

45+
// Drag related state
46+
late OffsetPair _initialPosition;
4547
_DragState _state = _DragState.ready;
4648

4749
// For consecutive tap
@@ -123,11 +125,14 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer {
123125
primaryDelta: null,
124126
globalPosition: event.position,
125127
localPosition: event.localPosition,
128+
offsetFromOrigin: event.position - _initialPosition.global,
129+
localOffsetFromOrigin: event.localPosition - _initialPosition.local,
126130
);
127131
invokeCallback<void>('onUpdate', () => onUpdate!(details, _dragTapCount!));
128132
} else if (_state == _DragState.possible) {
129133
print('is zoom start ${event is PointerPanZoomStartEvent}');
130134
_state = _DragState.accepted;
135+
_initialPosition = OffsetPair(global: event.position, local: event.localPosition);
131136
DragStartDetails details = DragStartDetails(
132137
sourceTimeStamp: event.timeStamp,
133138
globalPosition: event.position,

packages/flutter/lib/src/widgets/text_selection.dart

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ export 'package:flutter/services.dart' show TextSelectionDelegate;
3232
/// called.
3333
const Duration _kDragSelectionUpdateThrottle = Duration(milliseconds: 50);
3434

35-
/// Signature for when a pointer that's dragging to select text has moved again.
36-
///
37-
/// The first argument [startDetails] contains the details of the event that
38-
/// initiated the dragging.
39-
///
40-
/// The second argument [updateDetails] contains the details of the current
41-
/// pointer movement. It's the same as the one passed to [DragGestureRecognizer.onUpdate].
42-
///
43-
/// This signature is different from [GestureDragUpdateCallback] to make it
44-
/// easier for various text fields to use [TextSelectionGestureDetector] without
45-
/// having to store the start position.
46-
typedef DragSelectionUpdateCallback = void Function(DragStartDetails startDetails, DragUpdateDetails updateDetails, int tapCount);
47-
4835
/// The type for a Function that builds a toolbar's container with the given
4936
/// child.
5037
///
@@ -2115,7 +2102,7 @@ class TextSelectionGestureDetectorBuilder {
21152102
/// * [TextSelectionGestureDetector.onDragSelectionUpdate], which triggers
21162103
/// this callback./lib/src/material/text_field.dart
21172104
@protected
2118-
void onDragSelectionUpdate(DragStartDetails startDetails, DragUpdateDetails updateDetails, int tapCount) {
2105+
void onDragSelectionUpdate(DragUpdateDetails details, int tapCount) {
21192106
if (!delegate.selectionEnabled) {
21202107
return;
21212108
}
@@ -2125,32 +2112,33 @@ class TextSelectionGestureDetectorBuilder {
21252112
final Offset startOffset = renderEditable.maxLines == 1
21262113
? Offset(renderEditable.offset.pixels - _dragStartViewportOffset, 0.0)
21272114
: Offset(0.0, renderEditable.offset.pixels - _dragStartViewportOffset);
2115+
final Offset dragStartGlobalPosition = details.globalPosition - details.offsetFromOrigin;
21282116

21292117
// Select word by word.
21302118
if (tapCount == 2) {
21312119
return renderEditable.selectWordsInRange(
2132-
from: startDetails.globalPosition - startOffset,
2133-
to: updateDetails.globalPosition,
2120+
from: dragStartGlobalPosition - startOffset,
2121+
to: details.globalPosition,
21342122
cause: SelectionChangedCause.drag,
21352123
);
21362124
}
21372125
return renderEditable.selectPositionAt(
2138-
from: startDetails.globalPosition - startOffset,
2139-
to: updateDetails.globalPosition,
2126+
from: dragStartGlobalPosition - startOffset,
2127+
to: details.globalPosition,
21402128
cause: SelectionChangedCause.drag,
21412129
);
21422130
}
21432131

21442132
if (_shiftTapDragSelection!.isCollapsed
21452133
|| (defaultTargetPlatform != TargetPlatform.iOS
21462134
&& defaultTargetPlatform != TargetPlatform.macOS)) {
2147-
return _extendSelection(updateDetails.globalPosition, SelectionChangedCause.drag);
2135+
return _extendSelection(details.globalPosition, SelectionChangedCause.drag);
21482136
}
21492137

21502138
// If the drag inverts the selection, Mac and iOS revert to the initial
21512139
// selection.
21522140
final TextSelection selection = editableText.textEditingValue.selection;
2153-
final TextPosition nextExtent = renderEditable.getPositionForPoint(updateDetails.globalPosition);
2141+
final TextPosition nextExtent = renderEditable.getPositionForPoint(details.globalPosition);
21542142
final bool isShiftTapDragSelectionForward =
21552143
_shiftTapDragSelection!.baseOffset < _shiftTapDragSelection!.extentOffset;
21562144
final bool isInverted = isShiftTapDragSelectionForward
@@ -2179,7 +2167,7 @@ class TextSelectionGestureDetectorBuilder {
21792167
SelectionChangedCause.drag,
21802168
);
21812169
} else {
2182-
_extendSelection(updateDetails.globalPosition, SelectionChangedCause.drag);
2170+
_extendSelection(details.globalPosition, SelectionChangedCause.drag);
21832171
}
21842172
}
21852173

@@ -2321,7 +2309,7 @@ class TextSelectionGestureDetector extends StatefulWidget {
23212309
/// The frequency of calls is throttled to avoid excessive text layout
23222310
/// operations in text fields. The throttling is controlled by the constant
23232311
/// [_kDragSelectionUpdateThrottle].
2324-
final DragSelectionUpdateCallback? onDragSelectionUpdate;
2312+
final GestureTapAndDragUpdateCallback? onDragSelectionUpdate;
23252313

23262314
/// Called when a mouse that was previously dragging is released.
23272315
final GestureDragEndCallback? onDragSelectionEnd;
@@ -2376,17 +2364,13 @@ class _TextSelectionGestureDetectorState extends State<TextSelectionGestureDetec
23762364
widget.onSingleTapCancel?.call();
23772365
}
23782366

2379-
DragStartDetails? _lastDragStartDetails;
23802367
DragUpdateDetails? _lastDragUpdateDetails;
23812368
Timer? _dragUpdateThrottleTimer;
23822369
int? _dragTapCount;
23832370

23842371
void _handleDragStart(DragStartDetails details, int tapCount) {
23852372
print('drag start');
2386-
assert(_lastDragStartDetails == null);
2387-
print('passed assert');
23882373
print('tap count $tapCount');
2389-
_lastDragStartDetails = details;
23902374
if (tapCount != 2) {
23912375
widget.onDragSelectionStart?.call(details);
23922376
}
@@ -2408,15 +2392,13 @@ class _TextSelectionGestureDetectorState extends State<TextSelectionGestureDetec
24082392
/// Once the drag gesture ends, any pending drag update will be fired
24092393
/// immediately. See [_handleDragEnd].
24102394
void _handleDragUpdateThrottled() {
2411-
assert(_lastDragStartDetails != null);
24122395
assert(_lastDragUpdateDetails != null);
2413-
widget.onDragSelectionUpdate?.call(_lastDragStartDetails!, _lastDragUpdateDetails!, _dragTapCount!);
2396+
widget.onDragSelectionUpdate?.call(_lastDragUpdateDetails!, _dragTapCount!);
24142397
_dragUpdateThrottleTimer = null;
24152398
_lastDragUpdateDetails = null;
24162399
}
24172400

24182401
void _handleDragEnd(TapUpDetails upDetails, DragEndDetails endDetails, int tapCount) {
2419-
assert(_lastDragStartDetails != null);
24202402
print('drag end');
24212403
print('tap count $tapCount');
24222404
_handleTapUp(upDetails, tapCount);
@@ -2429,7 +2411,6 @@ class _TextSelectionGestureDetectorState extends State<TextSelectionGestureDetec
24292411
widget.onDragSelectionEnd?.call(endDetails);
24302412
_dragTapCount = null;
24312413
_dragUpdateThrottleTimer = null;
2432-
_lastDragStartDetails = null;
24332414
_lastDragUpdateDetails = null;
24342415
}
24352416

0 commit comments

Comments
 (0)