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]. <!-- 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
This commit is contained in:
@@ -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<T> extends State<PopupMenuButton<T>> {
|
||||
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<T> extends State<PopupMenuButton<T>> {
|
||||
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(
|
||||
|
||||
@@ -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<String>(
|
||||
style: ButtonStyle(tapTargetSize: tapTargetSize),
|
||||
itemBuilder: (_) => <PopupMenuEntry<String>>[
|
||||
const PopupMenuItem<String>(
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user