Skip to content

Commit f9dc752

Browse files
[widgets/shortcuts] Add includeSemantics property to the Shortcuts widget. (#152077)
Adds an `includeSemantics` property to the Shortcuts widget. @gspencergoog and I discussed this change to make introducing Shortcuts easier on the MenuAnchor widget. This change should also make testing semantics trees slightly less onerous (assuming this doesn't harm accessibility, that is). On the point of accessibility, I'm not sure what useful semantic information is exposed by the Shortcuts widget's semantic node, since it's basically just an unfocusable keyboard listener. However, I kept the `includeSemantics` defaulting to true so as to not break semantics tests in the Flutter repo. Fixes flutter/flutter#152076
1 parent 64d340a commit f9dc752

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

packages/flutter/lib/src/widgets/shortcuts.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ class Shortcuts extends StatefulWidget {
997997
required Map<ShortcutActivator, Intent> shortcuts,
998998
required this.child,
999999
this.debugLabel,
1000+
this.includeSemantics = true,
10001001
}) : _shortcuts = shortcuts,
10011002
manager = null;
10021003

@@ -1012,6 +1013,7 @@ class Shortcuts extends StatefulWidget {
10121013
required ShortcutManager this.manager,
10131014
required this.child,
10141015
this.debugLabel,
1016+
this.includeSemantics = true,
10151017
}) : _shortcuts = const <ShortcutActivator, Intent>{};
10161018

10171019
/// The [ShortcutManager] that will manage the mapping between key
@@ -1047,6 +1049,9 @@ class Shortcuts extends StatefulWidget {
10471049
/// unnecessarily with large default shortcut maps.
10481050
final String? debugLabel;
10491051

1052+
/// {@macro flutter.widgets.Focus.includeSemantics}
1053+
final bool includeSemantics;
1054+
10501055
@override
10511056
State<Shortcuts> createState() => _ShortcutsState();
10521057

@@ -1104,6 +1109,7 @@ class _ShortcutsState extends State<Shortcuts> {
11041109
debugLabel: '$Shortcuts',
11051110
canRequestFocus: false,
11061111
onKeyEvent: _handleOnKeyEvent,
1112+
includeSemantics: widget.includeSemantics,
11071113
child: widget.child,
11081114
);
11091115
}

packages/flutter/test/widgets/shortcuts_test.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:flutter/services.dart';
88
import 'package:flutter_test/flutter_test.dart';
99
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1010

11+
import 'semantics_tester.dart';
12+
1113
void main() {
1214
group(LogicalKeySet, () {
1315
test('LogicalKeySet passes parameters correctly.', () {
@@ -1255,6 +1257,51 @@ void main() {
12551257
expect(invoked, 1);
12561258
invoked = 0;
12571259
});
1260+
1261+
testWidgets('Shortcuts does not insert a semantics node when includeSemantics is false', (WidgetTester tester) async {
1262+
final SemanticsTester semanticsTester = SemanticsTester(tester);
1263+
addTearDown(semanticsTester.dispose);
1264+
1265+
// By default, includeSemantics is true.
1266+
await tester.pumpWidget(
1267+
const Shortcuts(
1268+
shortcuts: <LogicalKeySet, Intent>{},
1269+
child: SizedBox(),
1270+
),
1271+
);
1272+
1273+
expect(
1274+
semanticsTester,
1275+
hasSemantics(
1276+
TestSemantics.root(
1277+
children: <TestSemantics>[
1278+
TestSemantics(id: 1),
1279+
],
1280+
),
1281+
ignoreRect: true,
1282+
ignoreTransform: true,
1283+
),
1284+
);
1285+
1286+
await tester.pumpWidget(
1287+
const Shortcuts(
1288+
includeSemantics: false,
1289+
shortcuts: <LogicalKeySet, Intent>{},
1290+
child: SizedBox(),
1291+
),
1292+
);
1293+
1294+
expect(
1295+
semanticsTester,
1296+
hasSemantics(
1297+
TestSemantics.root(),
1298+
ignoreRect: true,
1299+
ignoreTransform: true,
1300+
),
1301+
);
1302+
1303+
semanticsTester.dispose();
1304+
});
12581305
});
12591306

12601307
group('CharacterActivator', () {

0 commit comments

Comments
 (0)