monodrag.dart 23.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6
import 'package:vector_math/vector_math_64.dart';
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import 'arena.dart';
import 'constants.dart';
import 'drag_details.dart';
import 'events.dart';
import 'recognizer.dart';
import 'velocity_tracker.dart';

enum _DragState {
  ready,
  possible,
  accepted,
}

/// Signature for when a pointer that was previously in contact with the screen
/// and moving is no longer in contact with the screen.
///
/// The velocity at which the pointer was moving when it stopped contacting
/// the screen is available in the `details`.
///
27
/// Used by [DragGestureRecognizer.onEnd].
28
typedef GestureDragEndCallback = void Function(DragEndDetails details);
29 30 31 32

/// Signature for when the pointer that previously triggered a
/// [GestureDragDownCallback] did not complete.
///
33
/// Used by [DragGestureRecognizer.onCancel].
34
typedef GestureDragCancelCallback = void Function();
35

36 37 38
/// Signature for a function that builds a [VelocityTracker].
///
/// Used by [DragGestureRecognizer.velocityTrackerBuilder].
39 40
typedef GestureVelocityTrackerBuilder = VelocityTracker Function(PointerEvent event);

41 42 43 44 45 46 47 48 49 50 51
/// Recognizes movement.
///
/// In contrast to [MultiDragGestureRecognizer], [DragGestureRecognizer]
/// recognizes a single gesture sequence for all the pointers it watches, which
/// means that the recognizer has at most one drag sequence active at any given
/// time regardless of how many pointers are in contact with the screen.
///
/// [DragGestureRecognizer] is not intended to be used directly. Instead,
/// consider using one of its subclasses to recognize specific types for drag
/// gestures.
///
52 53 54 55
/// [DragGestureRecognizer] competes on pointer events of [kPrimaryButton]
/// only when it has at least one non-null callback. If it has no callbacks, it
/// is a no-op.
///
56 57
/// See also:
///
58 59 60
///  * [HorizontalDragGestureRecognizer], for left and right drags.
///  * [VerticalDragGestureRecognizer], for up and down drags.
///  * [PanGestureRecognizer], for drags that are not locked to a single axis.
61
abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer {
62
  /// Initialize the object.
63 64
  ///
  /// [dragStartBehavior] must not be null.
65
  ///
66
  /// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
67
  DragGestureRecognizer({
68
    Object? debugOwner,
69 70 71 72
    @Deprecated(
      'Migrate to supportedDevices. '
      'This feature was deprecated after v2.3.0-1.0.pre.',
    )
73
    PointerDeviceKind? kind,
74
    this.dragStartBehavior = DragStartBehavior.start,
75
    this.velocityTrackerBuilder = _defaultBuilder,
76
    Set<PointerDeviceKind>? supportedDevices,
77
  }) : assert(dragStartBehavior != null),
78 79 80 81 82
       super(
         debugOwner: debugOwner,
         kind: kind,
         supportedDevices: supportedDevices,
       );
83

84
  static VelocityTracker _defaultBuilder(PointerEvent event) => VelocityTracker.withKind(event.kind);
85 86

  /// Configure the behavior of offsets passed to [onStart].
87
  ///
88
  /// If set to [DragStartBehavior.start], the [onStart] callback will be called
89 90 91 92 93
  /// with the position of the pointer at the time this gesture recognizer won
  /// the arena. If [DragStartBehavior.down], [onStart] will be called with
  /// the position of the first detected down event for the pointer. When there
  /// are no other gestures competing with this gesture in the arena, there's
  /// no difference in behavior between the two settings.
94 95
  ///
  /// For more information about the gesture arena:
96
  /// https://flutter.dev/docs/development/ui/advanced/gestures#gesture-disambiguation
97 98 99 100 101
  ///
  /// By default, the drag start behavior is [DragStartBehavior.start].
  ///
  /// ## Example:
  ///
102 103 104 105
  /// A [HorizontalDragGestureRecognizer] and a [VerticalDragGestureRecognizer]
  /// compete with each other. A finger presses down on the screen with
  /// offset (500.0, 500.0), and then moves to position (510.0, 500.0) before
  /// the [HorizontalDragGestureRecognizer] wins the arena. With
106
  /// [dragStartBehavior] set to [DragStartBehavior.down], the [onStart]
107 108 109
  /// callback will be called with position (500.0, 500.0). If it is
  /// instead set to [DragStartBehavior.start], [onStart] will be called with
  /// position (510.0, 500.0).
110
  DragStartBehavior dragStartBehavior;
111

112 113
  /// A pointer has contacted the screen with a primary button and might begin
  /// to move.
114 115 116
  ///
  /// The position of the pointer is provided in the callback's `details`
  /// argument, which is a [DragDownDetails] object.
117 118 119 120 121
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [DragDownDetails], which is passed as an argument to this callback.
122
  GestureDragDownCallback? onDown;
123

124 125
  /// A pointer has contacted the screen with a primary button and has begun to
  /// move.
126 127
  ///
  /// The position of the pointer is provided in the callback's `details`
128 129
  /// argument, which is a [DragStartDetails] object. The [dragStartBehavior]
  /// determines this position.
130 131 132 133 134
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [DragStartDetails], which is passed as an argument to this callback.
135
  GestureDragStartCallback? onStart;
136

137 138
  /// A pointer that is in contact with the screen with a primary button and
  /// moving has moved again.
139
  ///
140
  /// The distance traveled by the pointer since the last update is provided in
141
  /// the callback's `details` argument, which is a [DragUpdateDetails] object.
142 143 144 145 146
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [DragUpdateDetails], which is passed as an argument to this callback.
147
  GestureDragUpdateCallback? onUpdate;
148

149 150 151
  /// A pointer that was previously in contact with the screen with a primary
  /// button and moving is no longer in contact with the screen and was moving
  /// at a specific velocity when it stopped contacting the screen.
152 153 154
  ///
  /// The velocity is provided in the callback's `details` argument, which is a
  /// [DragEndDetails] object.
155 156 157 158 159
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [DragEndDetails], which is passed as an argument to this callback.
160
  GestureDragEndCallback? onEnd;
161 162

  /// The pointer that previously triggered [onDown] did not complete.
163 164 165 166
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
167
  GestureDragCancelCallback? onCancel;
168 169 170 171 172 173

  /// The minimum distance an input pointer drag must have moved to
  /// to be considered a fling gesture.
  ///
  /// This value is typically compared with the distance traveled along the
  /// scrolling axis. If null then [kTouchSlop] is used.
174
  double? minFlingDistance;
175 176 177 178 179 180

  /// The minimum velocity for an input pointer drag to be considered fling.
  ///
  /// This value is typically compared with the magnitude of fling gesture's
  /// velocity along the scrolling axis. If null then [kMinFlingVelocity]
  /// is used.
181
  double? minFlingVelocity;
182 183 184 185

  /// Fling velocity magnitudes will be clamped to this value.
  ///
  /// If null then [kMaxFlingVelocity] is used.
186
  double? maxFlingVelocity;
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  /// Determines the type of velocity estimation method to use for a potential
  /// drag gesture, when a new pointer is added.
  ///
  /// To estimate the velocity of a gesture, [DragGestureRecognizer] calls
  /// [velocityTrackerBuilder] when it starts to track a new pointer in
  /// [addAllowedPointer], and add subsequent updates on the pointer to the
  /// resulting velocity tracker, until the gesture recognizer stops tracking
  /// the pointer. This allows you to specify a different velocity estimation
  /// strategy for each allowed pointer added, by changing the type of velocity
  /// tracker this [GestureVelocityTrackerBuilder] returns.
  ///
  /// If left unspecified the default [velocityTrackerBuilder] creates a new
  /// [VelocityTracker] for every pointer added.
  ///
  /// See also:
  ///
  ///  * [VelocityTracker], a velocity tracker that uses least squares estimation
  ///    on the 20 most recent pointer data samples. It's a well-rounded velocity
  ///    tracker and is used by default.
  ///  * [IOSScrollViewFlingVelocityTracker], a specialized velocity tracker for
  ///    determining the initial fling velocity for a [Scrollable] on iOS, to
  ///    match the native behavior on that platform.
  GestureVelocityTrackerBuilder velocityTrackerBuilder;

212
  _DragState _state = _DragState.ready;
213 214 215
  late OffsetPair _initialPosition;
  late OffsetPair _pendingDragOffset;
  Duration? _lastPendingEventTimestamp;
216 217
  // The buttons sent by `PointerDownEvent`. If a `PointerMoveEvent` comes with a
  // different set of buttons, the gesture is canceled.
218 219
  int? _initialButtons;
  Matrix4? _lastTransform;
220 221 222 223 224

  /// Distance moved in the global coordinate space of the screen in drag direction.
  ///
  /// If drag is only allowed along a defined axis, this value may be negative to
  /// differentiate the direction of the drag.
225
  late double _globalDistanceMoved;
226

227 228 229 230 231
  /// Determines if a gesture is a fling or not based on velocity.
  ///
  /// A fling calls its gesture end callback with a velocity, allowing the
  /// provider of the callback to respond by carrying the gesture forward with
  /// inertia, for example.
232
  bool isFlingGesture(VelocityEstimate estimate, PointerDeviceKind kind);
233

234
  Offset _getDeltaForDetails(Offset delta);
235
  double? _getPrimaryValueFromOffset(Offset value);
236
  bool _hasSufficientGlobalDistanceToAccept(PointerDeviceKind pointerDeviceKind, double? deviceTouchSlop);
237 238 239

  final Map<int, VelocityTracker> _velocityTrackers = <int, VelocityTracker>{};

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  @override
  bool isPointerAllowed(PointerEvent event) {
    if (_initialButtons == null) {
      switch (event.buttons) {
        case kPrimaryButton:
          if (onDown == null &&
              onStart == null &&
              onUpdate == null &&
              onEnd == null &&
              onCancel == null)
            return false;
          break;
        default:
          return false;
      }
    } else {
      // There can be multiple drags simultaneously. Their effects are combined.
      if (event.buttons != _initialButtons) {
        return false;
      }
    }
261
    return super.isPointerAllowed(event as PointerDownEvent);
262 263
  }

264
  @override
265
  void addAllowedPointer(PointerDownEvent event) {
266
    super.addAllowedPointer(event);
267
    _velocityTrackers[event.pointer] = velocityTrackerBuilder(event);
268 269
    if (_state == _DragState.ready) {
      _state = _DragState.possible;
270
      _initialPosition = OffsetPair(global: event.position, local: event.localPosition);
271
      _initialButtons = event.buttons;
272 273
      _pendingDragOffset = OffsetPair.zero;
      _globalDistanceMoved = 0.0;
274
      _lastPendingEventTimestamp = event.timeStamp;
275
      _lastTransform = event.transform;
276
      _checkDown();
277 278
    } else if (_state == _DragState.accepted) {
      resolve(GestureDisposition.accepted);
279 280 281 282 283 284
    }
  }

  @override
  void handleEvent(PointerEvent event) {
    assert(_state != _DragState.ready);
285 286
    if (!event.synthesized
        && (event is PointerDownEvent || event is PointerMoveEvent)) {
287
      final VelocityTracker tracker = _velocityTrackers[event.pointer]!;
288
      assert(tracker != null);
289
      tracker.addPosition(event.timeStamp, event.localPosition);
290 291 292
    }

    if (event is PointerMoveEvent) {
293
      if (event.buttons != _initialButtons) {
294
        _giveUpPointer(event.pointer);
295 296
        return;
      }
297
      if (_state == _DragState.accepted) {
298 299
        _checkUpdate(
          sourceTimeStamp: event.timeStamp,
300 301
          delta: _getDeltaForDetails(event.localDelta),
          primaryDelta: _getPrimaryValueFromOffset(event.localDelta),
302
          globalPosition: event.position,
303
          localPosition: event.localPosition,
304
        );
305
      } else {
306
        _pendingDragOffset += OffsetPair(local: event.localDelta, global: event.delta);
307
        _lastPendingEventTimestamp = event.timeStamp;
308 309
        _lastTransform = event.transform;
        final Offset movedLocally = _getDeltaForDetails(event.localDelta);
310
        final Matrix4? localToGlobalTransform = event.transform == null ? null : Matrix4.tryInvert(event.transform!);
311 312 313 314 315
        _globalDistanceMoved += PointerEvent.transformDeltaViaPositions(
          transform: localToGlobalTransform,
          untransformedDelta: movedLocally,
          untransformedEndPosition: event.localPosition,
        ).distance * (_getPrimaryValueFromOffset(movedLocally) ?? 1).sign;
316
        if (_hasSufficientGlobalDistanceToAccept(event.kind, gestureSettings?.touchSlop))
317 318 319
          resolve(GestureDisposition.accepted);
      }
    }
320
    if (event is PointerUpEvent || event is PointerCancelEvent) {
321
      _giveUpPointer(event.pointer);
322
    }
323 324
  }

325 326
  final Set<int> _acceptedActivePointers = <int>{};

327 328
  @override
  void acceptGesture(int pointer) {
329 330
    assert(!_acceptedActivePointers.contains(pointer));
    _acceptedActivePointers.add(pointer);
331 332
    if (_state != _DragState.accepted) {
      _state = _DragState.accepted;
333
      final OffsetPair delta = _pendingDragOffset;
334 335
      final Duration timestamp = _lastPendingEventTimestamp!;
      final Matrix4? transform = _lastTransform;
336
      final Offset localUpdateDelta;
337 338 339
      switch (dragStartBehavior) {
        case DragStartBehavior.start:
          _initialPosition = _initialPosition + delta;
340
          localUpdateDelta = Offset.zero;
341 342
          break;
        case DragStartBehavior.down:
343
          localUpdateDelta = _getDeltaForDetails(delta.local);
344 345
          break;
      }
346
      _pendingDragOffset = OffsetPair.zero;
347
      _lastPendingEventTimestamp = null;
348
      _lastTransform = null;
349
      _checkStart(timestamp, pointer);
350
      if (localUpdateDelta != Offset.zero && onUpdate != null) {
351
        final Matrix4? localToGlobal = transform != null ? Matrix4.tryInvert(transform) : null;
352 353 354 355 356 357 358 359
        final Offset correctedLocalPosition = _initialPosition.local + localUpdateDelta;
        final Offset globalUpdateDelta = PointerEvent.transformDeltaViaPositions(
          untransformedEndPosition: correctedLocalPosition,
          untransformedDelta: localUpdateDelta,
          transform: localToGlobal,
        );
        final OffsetPair updateDelta = OffsetPair(local: localUpdateDelta, global: globalUpdateDelta);
        final OffsetPair correctedPosition = _initialPosition + updateDelta; // Only adds delta for down behaviour
360
        _checkUpdate(
361
          sourceTimeStamp: timestamp,
362 363 364 365
          delta: localUpdateDelta,
          primaryDelta: _getPrimaryValueFromOffset(localUpdateDelta),
          globalPosition: correctedPosition.global,
          localPosition: correctedPosition.local,
366
        );
367
      }
368 369 370 371
      // This acceptGesture might have been called only for one pointer, instead
      // of all pointers. Resolve all pointers to `accepted`. This won't cause
      // infinite recursion because an accepted pointer won't be accepted again.
      resolve(GestureDisposition.accepted);
372 373 374 375 376
    }
  }

  @override
  void rejectGesture(int pointer) {
377
    _giveUpPointer(pointer);
378 379 380 381
  }

  @override
  void didStopTrackingLastPointer(int pointer) {
382 383 384 385 386 387 388 389 390 391 392 393 394
    assert(_state != _DragState.ready);
    switch(_state) {
      case _DragState.ready:
        break;

      case _DragState.possible:
        resolve(GestureDisposition.rejected);
        _checkCancel();
        break;

      case _DragState.accepted:
        _checkEnd(pointer);
        break;
395
    }
396 397
    _velocityTrackers.clear();
    _initialButtons = null;
398
    _state = _DragState.ready;
399
  }
400

401
  void _giveUpPointer(int pointer) {
402
    stopTrackingPointer(pointer);
403 404 405
    // If we never accepted the pointer, we reject it since we are no longer
    // interested in winning the gesture arena for it.
    if (!_acceptedActivePointers.remove(pointer))
406
      resolvePointer(pointer, GestureDisposition.rejected);
407 408
  }

409 410
  void _checkDown() {
    assert(_initialButtons == kPrimaryButton);
411 412 413 414 415
    if (onDown != null) {
      final DragDownDetails details = DragDownDetails(
        globalPosition: _initialPosition.global,
        localPosition: _initialPosition.local,
      );
416
      invokeCallback<void>('onDown', () => onDown!(details));
417
    }
418 419
  }

420
  void _checkStart(Duration timestamp, int pointer) {
421
    assert(_initialButtons == kPrimaryButton);
422 423 424 425 426 427 428
    if (onStart != null) {
      final DragStartDetails details = DragStartDetails(
        sourceTimeStamp: timestamp,
        globalPosition: _initialPosition.global,
        localPosition: _initialPosition.local,
        kind: getKindForPointer(pointer),
      );
429
      invokeCallback<void>('onStart', () => onStart!(details));
430
    }
431 432 433
  }

  void _checkUpdate({
434 435 436 437 438
    Duration? sourceTimeStamp,
    required Offset delta,
    double? primaryDelta,
    required Offset globalPosition,
    Offset? localPosition,
439 440
  }) {
    assert(_initialButtons == kPrimaryButton);
441 442 443 444 445 446 447 448
    if (onUpdate != null) {
      final DragUpdateDetails details = DragUpdateDetails(
        sourceTimeStamp: sourceTimeStamp,
        delta: delta,
        primaryDelta: primaryDelta,
        globalPosition: globalPosition,
        localPosition: localPosition,
      );
449
      invokeCallback<void>('onUpdate', () => onUpdate!(details));
450
    }
451 452 453 454 455 456 457
  }

  void _checkEnd(int pointer) {
    assert(_initialButtons == kPrimaryButton);
    if (onEnd == null)
      return;

458
    final VelocityTracker tracker = _velocityTrackers[pointer]!;
459 460
    assert(tracker != null);

461 462
    final DragEndDetails details;
    final String Function() debugReport;
463

464
    final VelocityEstimate? estimate = tracker.getVelocityEstimate();
465
    if (estimate != null && isFlingGesture(estimate, tracker.kind)) {
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
      final Velocity velocity = Velocity(pixelsPerSecond: estimate.pixelsPerSecond)
        .clampMagnitude(minFlingVelocity ?? kMinFlingVelocity, maxFlingVelocity ?? kMaxFlingVelocity);
      details = DragEndDetails(
        velocity: velocity,
        primaryVelocity: _getPrimaryValueFromOffset(velocity.pixelsPerSecond),
      );
      debugReport = () {
        return '$estimate; fling at $velocity.';
      };
    } else {
      details = DragEndDetails(
        velocity: Velocity.zero,
        primaryVelocity: 0.0,
      );
      debugReport = () {
        if (estimate == null)
          return 'Could not estimate velocity.';
        return '$estimate; judged to not be a fling.';
      };
485
    }
486
    invokeCallback<void>('onEnd', () => onEnd!(details), debugReport: debugReport);
487 488 489 490 491
  }

  void _checkCancel() {
    assert(_initialButtons == kPrimaryButton);
    if (onCancel != null)
492
      invokeCallback<void>('onCancel', onCancel!);
493 494 495 496 497 498 499
  }

  @override
  void dispose() {
    _velocityTrackers.clear();
    super.dispose();
  }
500 501 502
  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
503
    properties.add(EnumProperty<DragStartBehavior>('start behavior', dragStartBehavior));
504
  }
