@@ -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
0 commit comments