Skip to content

Commit 83af8ea

Browse files
WudgeJalkclaude
andcommitted
fix: normalise raw rotation angle to [-180, 180)
`ScaleUpdateDetails.rotation` is `atan2(now) - atan2(start)`, not normalised: two fingers side by side rotated by 4° report -356°, which wins the multi-finger gesture race as a rotation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 34457c1 commit 83af8ea

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎lib/src/gestures/map_interactive_viewer.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,10 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
555555
return;
556556
}
557557

558-
final currentRotation = details.rotation * radians2Degrees;
558+
// `details.rotation` is a raw difference of two `atan2` angles: two fingers
559+
// side by side rotated by 4° may report -356°. Normalise to [-180, 180).
560+
final currentRotation =
561+
(details.rotation * radians2Degrees + 180) % 360 - 180;
559562
if (_dragMode) {
560563
_handleScaleDragUpdate(details);
561564
} else if (InteractiveFlag.hasMultiFinger(_interactionOptions.flags)) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'dart:math' as math;
2+
3+
import 'package:flutter/widgets.dart';
4+
import 'package:flutter_map/flutter_map.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:latlong2/latlong.dart';
7+
8+
/// Two fingers around [c], [scale] times their initial 100 px apart, the
9+
/// line between them rotated by [degrees]. At 0° the line runs along the x
10+
/// axis with the first finger to the left: `atan2` of the start line is 180°,
11+
/// so a small rotation crosses ±180°.
12+
List<Offset> _fingers(Offset c, double scale, double degrees) {
13+
final a = degrees * math.pi / 180;
14+
final v = Offset(math.cos(a), math.sin(a)) * 50 * scale;
15+
return [c - v, c + v];
16+
}
17+
18+
void main() {
19+
testWidgets('a small rotation across ±180° does not win the gesture race',
20+
(tester) async {
21+
final controller = MapController();
22+
await tester.pumpWidget(
23+
Directionality(
24+
textDirection: TextDirection.ltr,
25+
child: Center(
26+
child: SizedBox(
27+
width: 400,
28+
height: 400,
29+
child: FlutterMap(
30+
mapController: controller,
31+
options: const MapOptions(
32+
initialCenter: LatLng(46.5, 6.6),
33+
initialZoom: 10,
34+
interactionOptions: InteractionOptions(
35+
enableMultiFingerGestureRace: true,
36+
),
37+
),
38+
children: const [],
39+
),
40+
),
41+
),
42+
),
43+
);
44+
45+
final c = tester.getCenter(find.byType(FlutterMap));
46+
final start = _fingers(c, 1, 0);
47+
final g1 = await tester.startGesture(start[0], pointer: 1);
48+
final g2 = await tester.startGesture(start[1], pointer: 2);
49+
await tester.pump();
50+
// Spread the fingers too, so the gesture passes the touch slop.
51+
for (final (scale, degrees) in [(1.2, 2.0), (1.4, 4.0)]) {
52+
final p = _fingers(c, scale, degrees);
53+
await g1.moveTo(p[0]);
54+
await g2.moveTo(p[1]);
55+
await tester.pump();
56+
}
57+
await g1.up();
58+
await g2.up();
59+
await tester.pumpAndSettle();
60+
// Let the double tap timer expire.
61+
await tester.pump(const Duration(seconds: 1));
62+
63+
// 4° is below the default rotationThreshold (20°): rotation must not win
64+
// the race, which the raw -356° value made it do.
65+
expect(controller.camera.rotation, 0);
66+
});
67+
}

0 commit comments

Comments
 (0)