Unverified Commit 00cd3eda authored by Valentin Vignal's avatar Valentin Vignal Committed by GitHub

Align the Popup Menu under its child (#125534)

Align the `PopupMenu` under its child.

Before:

![image](https://user-images.githubusercontent.com/32538273/234468262-beddb257-5f5a-422f-a678-c26efc52dc07.png)

After:

![image](https://user-images.githubusercontent.com/32538273/234468233-3ea4d741-b04c-4012-bdd7-a36ecedc029c.png)

*List which issues are fixed by this PR. You must list at least one issue.*

Fixes https://github.com/flutter/flutter/issues/125474

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
parent 25bbe5c5
...@@ -1241,12 +1241,16 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> { ...@@ -1241,12 +1241,16 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
final RenderBox button = context.findRenderObject()! as RenderBox; final RenderBox button = context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox; final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;
final PopupMenuPosition popupMenuPosition = widget.position ?? popupMenuTheme.position ?? PopupMenuPosition.over; final PopupMenuPosition popupMenuPosition = widget.position ?? popupMenuTheme.position ?? PopupMenuPosition.over;
final Offset offset; late Offset offset;
switch (popupMenuPosition) { switch (popupMenuPosition) {
case PopupMenuPosition.over: case PopupMenuPosition.over:
offset = widget.offset; offset = widget.offset;
case PopupMenuPosition.under: case PopupMenuPosition.under:
offset = Offset(0.0, button.size.height - (widget.padding.vertical / 2)) + widget.offset; offset = Offset(0.0, button.size.height) + widget.offset;
if (widget.child == null) {
// Remove the padding of the icon button.
offset -= Offset(0.0, widget.padding.vertical / 2);
}
} }
final RelativeRect position = RelativeRect.fromRect( final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints( Rect.fromPoints(
......
...@@ -3222,6 +3222,44 @@ void main() { ...@@ -3222,6 +3222,44 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(currentRouteSetting.name, '/'); expect(currentRouteSetting.name, '/');
}); });
testWidgets('Popup menu is positioned under the child', (WidgetTester tester) async {
final Key popupButtonKey = UniqueKey();
final Key childKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Column(
children: <Widget>[
PopupMenuButton<void>(
key: popupButtonKey,
position: PopupMenuPosition.under,
itemBuilder: (BuildContext context) {
return <PopupMenuEntry<void>>[
const PopupMenuItem<void>(
child: Text('Example'),
),
];
},
child: SizedBox(
key: childKey,
height: 50,
width: 50,
)
),
],
),
),
)
);
await tester.tap(find.byKey(popupButtonKey));
await tester.pumpAndSettle();
final Offset childBottomLeft = tester.getBottomLeft(find.byKey(childKey));
final Offset menuTopLeft = tester.getTopLeft(find.bySemanticsLabel('Popup menu'));
expect(childBottomLeft, menuTopLeft);
});
} }
class TestApp extends StatelessWidget { class TestApp extends StatelessWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment