diff --git a/packages/flutter/lib/src/widgets/shortcuts.dart b/packages/flutter/lib/src/widgets/shortcuts.dart index b0cdc1713a..ec9e886080 100644 --- a/packages/flutter/lib/src/widgets/shortcuts.dart +++ b/packages/flutter/lib/src/widgets/shortcuts.dart @@ -997,6 +997,7 @@ class Shortcuts extends StatefulWidget { required Map shortcuts, required this.child, this.debugLabel, + this.includeSemantics = true, }) : _shortcuts = shortcuts, manager = null; @@ -1012,6 +1013,7 @@ class Shortcuts extends StatefulWidget { required ShortcutManager this.manager, required this.child, this.debugLabel, + this.includeSemantics = true, }) : _shortcuts = const {}; /// The [ShortcutManager] that will manage the mapping between key @@ -1047,6 +1049,9 @@ class Shortcuts extends StatefulWidget { /// unnecessarily with large default shortcut maps. final String? debugLabel; + /// {@macro flutter.widgets.Focus.includeSemantics} + final bool includeSemantics; + @override State createState() => _ShortcutsState(); @@ -1104,6 +1109,7 @@ class _ShortcutsState extends State { debugLabel: '$Shortcuts', canRequestFocus: false, onKeyEvent: _handleOnKeyEvent, + includeSemantics: widget.includeSemantics, child: widget.child, ); } diff --git a/packages/flutter/test/widgets/shortcuts_test.dart b/packages/flutter/test/widgets/shortcuts_test.dart index 20d656d889..6863d2449f 100644 --- a/packages/flutter/test/widgets/shortcuts_test.dart +++ b/packages/flutter/test/widgets/shortcuts_test.dart @@ -8,6 +8,8 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; +import 'semantics_tester.dart'; + void main() { group(LogicalKeySet, () { test('LogicalKeySet passes parameters correctly.', () { @@ -1255,6 +1257,51 @@ void main() { expect(invoked, 1); invoked = 0; }); + + testWidgets('Shortcuts does not insert a semantics node when includeSemantics is false', (WidgetTester tester) async { + final SemanticsTester semanticsTester = SemanticsTester(tester); + addTearDown(semanticsTester.dispose); + + // By default, includeSemantics is true. + await tester.pumpWidget( + const Shortcuts( + shortcuts: {}, + child: SizedBox(), + ), + ); + + expect( + semanticsTester, + hasSemantics( + TestSemantics.root( + children: [ + TestSemantics(id: 1), + ], + ), + ignoreRect: true, + ignoreTransform: true, + ), + ); + + await tester.pumpWidget( + const Shortcuts( + includeSemantics: false, + shortcuts: {}, + child: SizedBox(), + ), + ); + + expect( + semanticsTester, + hasSemantics( + TestSemantics.root(), + ignoreRect: true, + ignoreTransform: true, + ), + ); + + semanticsTester.dispose(); + }); }); group('CharacterActivator', () {