505 506 507 508 509 510 511 512
}

/// Recognizes movement in the vertical direction.
///
/// Used for vertical scrolling.
///
/// See also:
///
513 514 515 516
///  * [HorizontalDragGestureRecognizer], for a similar recognizer but for
///    horizontal movement.
///  * [MultiDragGestureRecognizer], for a family of gesture recognizers that
///    track each touch point independently.
517
class VerticalDragGestureRecognizer extends DragGestureRecognizer {
518
  /// Create a gesture recognizer for interactions in the vertical axis.
519
  ///
520
  /// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
521
  VerticalDragGestureRecognizer({
522
    Object? debugOwner,
523 524 525 526
    @Deprecated(
      'Migrate to supportedDevices. '
      'This feature was deprecated after v2.3.0-1.0.pre.',
    )
527
    PointerDeviceKind? kind,
528 529 530 531 532 533
    Set<PointerDeviceKind>? supportedDevices,
  }) : super(
         debugOwner: debugOwner,
         kind: kind,
         supportedDevices: supportedDevices,
       );
534

535
  @override
536
  bool isFlingGesture(VelocityEstimate estimate, PointerDeviceKind kind) {
537
    final double minVelocity = minFlingVelocity ?? kMinFlingVelocity;
538
    final double minDistance = minFlingDistance ?? computeHitSlop(kind, gestureSettings);
539 540 541 542
    return estimate.pixelsPerSecond.dy.abs() > minVelocity && estimate.offset.dy.abs() > minDistance;
  }

