Skip to content

Commit f0c985d

Browse files
authored
[material_ui] Don't clip MenuItemButton.leadingIcon (flutter#12986)
`_MenuItemLabel` wrapped the leading `Row` in a hard-coded `ClipRect` when `overflowAxis` is vertical, which clipped `leadingIcon` to its own bounds. This clips any decoration painting outside those bounds, such as a `Badge`. This PR is to remove the `ClipRect` so leading icons won't be incorrectly clipped. Fixes flutter#192732 Before <img width="158" height="196" alt="badge_before_real" src="https://github.com/user-attachments/assets/4f1a5121-3908-4f5b-b1ea-f78803da7540" /> After <img width="158" height="196" alt="badge_after_real" src="https://github.com/user-attachments/assets/8c302692-5dcf-40d7-92bf-111a72437cfc" /> ## Pre-Review Checklist
1 parent 5eb35f5 commit f0c985d

3 files changed

Lines changed: 61 additions & 16 deletions

File tree

‎packages/material_ui/lib/src/menu_anchor.dart‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,24 +3327,22 @@ class _MenuItemLabel extends StatelessWidget {
33273327
Widget leadings;
33283328
if (overflowAxis == Axis.vertical) {
33293329
leadings = Expanded(
3330-
child: ClipRect(
3331-
child: Row(
3332-
mainAxisSize: MainAxisSize.min,
3333-
children: <Widget>[
3334-
?leadingIcon,
3335-
if (child != null)
3336-
Expanded(
3337-
child: ClipRect(
3338-
child: Padding(
3339-
padding: leadingIcon != null
3340-
? EdgeInsetsDirectional.only(start: horizontalPadding)
3341-
: EdgeInsets.zero,
3342-
child: child,
3343-
),
3330+
child: Row(
3331+
mainAxisSize: MainAxisSize.min,
3332+
children: <Widget>[
3333+
?leadingIcon,
3334+
if (child != null)
3335+
Expanded(
3336+
child: ClipRect(
3337+
child: Padding(
3338+
padding: leadingIcon != null
3339+
? EdgeInsetsDirectional.only(start: horizontalPadding)
3340+
: EdgeInsets.zero,
3341+
child: child,
33443342
),
33453343
),
3346-
],
3347-
),
3344+
),
3345+
],
33483346
),
33493347
);
33503348
} else {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes decorations on `MenuItemButton.leadingIcon`, such as `Badge`, being clipped to the icon's bounds in vertically expanding menus.
3+
version: patch

‎packages/material_ui/test/menu_anchor_test.dart‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,50 @@ void main() {
33373337
expect(exception, isAssertionError);
33383338
});
33393339

3340+
// This is a regression test for https://github.com/flutter/flutter/issues/192732.
3341+
testWidgets('MenuItemButton does not clip leadingIcon', (WidgetTester tester) async {
3342+
const label = 'This is a very long menu item label that must be clipped';
3343+
await tester.pumpWidget(
3344+
MaterialApp(
3345+
home: Scaffold(
3346+
body: MenuAnchor(
3347+
menuChildren: <Widget>[
3348+
SizedBox(
3349+
width: 200.0,
3350+
child: MenuItemButton(
3351+
leadingIcon: const Badge(
3352+
label: Text('1'),
3353+
child: Icon(Icons.add, key: Key('leading')),
3354+
),
3355+
onPressed: () {},
3356+
child: const Text(label, maxLines: 1),
3357+
),
3358+
),
3359+
],
3360+
builder: (BuildContext context, MenuController controller, Widget? child) {
3361+
return TextButton(onPressed: controller.open, child: const Text('Open'));
3362+
},
3363+
),
3364+
),
3365+
),
3366+
);
3367+
3368+
await tester.tap(find.text('Open'));
3369+
await tester.pumpAndSettle();
3370+
3371+
// Decorations that paint outside the leading icon's bounds stay visible.
3372+
expect(
3373+
find.ancestor(of: find.byKey(const Key('leading')), matching: find.byType(ClipRect)),
3374+
findsNothing,
3375+
);
3376+
3377+
// The label itself is still clipped so that long labels truncate.
3378+
final Finder labelFinder = find.text(label);
3379+
expect(find.ancestor(of: labelFinder, matching: find.byType(ClipRect)), findsOneWidget);
3380+
final RenderParagraph paragraph = tester.renderObject<RenderParagraph>(labelFinder);
3381+
expect(paragraph.didExceedMaxLines, isTrue);
3382+
});
3383+
33403384
testWidgets('MenuItemButton.styleFrom overlayColor overrides default overlay color', (
33413385
WidgetTester tester,
33423386
) async {

0 commit comments

Comments
 (0)