diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 34c616881f..a29a48d607 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -648,6 +648,11 @@ class _DropdownMenuBody extends MultiChildRenderObjectWidget { width: width, ); } + + @override + void updateRenderObject(BuildContext context, _RenderDropdownMenuBody renderObject) { + renderObject.width = width; + } } class _DropdownMenuBodyParentData extends ContainerBoxParentData { } @@ -657,10 +662,18 @@ class _RenderDropdownMenuBody extends RenderBox RenderBoxContainerDefaultsMixin { _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) { diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index a6e85f99db..e326411e4f 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -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> shortMenuItems = >[]; + + for (final ShortMenu value in ShortMenu.values) { + final DropdownMenuEntry entry = DropdownMenuEntry(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)); + expect(box.size.width, customWidth); + + // Update width + customWidth = 400.0; + await tester.pumpWidget(buildTest(themeData, shortMenuItems, width: customWidth)); + box = tester.firstRenderObject(find.byType(DropdownMenu)); + 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();