  @override
543 544
  bool _hasSufficientGlobalDistanceToAccept(PointerDeviceKind pointerDeviceKind, double? deviceTouchSlop) {
    return _globalDistanceMoved.abs() > computeHitSlop(pointerDeviceKind, gestureSettings);
545
  }
546 547

  @override
548
  Offset _getDeltaForDetails(Offset delta) => Offset(0.0, delta.dy);
549 550 551 552 553

  @override
  double _getPrimaryValueFromOffset(Offset value) => value.dy;

  @override
554
  String get debugDescription => 'vertical drag';
555 556 557 558 559 560 561 562
}

/// Recognizes movement in the horizontal direction.
///
/// Used for horizontal scrolling.
///
/// See also:
///
563 564 565 566
///  * [VerticalDragGestureRecognizer], for a similar recognizer but for
///    vertical movement.
///  * [MultiDragGestureRecognizer], for a family of gesture recognizers that
///    track each touch point independently.
567
class HorizontalDragGestureRecognizer extends DragGestureRecognizer {
568
  /// Create a gesture recognizer for interactions in the horizontal axis.
569
  ///
570
  /// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
571
  HorizontalDragGestureRecognizer({
572
    Object? debugOwner,
573 574 575 576
    @Deprecated(
      'Migrate to supportedDevices. '
      'This feature was deprecated after v2.3.0-1.0.pre.',
    )
577
    PointerDeviceKind? kind,
578 579 580 581 582 583
    Set<PointerDeviceKind>? supportedDevices,
  }) : super(
         debugOwner: debugOwner,
         kind: kind,
         supportedDevices: supportedDevices,
       );
584

585
  @override
586
  bool isFlingGesture(VelocityEstimate estimate, PointerDeviceKind kind) {
587
    final double minVelocity = minFlingVelocity ?? kMinFlingVelocity;
588
    final double minDistance = minFlingDistance ?? computeHitSlop(kind, gestureSettings);
589 590 591 592
    return estimate.pixelsPerSecond.dx.abs() > minVelocity && estimate.offset.dx.abs() > minDistance;
  }

