Skip to content

Commit b5321d1

Browse files
authored
Reland "Fix position of CupertinoContextMenu within Transform.scale" (#102943)
* Fix position of CupertinoContextMenu within Transform.scale (#97896) * Fix after rebase
1 parent b3838eb commit b5321d1

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

packages/flutter/lib/src/cupertino/context_menu.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ typedef _ContextMenuPreviewBuilderChildless = Widget Function(
4747
Rect _getRect(GlobalKey globalKey) {
4848
assert(globalKey.currentContext != null);
4949
final RenderBox renderBoxContainer = globalKey.currentContext!.findRenderObject()! as RenderBox;
50-
final Offset containerOffset = renderBoxContainer.localToGlobal(
50+
return Rect.fromPoints(renderBoxContainer.localToGlobal(
5151
renderBoxContainer.paintBounds.topLeft,
52-
);
53-
return containerOffset & renderBoxContainer.paintBounds.size;
52+
), renderBoxContainer.localToGlobal(
53+
renderBoxContainer.paintBounds.bottomRight
54+
));
5455
}
5556

5657
// The context menu arranges itself slightly differently based on the location

packages/flutter/test/cupertino/context_menu_test.dart

+56
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,62 @@ void main() {
228228
kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
229229
);
230230
});
231+
232+
testWidgets('CupertinoContextMenu is in the correct position when within a Transform.scale', (WidgetTester tester) async {
233+
final Widget child = getChild();
234+
await tester.pumpWidget(CupertinoApp(
235+
home: CupertinoPageScaffold(
236+
child: MediaQuery(
237+
data: const MediaQueryData(size: Size(800, 600)),
238+
child: Transform.scale(
239+
scale: 0.5,
240+
child: Align(
241+
//alignment: Alignment.bottomRight,
242+
child: CupertinoContextMenu(
243+
actions: const <CupertinoContextMenuAction>[
244+
CupertinoContextMenuAction(
245+
child: Text('CupertinoContextMenuAction'),
246+
),
247+
],
248+
child: child
249+
),
250+
)
251+
)
252+
)
253+
)
254+
));
255+
expect(find.byWidget(child), findsOneWidget);
256+
final Rect childRect = tester.getRect(find.byWidget(child));
257+
expect(find.byType(ShaderMask), findsNothing);
258+
259+
// Start a press on the child.
260+
final TestGesture gesture = await tester.startGesture(childRect.center);
261+
await tester.pump();
262+
263+
// The _DecoyChild is showing directly on top of the child.
264+
expect(findDecoyChild(child), findsOneWidget);
265+
Rect decoyChildRect = tester.getRect(findDecoyChild(child));
266+
expect(childRect, equals(decoyChildRect));
267+
268+
expect(find.byType(ShaderMask), findsOneWidget);
269+
270+
// After a small delay, the _DecoyChild has begun to animate.
271+
await tester.pump(const Duration(milliseconds: 100));
272+
decoyChildRect = tester.getRect(findDecoyChild(child));
273+
expect(childRect, isNot(equals(decoyChildRect)));
274+
275+
// Eventually the decoy fully scales by _kOpenSize.
276+
await tester.pump(const Duration(milliseconds: 500));
277+
decoyChildRect = tester.getRect(findDecoyChild(child));
278+
expect(childRect, isNot(equals(decoyChildRect)));
279+
expect(decoyChildRect.width, childRect.width * kOpenScale);
280+
281+
// Then the CupertinoContextMenu opens.
282+
await tester.pumpAndSettle();
283+
await gesture.up();
284+
await tester.pumpAndSettle();
285+
expect(findStatic(), findsOneWidget);
286+
});
231287
});
232288

233289
group('CupertinoContextMenu when open', () {

packages/flutter/test/material/flexible_space_bar_test.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ void main() {
473473
Rect.fromLTRB(
474474
0,
475475
height - titleFontSize - 10,
476-
(width / 1.5).floorToDouble(),
477-
height - 10,
476+
(width / 1.5).floorToDouble() * 1.5,
477+
height,
478478
),
479479
);
480480
});
@@ -545,8 +545,8 @@ void main() {
545545
Rect.fromLTRB(
546546
0,
547547
height - titleFontSize - bottomMargin,
548-
(collapsedWidth / 3).floorToDouble(),
549-
height - bottomMargin,
548+
(collapsedWidth / 3).floorToDouble() * 3,
549+
height,
550550
),
551551
);
552552
});

packages/flutter/test/material/input_decorator_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -4321,17 +4321,17 @@ void main() {
43214321
await pumpDecorator(focused: false, empty: false);
43224322
await tester.pumpAndSettle();
43234323
expect(getLabelRect(tester).topLeft, equals(const Offset(12, -5.5)));
4324-
expect(getLabelRect(tester).size, equals(const Size(80, 16)));
4324+
expect(getLabelRect(tester).size, equals(const Size(80, 16) * 0.75));
43254325

43264326
await pumpDecorator(focused: true);
43274327
await tester.pumpAndSettle();
43284328
expect(getLabelRect(tester).topLeft, equals(const Offset(12, -5.5)));
4329-
expect(getLabelRect(tester).size, equals(const Size(80, 16)));
4329+
expect(getLabelRect(tester).size, equals(const Size(80, 16) * 0.75));
43304330

43314331
await pumpDecorator(focused: true, empty: false);
43324332
await tester.pumpAndSettle();
43334333
expect(getLabelRect(tester).topLeft, equals(const Offset(12, -5.5)));
4334-
expect(getLabelRect(tester).size, equals(const Size(80, 16)));
4334+
expect(getLabelRect(tester).size, equals(const Size(80, 16) * 0.75));
43354335

43364336
await pumpDecorator(focused: false, enabled: false);
43374337
await tester.pumpAndSettle();
@@ -4341,7 +4341,7 @@ void main() {
43414341
await pumpDecorator(focused: false, empty: false, enabled: false);
43424342
await tester.pumpAndSettle();
43434343
expect(getLabelRect(tester).topLeft, equals(const Offset(12, -5.5)));
4344-
expect(getLabelRect(tester).size, equals(const Size(80, 16)));
4344+
expect(getLabelRect(tester).size, equals(const Size(80, 16) * 0.75));
43454345

43464346
// Focused and disabled happens with NavigationMode.directional.
43474347
await pumpDecorator(focused: true, enabled: false);
@@ -4352,7 +4352,7 @@ void main() {
43524352
await pumpDecorator(focused: true, empty: false, enabled: false);
43534353
await tester.pumpAndSettle();
43544354
expect(getLabelRect(tester).topLeft, equals(const Offset(12, -5.5)));
4355-
expect(getLabelRect(tester).size, equals(const Size(80, 16)));
4355+
expect(getLabelRect(tester).size, equals(const Size(80, 16) * 0.75));
43564356
});
43574357

43584358
testWidgets('InputDecorationTheme.toString()', (WidgetTester tester) async {

packages/flutter_test/lib/src/controller.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ abstract class WidgetController {
11381138

11391139
/// Returns the rect of the given widget. This is only valid once
11401140
/// the widget's render object has been laid out at least once.
1141-
Rect getRect(Finder finder) => getTopLeft(finder) & getSize(finder);
1141+
Rect getRect(Finder finder) => Rect.fromPoints(getTopLeft(finder), getBottomRight(finder));
11421142

11431143
/// Attempts to find the [SemanticsNode] of first result from `finder`.
11441144
///

0 commit comments

Comments
 (0)