Skip to content

Commit

Permalink
Fix DropdownMenu with expandedInsets always aligned on top (flutt…
Browse files Browse the repository at this point in the history
…er#156214)

Fixes [DropdownMenu can not be center aligned when using expandedInsets
](flutter#155581)

### Code sample

<details>
<summary>expand to view the code sample</summary> 

```dart
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @OverRide
  Widget build(BuildContext context) {
    final List<DropdownMenuEntry<ShortMenu>> shortMenuItems =
        <DropdownMenuEntry<ShortMenu>>[];
    for (final ShortMenu value in ShortMenu.values) {
      final DropdownMenuEntry<ShortMenu> entry =
          DropdownMenuEntry<ShortMenu>(value: value, label: value.label);
      shortMenuItems.add(entry);
    }

    return MaterialApp(
      home: Scaffold(
        body: Row(
          children: <Widget>[
            Expanded(
              child: Center(
                child: DropdownMenu<ShortMenu>(
                  expandedInsets: const EdgeInsets.all(16),
                  initialSelection: ShortMenu.item0,
                  dropdownMenuEntries: shortMenuItems,
                  label: const Text('With expandedInsets'),
                ),
              ),
            ),
            Expanded(
              child: Center(
                child: DropdownMenu<ShortMenu>(
                  initialSelection: ShortMenu.item0,
                  dropdownMenuEntries: shortMenuItems,
                  label: const Text('Without expandedInsets'),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

enum ShortMenu {
  item0('Menu 0'),
  item1('Menu 1'),
  item2('Menu 2');

  const ShortMenu(this.label);
  final String label;
}
```

</details>

### Before 
(`DropdownMenu` without `expandedInsets` cannot be centered)
<img width="770" alt="Screenshot 2024-10-04 at 14 13 58" src="https://github.com/user-attachments/assets/c7520c12-d16a-4867-8fae-38b75dbc4225">

### After
(`DropdownMenu` with `expandedInsets` be centered)

<img width="770" alt="Screenshot 2024-10-04 at 14 13 49" src="https://github.com/user-attachments/assets/82e0b81e-5c85-4e59-99b8-df329459773b">
  • Loading branch information
TahaTesser authored Oct 7, 2024
1 parent ef4807b commit b3de00a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/flutter/lib/src/material/dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -972,10 +972,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
),
),
),
child: Align(
alignment: AlignmentDirectional.topStart,
child: menuAnchor,
),
child: menuAnchor,
);
}

Expand Down
37 changes: 37 additions & 0 deletions packages/flutter/test/material/dropdown_menu_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3415,6 +3415,43 @@ void main() {
tester.getRect(find.byType(TextField).first).bottom,
);
});

testWidgets('DropdownMenu with expandedInsets can be aligned', (WidgetTester tester) async {
Widget buildMenuAnchor({ AlignmentGeometry alignment = Alignment.topCenter }) {
return MaterialApp(
home: Scaffold(
body: Row(
children: <Widget>[
Expanded(
child: Align(
alignment: alignment,
child: DropdownMenu<TestMenu>(
expandedInsets: const EdgeInsets.all(16),
dropdownMenuEntries: menuChildren,
),
),
),
],
),
),
);
}

await tester.pumpWidget(buildMenuAnchor());

Offset textFieldPosition = tester.getTopLeft(find.byType(TextField));
expect(textFieldPosition, equals(const Offset(16.0, 0.0)));

await tester.pumpWidget(buildMenuAnchor(alignment: Alignment.center));

textFieldPosition = tester.getTopLeft(find.byType(TextField));
expect(textFieldPosition, equals(const Offset(16.0, 272.0)));

await tester.pumpWidget(buildMenuAnchor(alignment: Alignment.bottomCenter));

textFieldPosition = tester.getTopLeft(find.byType(TextField));
expect(textFieldPosition, equals(const Offset(16.0, 544.0)));
});
}

enum TestMenu {
Expand Down

0 comments on commit b3de00a

Please sign in to comment.