Skip to content

Commit

Permalink
DropdownMenu.width should support updating at runtime (flutter#124847)
Browse files Browse the repository at this point in the history
  • Loading branch information
xu-baolin authored Apr 20, 2023
1 parent b04efe4 commit 9f2e708
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/flutter/lib/src/material/dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@ class _DropdownMenuBody extends MultiChildRenderObjectWidget {
width: width,
);
}

@override
void updateRenderObject(BuildContext context, _RenderDropdownMenuBody renderObject) {
renderObject.width = width;
}
}

class _DropdownMenuBodyParentData extends ContainerBoxParentData<RenderBox> { }
Expand All @@ -657,10 +662,18 @@ class _RenderDropdownMenuBody extends RenderBox
RenderBoxContainerDefaultsMixin<RenderBox, _DropdownMenuBodyParentData> {

_RenderDropdownMenuBody({
this.width,
});
double? width,
}) : _width = width;

final double? width;
double? get width => _width;
double? _width;
set width(double? value) {
if (_width == value) {
return;
}
_width = value;
markNeedsLayout();
}

@override
void setupParentData(RenderBox child) {
Expand Down
22 changes: 22 additions & 0 deletions packages/flutter/test/material/dropdown_menu_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ void main() {
expect(buttonSize.width, customSmallWidth);
});

testWidgets('The width property update test', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/120567
final ThemeData themeData = ThemeData();
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);
}

double customWidth = 250.0;
await tester.pumpWidget(buildTest(themeData, shortMenuItems, width: customWidth));
RenderBox box = tester.firstRenderObject(find.byType(DropdownMenu<ShortMenu>));
expect(box.size.width, customWidth);

// Update width
customWidth = 400.0;
await tester.pumpWidget(buildTest(themeData, shortMenuItems, width: customWidth));
box = tester.firstRenderObject(find.byType(DropdownMenu<ShortMenu>));
expect(box.size.width, customWidth);
});

testWidgets('The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list',
(WidgetTester tester) async {
final ThemeData themeData = ThemeData();
Expand Down

0 comments on commit 9f2e708

Please sign in to comment.