From 9992e9bc5c5c294b9c0f7710c7656bb34d3297c5 Mon Sep 17 00:00:00 2001 From: Hannah Jin Date: Tue, 12 Nov 2024 12:19:46 -0800 Subject: [PATCH] Support materialTapTargetSize in PopupMenuButton (#158357) fix https://github.com/flutter/flutter/issues/128697 PopupMenuButton can take customized child, in that case if ` widgt.style.tapTargetSize` is set to `padded`, PopupMenuButton should support it. if child is null, default child is an iconButton, which is already 48x48. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [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 --- .../flutter/lib/src/material/popup_menu.dart | 14 +++++++- .../test/material/popup_menu_test.dart | 35 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index ebbe302d71..fc13494539 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -28,6 +28,7 @@ import 'material_state.dart'; import 'popup_menu_theme.dart'; import 'text_theme.dart'; import 'theme.dart'; +import 'theme_data.dart'; import 'tooltip.dart'; // Examples can assume: @@ -1554,7 +1555,7 @@ class PopupMenuButtonState extends State> { assert(debugCheckHasMaterialLocalizations(context)); if (widget.child != null) { - return Tooltip( + final Widget child = Tooltip( message: widget.tooltip ?? MaterialLocalizations.of(context).showMenuTooltip, child: InkWell( borderRadius: widget.borderRadius, @@ -1565,6 +1566,17 @@ class PopupMenuButtonState extends State> { child: widget.child, ), ); + final MaterialTapTargetSize tapTargetSize = widget.style?.tapTargetSize ?? MaterialTapTargetSize.shrinkWrap; + if (tapTargetSize == MaterialTapTargetSize.padded) { + return ConstrainedBox( + constraints: const BoxConstraints( + minWidth: kMinInteractiveDimension, + minHeight: kMinInteractiveDimension, + ), + child: child, + ); + } + return child; } return IconButton( diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 3a1a76d546..a6a56c5577 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -4318,6 +4318,41 @@ void main() { expect(inkWell.borderRadius, borderRadius); }); + testWidgets('PopupMenuButton respects materialTapTargetSize', (WidgetTester tester) async { + const double buttonSize = 10.0; + + + Widget buildPopupMenu({required MaterialTapTargetSize tapTargetSize}) { + return MaterialApp( + home: Scaffold( + body: Center( + child: PopupMenuButton( + style: ButtonStyle(tapTargetSize: tapTargetSize), + itemBuilder: (_) => >[ + const PopupMenuItem( + value: 'value', + child: Text('Item 0'), + ), + ], + child: const SizedBox(height: buttonSize, width: buttonSize), + ), + ), + ), + ); + } + // Popup menu with MaterialTapTargetSize.padded. + await tester.pumpWidget(buildPopupMenu(tapTargetSize: MaterialTapTargetSize.padded)); + await tester.pumpAndSettle(); + + expect(tester.getSize(find.byType(InkWell)), const Size(48.0, 48.0)); + + // Popup menu with MaterialTapTargetSize.shrinkWrap. + await tester.pumpWidget(buildPopupMenu(tapTargetSize: MaterialTapTargetSize.shrinkWrap)); + await tester.pumpAndSettle(); + + expect(tester.getSize(find.byType(InkWell)), const Size(buttonSize, buttonSize)); + }); + testWidgets('If requestFocus is false, the original focus should be preserved upon menu appearance.', (WidgetTester tester) async { final FocusNode fieldFocusNode = FocusNode(); addTearDown(fieldFocusNode.dispose);