Skip to content

Commit 9f2e708

Browse files
authored
DropdownMenu.width should support updating at runtime (#124847)
Fixes flutter/flutter#120567
1 parent b04efe4 commit 9f2e708

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/flutter/lib/src/material/dropdown_menu.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ class _DropdownMenuBody extends MultiChildRenderObjectWidget {
648648
width: width,
649649
);
650650
}
651+
652+
@override
653+
void updateRenderObject(BuildContext context, _RenderDropdownMenuBody renderObject) {
654+
renderObject.width = width;
655+
}
651656
}
652657

653658
class _DropdownMenuBodyParentData extends ContainerBoxParentData<RenderBox> { }
@@ -657,10 +662,18 @@ class _RenderDropdownMenuBody extends RenderBox
657662
RenderBoxContainerDefaultsMixin<RenderBox, _DropdownMenuBodyParentData> {
658663

659664
_RenderDropdownMenuBody({
660-
this.width,
661-
});
665+
double? width,
666+
}) : _width = width;
662667

663-
final double? width;
668+
double? get width => _width;
669+
double? _width;
670+
set width(double? value) {
671+
if (_width == value) {
672+
return;
673+
}
674+
_width = value;
675+
markNeedsLayout();
676+
}
664677

665678
@override
666679
void setupParentData(RenderBox child) {

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ void main() {
193193
expect(buttonSize.width, customSmallWidth);
194194
});
195195

196+
testWidgets('The width property update test', (WidgetTester tester) async {
197+
// Regression test for https://github.com/flutter/flutter/issues/120567
198+
final ThemeData themeData = ThemeData();
199+
final List<DropdownMenuEntry<ShortMenu>> shortMenuItems = <DropdownMenuEntry<ShortMenu>>[];
200+
201+
for (final ShortMenu value in ShortMenu.values) {
202+
final DropdownMenuEntry<ShortMenu> entry = DropdownMenuEntry<ShortMenu>(value: value, label: value.label);
203+
shortMenuItems.add(entry);
204+
}
205+
206+
double customWidth = 250.0;
207+
await tester.pumpWidget(buildTest(themeData, shortMenuItems, width: customWidth));
208+
RenderBox box = tester.firstRenderObject(find.byType(DropdownMenu<ShortMenu>));
209+
expect(box.size.width, customWidth);
210+
211+
// Update width
212+
customWidth = 400.0;
213+
await tester.pumpWidget(buildTest(themeData, shortMenuItems, width: customWidth));
214+
box = tester.firstRenderObject(find.byType(DropdownMenu<ShortMenu>));
215+
expect(box.size.width, customWidth);
216+
});
217+
196218
testWidgets('The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list',
197219
(WidgetTester tester) async {
198220
final ThemeData themeData = ThemeData();

0 commit comments

Comments
 (0)