Skip to content

Commit 82cb749

Browse files
authored
ScaleGestureRecognizer: make pointerCount public (#127310)
make `pointCount` in `ScaleGestureRecognizer` public to handle pointer event depending on the number of pointers. flutter/flutter#127309
1 parent 4a3ab68 commit 82cb749

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
393393
/// {@endtemplate}
394394
Offset trackpadScrollToScaleFactor;
395395

396+
/// The number of pointers being tracked by the gesture recognizer.
397+
///
398+
/// Typically this is the number of fingers being used to pan the widget using the gesture
399+
/// recognizer.
400+
int get pointerCount {
401+
return _pointerPanZooms.length + _pointerQueue.length;
402+
}
403+
396404
late Offset _initialFocalPoint;
397405
Offset? _currentFocalPoint;
398406
late double _initialSpan;
@@ -443,10 +451,6 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
443451
return scale;
444452
}
445453

446-
int get _pointerCount {
447-
return _pointerPanZooms.length + _pointerQueue.length;
448-
}
449-
450454
double _computeRotationFactor() {
451455
double factor = 0.0;
452456
if (_initialLine != null && _currentLine != null) {
@@ -566,7 +570,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
566570
for (final _PointerPanZoomData p in _pointerPanZooms.values) {
567571
focalPoint += p.focalPoint;
568572
}
569-
_currentFocalPoint = _pointerCount > 0 ? focalPoint / _pointerCount.toDouble() : Offset.zero;
573+
_currentFocalPoint = pointerCount > 0 ? focalPoint / pointerCount.toDouble() : Offset.zero;
570574

571575
if (previousFocalPoint == null) {
572576
_localFocalPoint = PointerEvent.transformPosition(
@@ -662,9 +666,9 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
662666
if (pixelsPerSecond.distanceSquared > kMaxFlingVelocity * kMaxFlingVelocity) {
663667
velocity = Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * kMaxFlingVelocity);
664668
}
665-
invokeCallback<void>('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity, scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: _pointerCount)));
669+
invokeCallback<void>('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity, scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: pointerCount)));
666670
} else {
667-
invokeCallback<void>('onEnd', () => onEnd!(ScaleEndDetails(scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: _pointerCount)));
671+
invokeCallback<void>('onEnd', () => onEnd!(ScaleEndDetails(scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: pointerCount)));
668672
}
669673
}
670674
_state = _ScaleState.accepted;
@@ -706,7 +710,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
706710
focalPoint: _currentFocalPoint!,
707711
localFocalPoint: _localFocalPoint,
708712
rotation: _computeRotationFactor(),
709-
pointerCount: _pointerCount,
713+
pointerCount: pointerCount,
710714
focalPointDelta: _delta,
711715
));
712716
});
@@ -721,7 +725,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
721725
onStart!(ScaleStartDetails(
722726
focalPoint: _currentFocalPoint!,
723727
localFocalPoint: _localFocalPoint,
724-
pointerCount: _pointerCount,
728+
pointerCount: pointerCount,
725729
));
726730
});
727731
}

packages/flutter/test/gestures/scale_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ void main() {
7979
updatedDelta = null;
8080
expect(didEndScale, isFalse);
8181
expect(didTap, isFalse);
82+
expect(scale.pointerCount, 1);
8283

8384
// Two-finger scaling
8485
final TestPointer pointer2 = TestPointer(2);
@@ -87,6 +88,7 @@ void main() {
8788
tap.addPointer(down2);
8889
tester.closeArena(2);
8990
tester.route(down2);
91+
expect(scale.pointerCount, 2);
9092

9193
expect(didEndScale, isTrue);
9294
didEndScale = false;

0 commit comments

Comments
 (0)