Skip to content

Commit

Permalink
Add alignmentOffset when menu is positioned on the opposite side (#12…
Browse files Browse the repository at this point in the history
…2812)

Add alignmentOffset when menu is positioned on the opposite side
  • Loading branch information
whiskeyPeak authored Mar 28, 2023
1 parent ed713ad commit d173c66
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/material/menu_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ class _MenuLayout extends SingleChildLayoutDelegate {
if (parentOrientation != orientation) {
x = allowedRect.left;
} else {
final double newX = anchorRect.right;
final double newX = anchorRect.right + alignmentOffset.dx;
if (!offRightSide(newX)) {
x = newX;
} else {
Expand All @@ -3133,7 +3133,7 @@ class _MenuLayout extends SingleChildLayoutDelegate {
if (parentOrientation != orientation) {
x = allowedRect.right - childSize.width;
} else {
final double newX = anchorRect.left - childSize.width;
final double newX = anchorRect.left - childSize.width - alignmentOffset.dx;
if (!offLeftSide(newX)) {
x = newX;
} else {
Expand Down
154 changes: 154 additions & 0 deletions packages/flutter/test/material/menu_anchor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,160 @@ void main() {
);
});

testWidgets('constrained menus show up in the right place with offset in LTR', (WidgetTester tester) async {
await changeSurfaceSize(tester, const Size(800, 600));
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Align(
alignment: Alignment.topLeft,
child: MenuAnchor(
menuChildren: const <Widget> [
SubmenuButton(
alignmentOffset: Offset(10, 0),
menuChildren: <Widget> [
SubmenuButton(
menuChildren: <Widget> [
SubmenuButton(
alignmentOffset: Offset(10, 0),
menuChildren: <Widget> [
SubmenuButton(
menuChildren: <Widget> [
],
child: Text('SubMenuButton4'),
),
],
child: Text('SubMenuButton3'),
),
],
child: Text('SubMenuButton2'),
),
],
child: Text('SubMenuButton1'),
),
],
builder: (BuildContext context, MenuController controller, Widget? child) {
return FilledButton(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('Tap me'),
);
},
),
),
);
},
),
),
);
await tester.pump();

await tester.tap(find.text('Tap me'));
await tester.pump();
await tester.tap(find.text('SubMenuButton1'));
await tester.pump();
await tester.tap(find.text('SubMenuButton2'));
await tester.pump();
await tester.tap(find.text('SubMenuButton3'));
await tester.pump();

expect(find.byType(SubmenuButton), findsNWidgets(4));
expect(
collectSubmenuRects(),
equals(const <Rect>[
Rect.fromLTRB(0.0, 48.0, 256.0, 112.0),
Rect.fromLTRB(266.0, 48.0, 522.0, 112.0),
Rect.fromLTRB(522.0, 48.0, 778.0, 112.0),
Rect.fromLTRB(256.0, 48.0, 512.0, 112.0),
]),
);
});

testWidgets('constrained menus show up in the right place with offset in RTL', (WidgetTester tester) async {
await changeSurfaceSize(tester, const Size(800, 600));
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Align(
alignment: Alignment.topRight,
child: MenuAnchor(
menuChildren: const <Widget> [
SubmenuButton(
alignmentOffset: Offset(10, 0),
menuChildren: <Widget> [
SubmenuButton(
menuChildren: <Widget> [
SubmenuButton(
alignmentOffset: Offset(10, 0),
menuChildren: <Widget> [
SubmenuButton(
menuChildren: <Widget> [
],
child: Text('SubMenuButton4'),
),
],
child: Text('SubMenuButton3'),
),
],
child: Text('SubMenuButton2'),
),
],
child: Text('SubMenuButton1'),
),
],
builder: (BuildContext context, MenuController controller, Widget? child) {
return FilledButton(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('Tap me'),
);
},
),
),
);
},
),
),
);
await tester.pump();

await tester.tap(find.text('Tap me'));
await tester.pump();
await tester.tap(find.text('SubMenuButton1'));
await tester.pump();
await tester.tap(find.text('SubMenuButton2'));
await tester.pump();
await tester.tap(find.text('SubMenuButton3'));
await tester.pump();

expect(find.byType(SubmenuButton), findsNWidgets(4));
expect(
collectSubmenuRects(),
equals(const <Rect>[
Rect.fromLTRB(544.0, 48.0, 800.0, 112.0),
Rect.fromLTRB(278.0, 48.0, 534.0, 112.0),
Rect.fromLTRB(22.0, 48.0, 278.0, 112.0),
Rect.fromLTRB(288.0, 48.0, 544.0, 112.0),
]),
);
});

Future<void> buildDensityPaddingApp(WidgetTester tester, {
required TextDirection textDirection,
VisualDensity visualDensity = VisualDensity.standard,
Expand Down

0 comments on commit d173c66

Please sign in to comment.