Skip to content

Commit 0161303

Browse files
[material/dropdown_menu.dart] Refactor _RenderDropdownMenuBody.computeDryLayout (#176503)
_RenderDropdownMenuBody.computeDryLayout accesses this.constraints, but it should only access the constraints parameter passed into computeDryLayout. This PR removes this.constraints access. <img width="621" height="73" alt="image" src="https://github.com/user-attachments/assets/495573e3-3b91-4091-8a7c-76594c98e22f" /> Also, I removed a line in which the child's offset is being set in computeDryLayout. This appears to be an error: <img width="402" height="56" alt="image" src="https://github.com/user-attachments/assets/0045761d-c958-451b-a6ec-cbdf0fe7bd09" /> Finally, I'm curious whether there is a reason why only the first child is being used to set the height? Resolves flutter/flutter#176494 Blocking flutter/flutter#176494 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 204de75 commit 0161303

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,6 @@ class _RenderDropdownMenuBody extends RenderBox
14481448

14491449
@override
14501450
Size computeDryLayout(BoxConstraints constraints) {
1451-
final BoxConstraints constraints = this.constraints;
14521451
double maxWidth = 0.0;
14531452
double? maxHeight;
14541453
RenderBox? child = firstChild;
@@ -1460,22 +1459,17 @@ class _RenderDropdownMenuBody extends RenderBox
14601459
);
14611460

14621461
while (child != null) {
1463-
if (child == firstChild) {
1464-
final Size childSize = child.getDryLayout(innerConstraints);
1465-
maxHeight ??= childSize.height;
1466-
final _DropdownMenuBodyParentData childParentData =
1467-
child.parentData! as _DropdownMenuBodyParentData;
1468-
assert(child.parentData == childParentData);
1469-
child = childParentData.nextSibling;
1470-
continue;
1471-
}
14721462
final Size childSize = child.getDryLayout(innerConstraints);
1463+
1464+
// The first child is the TextField, which doesn't contribute to the
1465+
// menu's width calculation.
1466+
if (child != firstChild) {
1467+
maxWidth = math.max(maxWidth, childSize.width);
1468+
}
1469+
14731470
final _DropdownMenuBodyParentData childParentData =
14741471
child.parentData! as _DropdownMenuBodyParentData;
1475-
childParentData.offset = Offset.zero;
1476-
maxWidth = math.max(maxWidth, childSize.width);
14771472
maxHeight ??= childSize.height;
1478-
assert(child.parentData == childParentData);
14791473
child = childParentData.nextSibling;
14801474
}
14811475

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,32 @@ void main() {
953953
expect(dropdownWidth, menuWidth);
954954
});
955955

956+
// Regression test for https://github.com/flutter/flutter/issues/176501
957+
testWidgets('_RenderDropdownMenuBody.computeDryLayout does not access this.constraints', (
958+
WidgetTester tester,
959+
) async {
960+
await tester.pumpWidget(
961+
const MaterialApp(
962+
home: Scaffold(
963+
body: Center(
964+
child: _TestDryLayout(
965+
child: DropdownMenu<int>(
966+
dropdownMenuEntries: <DropdownMenuEntry<int>>[
967+
DropdownMenuEntry<int>(value: 1, label: 'One'),
968+
DropdownMenuEntry<int>(value: 2, label: 'Two'),
969+
],
970+
),
971+
),
972+
),
973+
),
974+
),
975+
);
976+
977+
// The test passes if no exception is thrown during the layout phase.
978+
expect(tester.takeException(), isNull);
979+
expect(find.byType(DropdownMenu<int>), findsOneWidget);
980+
});
981+
956982
testWidgets(
957983
'Material2 - The menuHeight property can be used to show a shorter scrollable menu list instead of the complete list',
958984
(WidgetTester tester) async {
@@ -4787,3 +4813,29 @@ enum ShortMenu {
47874813
const ShortMenu(this.label);
47884814
final String label;
47894815
}
4816+
4817+
// A helper widget that creates a render object designed to call `getDryLayout`
4818+
// on its child during its own `performLayout` phase. This is used to test
4819+
// that a child's `computeDryLayout` implementation is valid.
4820+
class _TestDryLayout extends SingleChildRenderObjectWidget {
4821+
const _TestDryLayout({super.child});
4822+
4823+
@override
4824+
RenderObject createRenderObject(BuildContext context) {
4825+
return _RenderTestDryLayout();
4826+
}
4827+
}
4828+
4829+
class _RenderTestDryLayout extends RenderProxyBox {
4830+
@override
4831+
void performLayout() {
4832+
if (child == null) {
4833+
size = constraints.smallest;
4834+
return;
4835+
}
4836+
4837+
child!.getDryLayout(constraints);
4838+
child!.layout(constraints, parentUsesSize: true);
4839+
size = child!.size;
4840+
}
4841+
}

0 commit comments

Comments
 (0)