Skip to content

Commit a140353

Browse files
vamQuncCccccc
authored andcommitted
Announce PopupMenuButton with a child as a button
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. Ported from flutter/flutter#191700, which is blocked by the Material code freeze. Fixes flutter/flutter#147043
1 parent 469b97b commit a140353

3 files changed

Lines changed: 119 additions & 3 deletions

File tree

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

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

18121822
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: 103 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,107 @@ 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>>[const PopupMenuItem<int>(value: 1, child: Text('Item 1'))];
1471+
},
1472+
child: const Text('XXX'),
1473+
),
1474+
),
1475+
),
1476+
);
1477+
1478+
expect(
1479+
tester.getSemantics(find.byType(PopupMenuButton<int>)),
1480+
matchesSemantics(
1481+
isButton: true,
1482+
hasEnabledState: true,
1483+
isEnabled: true,
1484+
hasExpandedState: true,
1485+
label: 'XXX',
1486+
tooltip: 'Show menu',
1487+
hasTapAction: true,
1488+
hasFocusAction: true,
1489+
isFocusable: true,
1490+
),
1491+
);
1492+
});
1493+
1494+
testWidgets('Disabled PopupMenuButton with a child has button semantics', (
1495+
WidgetTester tester,
1496+
) async {
1497+
// Regression test for https://github.com/flutter/flutter/issues/147043
1498+
await tester.pumpWidget(
1499+
MaterialApp(
1500+
home: Material(
1501+
child: PopupMenuButton<int>(
1502+
enabled: false,
1503+
itemBuilder: (BuildContext context) {
1504+
return <PopupMenuItem<int>>[const PopupMenuItem<int>(value: 1, child: Text('Item 1'))];
1505+
},
1506+
child: const Text('XXX'),
1507+
),
1508+
),
1509+
),
1510+
);
1511+
1512+
expect(
1513+
tester.getSemantics(find.byType(PopupMenuButton<int>)),
1514+
matchesSemantics(
1515+
isButton: true,
1516+
hasEnabledState: true,
1517+
hasExpandedState: true,
1518+
label: 'XXX',
1519+
tooltip: 'Show menu',
1520+
),
1521+
);
1522+
});
1523+
1524+
testWidgets('PopupMenuButton with a padded tap target has button semantics', (
1525+
WidgetTester tester,
1526+
) async {
1527+
// Regression test for https://github.com/flutter/flutter/issues/147043
1528+
await tester.pumpWidget(
1529+
MaterialApp(
1530+
home: Material(
1531+
child: PopupMenuButton<int>(
1532+
style: const ButtonStyle(tapTargetSize: MaterialTapTargetSize.padded),
1533+
itemBuilder: (BuildContext context) {
1534+
return <PopupMenuItem<int>>[const PopupMenuItem<int>(value: 1, child: Text('Item 1'))];
1535+
},
1536+
child: const Text('XXX'),
1537+
),
1538+
),
1539+
),
1540+
);
1541+
1542+
expect(
1543+
tester.getSemantics(find.byType(PopupMenuButton<int>)),
1544+
matchesSemantics(
1545+
isButton: true,
1546+
hasEnabledState: true,
1547+
isEnabled: true,
1548+
hasExpandedState: true,
1549+
label: 'XXX',
1550+
tooltip: 'Show menu',
14481551
hasTapAction: true,
14491552
hasFocusAction: true,
14501553
isFocusable: true,

0 commit comments

Comments
 (0)