Skip to content

Commit f779fae

Browse files
authored
fix: prevent NaN fling direction when tracked pointer offsets are zero (#2220)
1 parent 0a4b176 commit f779fae

2 files changed

Lines changed: 81 additions & 10 deletions

File tree

‎lib/src/gestures/map_interactive_viewer.dart‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -802,17 +802,14 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
802802
// gestures where the user changes direction during the drag.
803803
final flingOffset = _focalStartLocal - _lastFocalLocal;
804804
final finalSegment = _prevFocalLocal - _lastFocalLocal;
805-
final finalSegmentDistance = finalSegment.distance;
806805

807-
// Use final segment direction if available, otherwise fall back to overall
808-
// direction for edge cases where the final segment has no movement.
809-
final Offset direction;
810-
if (finalSegmentDistance > 0) {
811-
direction = finalSegment / finalSegmentDistance;
812-
} else {
813-
final flingDistance = flingOffset.distance;
814-
direction = flingOffset / flingDistance;
815-
}
806+
final direction = flingDirection(
807+
finalSegment: finalSegment,
808+
flingOffset: flingOffset,
809+
// `magnitude` is checked to be non-zero above, so this is always a
810+
// finite, non-zero-length direction and is a safe final fallback.
811+
velocityDirection: details.velocity.pixelsPerSecond / magnitude,
812+
);
816813
final distance = (Offset.zero & _camera.nonRotatedSize).shortestSide;
817814

818815
_flingAnimation = Tween<Offset>(
@@ -831,6 +828,31 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
831828
));
832829
}
833830

831+
/// Calculates the direction a fling gesture should continue in.
832+
///
833+
/// Prefers the direction of the final tracked pointer segment, falling
834+
/// back to the overall drag direction. If both [finalSegment] and
835+
/// [flingOffset] have zero length - which can happen even when the
836+
/// gesture recognizer reports a velocity above the fling threshold, since
837+
/// the velocity is fitted over multiple recent samples and is not
838+
/// necessarily proportional to the last tracked position deltas - falls
839+
/// back to [velocityDirection] to avoid a division by zero (which would
840+
/// otherwise produce a `NaN` direction and corrupt the camera position).
841+
@visibleForTesting
842+
static Offset flingDirection({
843+
required Offset finalSegment,
844+
required Offset flingOffset,
845+
required Offset velocityDirection,
846+
}) {
847+
final finalSegmentDistance = finalSegment.distance;
848+
if (finalSegmentDistance > 0) return finalSegment / finalSegmentDistance;
849+
850+
final flingDistance = flingOffset.distance;
851+
if (flingDistance > 0) return flingOffset / flingDistance;
852+
853+
return velocityDirection;
854+
}
855+
834856
void _handleTap(TapPosition position) {
835857
if (_ckrTriggered.value) return;
836858

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:flutter_map/src/gestures/map_interactive_viewer.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('MapInteractiveViewerState.flingDirection', () {
6+
test('uses the final segment direction when it has non-zero length', () {
7+
final direction = MapInteractiveViewerState.flingDirection(
8+
finalSegment: const Offset(10, 0),
9+
flingOffset: const Offset(100, 0),
10+
velocityDirection: const Offset(0, 1),
11+
);
12+
13+
expect(direction, const Offset(1, 0));
14+
});
15+
16+
test(
17+
'falls back to the overall drag direction when the final segment has '
18+
'zero length',
19+
() {
20+
final direction = MapInteractiveViewerState.flingDirection(
21+
finalSegment: Offset.zero,
22+
flingOffset: const Offset(0, -50),
23+
velocityDirection: const Offset(1, 0),
24+
);
25+
26+
expect(direction, const Offset(0, -1));
27+
},
28+
);
29+
30+
test(
31+
'falls back to the velocity direction instead of dividing by zero '
32+
'when both the final segment and the overall drag offset have zero '
33+
'length (regression test: this previously produced a NaN direction, '
34+
'which corrupted the camera position - '
35+
'https://github.com/fleaflet/flutter_map/issues/2199)',
36+
() {
37+
final direction = MapInteractiveViewerState.flingDirection(
38+
finalSegment: Offset.zero,
39+
flingOffset: Offset.zero,
40+
velocityDirection: const Offset(0.6, 0.8),
41+
);
42+
43+
expect(direction, const Offset(0.6, 0.8));
44+
expect(direction.dx.isFinite, isTrue);
45+
expect(direction.dy.isFinite, isTrue);
46+
},
47+
);
48+
});
49+
}

0 commit comments

Comments
 (0)