[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
This commit is contained in:
davidhicks980
2024-07-23 11:46:08 -04:00
committed by GitHub
parent 64d340a503
commit f9dc7526fd
2 changed files with 53 additions and 0 deletions

View File

@@ -997,6 +997,7 @@ class Shortcuts extends StatefulWidget {
required Map<ShortcutActivator, Intent> 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 <ShortcutActivator, Intent>{};
/// 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<Shortcuts> createState() => _ShortcutsState();
@@ -1104,6 +1109,7 @@ class _ShortcutsState extends State<Shortcuts> {
debugLabel: '$Shortcuts',
canRequestFocus: false,
onKeyEvent: _handleOnKeyEvent,
includeSemantics: widget.includeSemantics,
child: widget.child,
);
}

View File

@@ -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: <LogicalKeySet, Intent>{},
child: SizedBox(),
),
);
expect(
semanticsTester,
hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics(id: 1),
],
),
ignoreRect: true,
ignoreTransform: true,
),
);
await tester.pumpWidget(
const Shortcuts(
includeSemantics: false,
shortcuts: <LogicalKeySet, Intent>{},
child: SizedBox(),
),
);
expect(
semanticsTester,
hasSemantics(
TestSemantics.root(),
ignoreRect: true,
ignoreTransform: true,
),
);
semanticsTester.dispose();
});
});
group('CharacterActivator', () {