  @override
593 594
  bool _hasSufficientGlobalDistanceToAccept(PointerDeviceKind pointerDeviceKind, double? deviceTouchSlop) {
    return _globalDistanceMoved.abs() > computeHitSlop(pointerDeviceKind, gestureSettings);
595
  }
596 597

  @override
598
  Offset _getDeltaForDetails(Offset delta) => Offset(delta.dx, 0.0);
599 600 601 602 603

  @override
  double _getPrimaryValueFromOffset(Offset value) => value.dx;

  @override
604
  String get debugDescription => 'horizontal drag';
605 606 607 608 609 610
}

/// Recognizes movement both horizontally and vertically.
///
/// See also:
///
611 612 613 614 615
///  * [ImmediateMultiDragGestureRecognizer], for a similar recognizer that
///    tracks each touch point independently.
///  * [DelayedMultiDragGestureRecognizer], for a similar recognizer that
///    tracks each touch point independently, but that doesn't start until
///    some time has passed.
616
class PanGestureRecognizer extends DragGestureRecognizer {
617
  /// Create a gesture recognizer for tracking movement on a plane.
618
  PanGestureRecognizer({ Object? debugOwner }) : super(debugOwner: debugOwner);
619

620
  @override
621
  bool isFlingGesture(VelocityEstimate estimate, PointerDeviceKind kind) {
622
    final double minVelocity = minFlingVelocity ?? kMinFlingVelocity;
623
    final double minDistance = minFlingDistance ?? computeHitSlop(kind, gestureSettings);
624 625
    return estimate.pixelsPerSecond.distanceSquared > minVelocity * minVelocity
        && estimate.offset.distanceSquared > minDistance * minDistance;
626 627 628
  }

  @override
629 630
  bool _hasSufficientGlobalDistanceToAccept(PointerDeviceKind pointerDeviceKind, double? deviceTouchSlop) {
    return _globalDistanceMoved.abs() > computePanSlop(pointerDeviceKind, gestureSettings);
631 632 633 634 635 636
  }

  @override
  Offset _getDeltaForDetails(Offset delta) => delta;

  @override
637
  double? _getPrimaryValueFromOffset(Offset value) => null;
638 639

  @override
640
  String get debugDescription => 'pan';
641
}