Skip to content

Commit bbca61d

Browse files
authored
[material_ui] Announce PopupMenuButton with a child as a button (#12585)
Ports flutter/flutter#191700, which is blocked by the Material/Cupertino code freeze in flutter/flutter (see flutter/flutter#188444). ## Original description `PopupMenuButton` only exposed button semantics when it built its own `IconButton`. When a `child` was supplied, the widget just wrapped the child in a `Tooltip` and an `InkWell`, so the semantics node had a tap action but no button flag and no enabled state. Assistive technologies therefore did not announce it as a button, and disabled buttons looked enabled. The `expanded` state was also dropped entirely when `ButtonStyle.tapTargetSize` was `MaterialTapTargetSize.padded`, because that branch returned early before the `Semantics` wrapper was added. The `child` branch now always wraps the result in a single `Semantics` widget that marks the popup menu button as a button, reports the enabled state, and keeps the expanded state for both tap target sizes. Fixes flutter/flutter#147043 ## Test plan ## Pre-Review Checklist The change uses the repository's pending changelog workflow (`packages/material_ui/pending_changelogs/change_2026_08_25_port191700.yaml`). No public API documentation change is needed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent e32043a commit bbca61d

3 files changed

Lines changed: 131 additions & 10 deletions

File tree

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,16 +1797,25 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
17971797
);
17981798
final MaterialTapTargetSize tapTargetSize =
17991799
widget.style?.tapTargetSize ?? MaterialTapTargetSize.shrinkWrap;
1800-
if (tapTargetSize == MaterialTapTargetSize.padded) {
1801-
return ConstrainedBox(
1802-
constraints: const BoxConstraints(
1803-
minWidth: kMinInteractiveDimension,
1804-
minHeight: kMinInteractiveDimension,
1805-
),
1806-
child: child,
1807-
);
1808-
}
1809-
return Semantics(expanded: _isMenuExpanded, child: child);
1800+
final Widget result = tapTargetSize == MaterialTapTargetSize.padded
1801+
? ConstrainedBox(
1802+
constraints: const BoxConstraints(
1803+
minWidth: kMinInteractiveDimension,
1804+
minHeight: kMinInteractiveDimension,
1805+
),
1806+
child: child,
1807+
)
1808+
: child;
1809+
// The button semantics are added here rather than by the [InkWell] so
1810+
// that assistive technologies describe the popup menu button the same way
1811+
// regardless of whether it is built from [child] or from [icon], in which
1812+
// case the semantics come from the [IconButton].
1813+
return Semantics(
1814+
button: true,
1815+
enabled: widget.enabled,
1816+
expanded: _isMenuExpanded,
1817+
child: result,
1818+
);
18101819
}
18111820

18121821
return Semantics(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fix `PopupMenuButton` not announcing button semantics (enabled state, expanded state) when built with a `child` instead of the default icon.
3+
version: patch

‎packages/material_ui/test/popup_menu_test.dart‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,12 @@ void main() {
14101410
expect(
14111411
tester.getSemantics(find.byType(PopupMenuButton<int>)),
14121412
matchesSemantics(
1413+
isButton: true,
1414+
hasEnabledState: true,
1415+
isEnabled: true,
14131416
hasExpandedState: true,
14141417
label: 'XXX',
1418+
tooltip: 'Show menu',
14151419
hasTapAction: true,
14161420
hasFocusAction: true,
14171421
isFocusable: true,
@@ -1443,8 +1447,113 @@ void main() {
14431447
expect(
14441448
tester.getSemantics(find.byType(PopupMenuButton<int>)),
14451449
matchesSemantics(
1450+
isButton: true,
1451+
hasEnabledState: true,
1452+
isEnabled: true,
14461453
hasExpandedState: true,
14471454
label: 'XXX',
1455+
tooltip: 'Show menu',
1456+
hasTapAction: true,
1457+
hasFocusAction: true,
1458+
isFocusable: true,
1459+
),
1460+
);
1461+
});
1462+
1463+
testWidgets('PopupMenuButton with a child has button semantics', (WidgetTester tester) async {
1464+
// Regression test for https://github.com/flutter/flutter/issues/147043
1465+
await tester.pumpWidget(
1466+
MaterialApp(
1467+
home: Material(
1468+
child: PopupMenuButton<int>(
1469+
itemBuilder: (BuildContext context) {
1470+
return <PopupMenuItem<int>>[
1471+
const PopupMenuItem<int>(value: 1, child: Text('Item 1')),
1472+
];
1473+
},
1474+
child: const Text('XXX'),
1475+
),
1476+
),
1477+
),
1478+
);
1479+
1480+
expect(
1481+
tester.getSemantics(find.byType(PopupMenuButton<int>)),
1482+
matchesSemantics(
1483+
isButton: true,
1484+
hasEnabledState: true,
1485+
isEnabled: true,
1486+
hasExpandedState: true,
1487+
label: 'XXX',
1488+
tooltip: 'Show menu',
1489+
hasTapAction: true,
1490+
hasFocusAction: true,
1491+
isFocusable: true,
1492+
),
1493+
);
1494+
});
1495+
1496+
testWidgets('Disabled PopupMenuButton with a child has button semantics', (
1497+
WidgetTester tester,
1498+
) async {
1499+
// Regression test for https://github.com/flutter/flutter/issues/147043
1500+
await tester.pumpWidget(
1501+
MaterialApp(
1502+
home: Material(
1503+
child: PopupMenuButton<int>(
1504+
enabled: false,
1505+
itemBuilder: (BuildContext context) {
1506+
return <PopupMenuItem<int>>[
1507+
const PopupMenuItem<int>(value: 1, child: Text('Item 1')),
1508+
];
1509+
},
1510+
child: const Text('XXX'),
1511+
),
1512+
),
1513+
),
1514+
);
1515+
1516+
expect(
1517+
tester.getSemantics(find.byType(PopupMenuButton<int>)),
1518+
matchesSemantics(
1519+
isButton: true,
1520+
hasEnabledState: true,
1521+
hasExpandedState: true,
1522+
label: 'XXX',
1523+
tooltip: 'Show menu',
1524+
),
1525+
);
1526+
});
1527+
1528+
testWidgets('PopupMenuButton with a padded tap target has button semantics', (
1529+
WidgetTester tester,
1530+
) async {
1531+
// Regression test for https://github.com/flutter/flutter/issues/147043
1532+
await tester.pumpWidget(
1533+
MaterialApp(
1534+
home: Material(
1535+
child: PopupMenuButton<int>(
1536+
style: const ButtonStyle(tapTargetSize: MaterialTapTargetSize.padded),
1537+
itemBuilder: (BuildContext context) {
1538+
return <PopupMenuItem<int>>[
1539+
const PopupMenuItem<int>(value: 1, child: Text('Item 1')),
1540+
];
1541+
},
1542+
child: const Text('XXX'),
1543+
),
1544+
),
1545+
),
1546+
);
1547+
1548+
expect(
1549+
tester.getSemantics(find.byType(PopupMenuButton<int>)),
1550+
matchesSemantics(
1551+
isButton: true,
1552+
hasEnabledState: true,
1553+
isEnabled: true,
1554+
hasExpandedState: true,
1555+
label: 'XXX',
1556+
tooltip: 'Show menu',
14481557
hasTapAction: true,
14491558
hasFocusAction: true,
14501559
isFocusable: true,

0 commit comments

Comments
 (0)