From f9dc7526fd392badc3e7fcfb1c83a3ab70916040 Mon Sep 17 00:00:00 2001 From: davidhicks980 <59215665+davidhicks980@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:46:08 -0400 Subject: [PATCH] [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 https://github.com/flutter/flutter/issues/152076 --- .../flutter/lib/src/widgets/shortcuts.dart | 6 +++ .../flutter/test/widgets/shortcuts_test.dart | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+) 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', () {