Add semanticLabel to CupertinoCheckbox (#151789)
This commit is contained in:
@@ -81,6 +81,7 @@ class CupertinoCheckbox extends StatefulWidget {
|
||||
this.autofocus = false,
|
||||
this.side,
|
||||
this.shape,
|
||||
this.semanticLabel,
|
||||
}) : assert(tristate || value != null);
|
||||
|
||||
/// Whether this checkbox is checked.
|
||||
@@ -171,6 +172,13 @@ class CupertinoCheckbox extends StatefulWidget {
|
||||
/// [RoundedRectangleBorder] with a circular corner radius of 4.0.
|
||||
final OutlinedBorder? shape;
|
||||
|
||||
/// The semantic label for the checkbox that will be announced by screen readers.
|
||||
///
|
||||
/// This is announced by assistive technologies (e.g TalkBack/VoiceOver).
|
||||
///
|
||||
/// This label does not show in the UI.
|
||||
final String? semanticLabel;
|
||||
|
||||
/// The width of a checkbox widget.
|
||||
static const double width = 18.0;
|
||||
|
||||
@@ -238,6 +246,7 @@ class _CupertinoCheckboxState extends State<CupertinoCheckbox> with TickerProvid
|
||||
?? CupertinoColors.white;
|
||||
|
||||
return Semantics(
|
||||
label: widget.semanticLabel,
|
||||
checked: widget.value ?? false,
|
||||
mixed: widget.tristate ? widget.value == null : null,
|
||||
child: buildToggleable(
|
||||
|
||||
@@ -77,7 +77,7 @@ class AnimatedIcon extends StatelessWidget {
|
||||
|
||||
/// Semantic label for the icon.
|
||||
///
|
||||
/// Announced in accessibility modes (e.g TalkBack/VoiceOver).
|
||||
/// Announced by assistive technologies (e.g TalkBack/VoiceOver).
|
||||
/// This label does not show in the UI.
|
||||
///
|
||||
/// See also:
|
||||
|
||||
@@ -394,7 +394,7 @@ class Checkbox extends StatefulWidget {
|
||||
/// {@template flutter.material.checkbox.semanticLabel}
|
||||
/// The semantic label for the checkbox that will be announced by screen readers.
|
||||
///
|
||||
/// This is announced in accessibility modes (e.g TalkBack/VoiceOver).
|
||||
/// This is announced by assistive technologies (e.g TalkBack/VoiceOver).
|
||||
///
|
||||
/// This label does not show in the UI.
|
||||
/// {@endtemplate}
|
||||
@@ -495,6 +495,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
|
||||
autofocus: widget.autofocus,
|
||||
side: widget.side,
|
||||
shape: widget.shape,
|
||||
semanticLabel: widget.semanticLabel,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ class Icon extends StatelessWidget {
|
||||
|
||||
/// Semantic label for the icon.
|
||||
///
|
||||
/// Announced in accessibility modes (e.g TalkBack/VoiceOver).
|
||||
/// Announced by assistive technologies (e.g TalkBack/VoiceOver).
|
||||
/// This label does not show in the UI.
|
||||
///
|
||||
/// * [SemanticsProperties.label], which is set to [semanticLabel] in the
|
||||
|
||||
@@ -57,7 +57,7 @@ class ImageIcon extends StatelessWidget {
|
||||
|
||||
/// Semantic label for the icon.
|
||||
///
|
||||
/// Announced in accessibility modes (e.g TalkBack/VoiceOver).
|
||||
/// Announced by assistive technologies (e.g TalkBack/VoiceOver).
|
||||
/// This label does not show in the UI.
|
||||
///
|
||||
/// * [SemanticsProperties.label], which is set to [semanticLabel] in the
|
||||
|
||||
@@ -338,6 +338,58 @@ void main() {
|
||||
semanticsTester.dispose();
|
||||
});
|
||||
|
||||
testWidgets('Checkbox can configure a semantic label', (WidgetTester tester) async {
|
||||
final SemanticsHandle handle = tester.ensureSemantics();
|
||||
|
||||
await tester.pumpWidget(
|
||||
CupertinoApp (
|
||||
home: Center(
|
||||
child: CupertinoCheckbox(
|
||||
value: false,
|
||||
onChanged: (bool? b) { },
|
||||
semanticLabel: 'checkbox',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getSemantics(find.byType(Focus).last), matchesSemantics(
|
||||
hasCheckedState: true,
|
||||
hasEnabledState: true,
|
||||
isEnabled: true,
|
||||
hasTapAction: true,
|
||||
hasFocusAction: true,
|
||||
isFocusable: true,
|
||||
label: 'checkbox'
|
||||
));
|
||||
|
||||
// If wrapped with semantics, both the parent semantic label and the
|
||||
// checkbox's semantic label are used in annotation.
|
||||
await tester.pumpWidget(
|
||||
CupertinoApp(
|
||||
home: Semantics(
|
||||
label: 'foo',
|
||||
textDirection: TextDirection.ltr,
|
||||
child: CupertinoCheckbox(
|
||||
value: false,
|
||||
onChanged: (bool? b) { },
|
||||
semanticLabel: 'checkbox',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.getSemantics(find.byType(Focus).last), matchesSemantics(
|
||||
label: 'foo\ncheckbox',
|
||||
textDirection: TextDirection.ltr,
|
||||
hasCheckedState: true,
|
||||
hasEnabledState: true,
|
||||
isEnabled: true,
|
||||
hasTapAction: true,
|
||||
hasFocusAction: true,
|
||||
isFocusable: true,
|
||||
));
|
||||
handle.dispose();
|
||||
});
|
||||
|
||||
testWidgets('Checkbox can be toggled by keyboard shortcuts', (WidgetTester tester) async {
|
||||
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
|
||||
bool? value = true;
|
||||
|
||||
Reference in New Issue
Block a user