Skip to content

Commit e2c7311

Browse files
EdwynZNQuncCccccc
andauthored
Fix default minimumSize in dropdownMenu when maximumSize is null (flutter#169438)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Previous change in `DropDownMenu` flutter#162380 forced the menu width to be constrained between the width or anchorWidth and the maximumSize width menuStyle, but giving a minimum of zero when maximumSize was not enforced or null. This prevents width or anchorWidth to be used at all as the minimum will be zero always if maximumSize is null Now this sets the default value back to width or anchorWidth if maximumSize is null, keeping consistency of the previous configuration while using the fix of maximumSize when provided. Fixes flutter#170970 ## 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]. <!-- 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 --------- Co-authored-by: Qun Cheng <[email protected]>
1 parent d085195 commit e2c7311

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
10441044
final double? effectiveMaximumWidth = effectiveMenuStyle!.maximumSize
10451045
?.resolve(states)
10461046
?.width;
1047-
return Size(math.min(widget.width!, effectiveMaximumWidth ?? 0.0), 0.0);
1047+
return Size(math.min(widget.width!, effectiveMaximumWidth ?? widget.width!), 0.0);
10481048
}),
10491049
);
10501050
} else if (anchorWidth != null) {
@@ -1053,7 +1053,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
10531053
final double? effectiveMaximumWidth = effectiveMenuStyle!.maximumSize
10541054
?.resolve(states)
10551055
?.width;
1056-
return Size(math.min(anchorWidth, effectiveMaximumWidth ?? 0.0), 0.0);
1056+
return Size(math.min(anchorWidth, effectiveMaximumWidth ?? anchorWidth), 0.0);
10571057
}),
10581058
);
10591059
}

packages/flutter/test/material/dropdown_menu_test.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4301,8 +4301,77 @@ void main() {
43014301

43024302
expect(tester.takeException(), isNull);
43034303
expect(tester.getSize(findMenuItemButton(menuChildren.first.label)).width, 150.0);
4304+
4305+
// The overwrite of menuStyle is different when a width is provided but maximumSize is not,
4306+
// So it needs to be tested separately.
4307+
await tester.pumpWidget(
4308+
MaterialApp(
4309+
home: Scaffold(
4310+
body: DropdownMenu<TestMenu>(
4311+
width: 200.0,
4312+
dropdownMenuEntries: menuChildren,
4313+
menuStyle: const MenuStyle(),
4314+
),
4315+
),
4316+
),
4317+
);
4318+
4319+
await tester.tap(find.byType(TextField));
4320+
await tester.pumpAndSettle();
4321+
4322+
expect(tester.getSize(findMenuItemButton(menuChildren.first.label)).width, 200.0);
43044323
});
43054324

4325+
testWidgets(
4326+
'ensure items are constrained to intrinsic size of DropdownMenu (width or anchor) when no maximumSize',
4327+
(WidgetTester tester) async {
4328+
const String shortLabel = 'Male';
4329+
await tester.pumpWidget(
4330+
const MaterialApp(
4331+
home: Scaffold(
4332+
body: DropdownMenu<int>(
4333+
width: 200,
4334+
dropdownMenuEntries: <DropdownMenuEntry<int>>[
4335+
DropdownMenuEntry<int>(value: 0, label: shortLabel),
4336+
],
4337+
menuStyle: MenuStyle(),
4338+
),
4339+
),
4340+
),
4341+
);
4342+
4343+
await tester.tap(find.byType(TextField));
4344+
await tester.pumpAndSettle();
4345+
4346+
expect(tester.getSize(findMenuItemButton(shortLabel)).width, 200);
4347+
4348+
// Use expandedInsets to anchor the TextField to the same size as the parent.
4349+
await tester.pumpWidget(
4350+
const MaterialApp(
4351+
home: Scaffold(
4352+
body: SizedBox(
4353+
width: double.infinity,
4354+
child: DropdownMenu<int>(
4355+
expandedInsets: EdgeInsets.symmetric(horizontal: 20),
4356+
dropdownMenuEntries: <DropdownMenuEntry<int>>[
4357+
DropdownMenuEntry<int>(value: 0, label: shortLabel),
4358+
],
4359+
menuStyle: MenuStyle(),
4360+
),
4361+
),
4362+
),
4363+
),
4364+
);
4365+
4366+
await tester.tap(find.byType(TextField));
4367+
await tester.pumpAndSettle();
4368+
4369+
expect(tester.takeException(), isNull);
4370+
// Default width is 800, so the expected width is 800 - padding (20 + 20).
4371+
expect(tester.getSize(findMenuItemButton(shortLabel)).width, 760.0);
4372+
},
4373+
);
4374+
43064375
// Regression test for https://github.com/flutter/flutter/issues/164905.
43074376
testWidgets('ensure exclude semantics for trailing button', (WidgetTester tester) async {
43084377
final SemanticsTester semantics = SemanticsTester(tester);

0 commit comments

Comments
 (0)