Skip to content

Commit 4d3c122

Browse files
yaakovschectmancbrackenloic-sharma
authored
Use tristate checkbox engine changes (#111032)
* Introduce tests for tristate checkboxes * Initial * Use tristate changes in engine * Flutter_test matchers test update * Comments, tests * Update packages/flutter/lib/src/semantics/semantics.dart Co-authored-by: Chris Bracken <[email protected]> * Assert mutual exclusivity * Assert valid state before updating state * Update packages/flutter/lib/src/semantics/semantics.dart Typo fix in comment Co-authored-by: Loïc Sharma <[email protected]> Co-authored-by: Chris Bracken <[email protected]> Co-authored-by: Loïc Sharma <[email protected]>
1 parent 70f6bed commit 4d3c122

10 files changed

Lines changed: 158 additions & 8 deletions

File tree

‎packages/flutter/lib/src/material/checkbox.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
474474

475475
return Semantics(
476476
checked: widget.value ?? false,
477+
mixed: widget.tristate ? widget.value == null : null,
477478
child: buildToggleable(
478479
mouseCursor: effectiveMouseCursor,
479480
focusNode: widget.focusNode,

‎packages/flutter/lib/src/rendering/custom_paint.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,9 @@ class RenderCustomPaint extends RenderProxyBox {
872872
if (properties.checked != null) {
873873
config.isChecked = properties.checked;
874874
}
875+
if (properties.mixed != null) {
876+
config.isCheckStateMixed = properties.mixed;
877+
}
875878
if (properties.selected != null) {
876879
config.isSelected = properties.selected!;
877880
}

‎packages/flutter/lib/src/rendering/proxy_box.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4355,6 +4355,9 @@ class RenderSemanticsAnnotations extends RenderProxyBox {
43554355
if (_properties.checked != null) {
43564356
config.isChecked = _properties.checked;
43574357
}
4358+
if (_properties.mixed != null) {
4359+
config.isCheckStateMixed = _properties.mixed;
4360+
}
43584361
if (_properties.toggled != null) {
43594362
config.isToggled = _properties.toggled;
43604363
}

‎packages/flutter/lib/src/semantics/semantics.dart‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ class SemanticsProperties extends DiagnosticableTree {
776776
const SemanticsProperties({
777777
this.enabled,
778778
this.checked,
779+
this.mixed,
779780
this.selected,
780781
this.toggled,
781782
this.button,
@@ -851,14 +852,30 @@ class SemanticsProperties extends DiagnosticableTree {
851852
/// or similar widget with a "checked" state, and what its current
852853
/// state is.
853854
///
854-
/// This is mutually exclusive with [toggled].
855+
/// When the [Checkbox.value] of a tristate Checkbox is null,
856+
/// indicating a mixed-state, this value shall be false, in which
857+
/// case, [mixed] will be true.
858+
///
859+
/// This is mutually exclusive with [toggled] and [mixed].
855860
final bool? checked;
856861

862+
/// If non-null, indicates that this subtree represents a checkbox
863+
/// or similar widget with a "half-checked" state or similar, and
864+
/// whether it is currently in this half-checked state.
865+
///
866+
/// This must be null when [Checkbox.tristate] is false, or
867+
/// when the widget is not a checkbox. When a tristate
868+
/// checkbox is fully unchecked/checked, this value shall
869+
/// be false.
870+
///
871+
/// This is mutually exclusive with [checked] and [toggled].
872+
final bool? mixed;
873+
857874
/// If non-null, indicates that this subtree represents a toggle switch
858875
/// or similar widget with an "on" state, and what its current
859876
/// state is.
860877
///
861-
/// This is mutually exclusive with [checked].
878+
/// This is mutually exclusive with [checked] and [mixed].
862879
final bool? toggled;
863880

864881
/// If non-null indicates that this subtree represents something that can be
@@ -1490,6 +1507,7 @@ class SemanticsProperties extends DiagnosticableTree {
14901507
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
14911508
super.debugFillProperties(properties);
14921509
properties.add(DiagnosticsProperty<bool>('checked', checked, defaultValue: null));
1510+
properties.add(DiagnosticsProperty<bool>('mixed', mixed, defaultValue: null));
14931511
properties.add(DiagnosticsProperty<bool>('selected', selected, defaultValue: null));
14941512
properties.add(StringProperty('label', label, defaultValue: null));
14951513
properties.add(AttributedStringProperty('attributedLabel', attributedLabel, defaultValue: null));
@@ -4189,10 +4207,26 @@ class SemanticsConfiguration {
41894207
/// checked/unchecked state.
41904208
bool? get isChecked => _hasFlag(SemanticsFlag.hasCheckedState) ? _hasFlag(SemanticsFlag.isChecked) : null;
41914209
set isChecked(bool? value) {
4210+
assert(value != true || isCheckStateMixed != true);
41924211
_setFlag(SemanticsFlag.hasCheckedState, true);
41934212
_setFlag(SemanticsFlag.isChecked, value!);
41944213
}
41954214

4215+
/// If this node has tristate that can be controlled by the user, whether
4216+
/// that state is in its mixed state.
4217+
///
4218+
/// Do not call the setter for this field if the owning [RenderObject] doesn't
4219+
/// have checked/unchecked state that can be controlled by the user.
4220+
///
4221+
/// The getter returns null if the owning [RenderObject] does not have
4222+
/// mixed checked state.
4223+
bool? get isCheckStateMixed => _hasFlag(SemanticsFlag.hasCheckedState) ? _hasFlag(SemanticsFlag.isCheckStateMixed) : null;
4224+
set isCheckStateMixed(bool? value) {
4225+
assert(value != true || isChecked != true);
4226+
_setFlag(SemanticsFlag.hasCheckedState, true);
4227+
_setFlag(SemanticsFlag.isCheckStateMixed, value!);
4228+
}
4229+
41964230
/// If this node has Boolean state that can be controlled by the user, whether
41974231
/// that state is on or off, corresponding to true and false, respectively.
41984232
///

‎packages/flutter/lib/src/widgets/basic.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6878,6 +6878,7 @@ class Semantics extends SingleChildRenderObjectWidget {
68786878
bool excludeSemantics = false,
68796879
bool? enabled,
68806880
bool? checked,
6881+
bool? mixed,
68816882
bool? selected,
68826883
bool? toggled,
68836884
bool? button,
@@ -6943,6 +6944,7 @@ class Semantics extends SingleChildRenderObjectWidget {
69436944
properties: SemanticsProperties(
69446945
enabled: enabled,
69456946
checked: checked,
6947+
mixed: mixed,
69466948
toggled: toggled,
69476949
selected: selected,
69486950
button: button,

‎packages/flutter/test/material/checkbox_test.dart‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,57 @@ void main() {
139139
hasEnabledState: true,
140140
isChecked: true,
141141
));
142+
143+
await tester.pumpWidget(Theme(
144+
data: theme,
145+
child: const Material(
146+
child: Checkbox(
147+
value: null,
148+
tristate: true,
149+
onChanged: null,
150+
),
151+
),
152+
));
153+
154+
expect(tester.getSemantics(find.byType(Checkbox)), matchesSemantics(
155+
hasCheckedState: true,
156+
hasEnabledState: true,
157+
isCheckStateMixed: true,
158+
));
159+
160+
await tester.pumpWidget(Theme(
161+
data: theme,
162+
child: const Material(
163+
child: Checkbox(
164+
value: true,
165+
tristate: true,
166+
onChanged: null,
167+
),
168+
),
169+
));
170+
171+
expect(tester.getSemantics(find.byType(Checkbox)), matchesSemantics(
172+
hasCheckedState: true,
173+
hasEnabledState: true,
174+
isChecked: true,
175+
));
176+
177+
await tester.pumpWidget(Theme(
178+
data: theme,
179+
child: const Material(
180+
child: Checkbox(
181+
value: false,
182+
tristate: true,
183+
onChanged: null,
184+
),
185+
),
186+
));
187+
188+
expect(tester.getSemantics(find.byType(Checkbox)), matchesSemantics(
189+
hasCheckedState: true,
190+
hasEnabledState: true,
191+
));
192+
142193
handle.dispose();
143194
});
144195

@@ -239,6 +290,7 @@ void main() {
239290
SemanticsFlag.hasEnabledState,
240291
SemanticsFlag.isEnabled,
241292
SemanticsFlag.isFocusable,
293+
SemanticsFlag.isCheckStateMixed,
242294
],
243295
actions: <SemanticsAction>[SemanticsAction.tap],
244296
), hasLength(1));

‎packages/flutter/test/widgets/custom_painter_test.dart‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ void _defineTests() {
451451
List<SemanticsFlag> flags = SemanticsFlag.values.values.toList();
452452
// [SemanticsFlag.hasImplicitScrolling] isn't part of [SemanticsProperties]
453453
// therefore it has to be removed.
454-
flags.remove(SemanticsFlag.hasImplicitScrolling);
454+
flags
455+
..remove(SemanticsFlag.hasImplicitScrolling)
456+
..remove(SemanticsFlag.isCheckStateMixed);
455457
TestSemantics expectedSemantics = TestSemantics.root(
456458
children: <TestSemantics>[
457459
TestSemantics.rootChild(
@@ -475,7 +477,8 @@ void _defineTests() {
475477
rect: Rect.fromLTRB(1.0, 2.0, 3.0, 4.0),
476478
properties: SemanticsProperties(
477479
enabled: true,
478-
checked: true,
480+
checked: false,
481+
mixed: true,
479482
toggled: true,
480483
selected: true,
481484
hidden: true,
@@ -502,7 +505,9 @@ void _defineTests() {
502505
flags = SemanticsFlag.values.values.toList();
503506
// [SemanticsFlag.hasImplicitScrolling] isn't part of [SemanticsProperties]
504507
// therefore it has to be removed.
505-
flags.remove(SemanticsFlag.hasImplicitScrolling);
508+
flags
509+
..remove(SemanticsFlag.hasImplicitScrolling)
510+
..remove(SemanticsFlag.isChecked);
506511
expectedSemantics = TestSemantics.root(
507512
children: <TestSemantics>[
508513
TestSemantics.rootChild(
@@ -519,7 +524,7 @@ void _defineTests() {
519524
);
520525
expect(semantics, hasSemantics(expectedSemantics, ignoreRect: true, ignoreTransform: true));
521526
semantics.dispose();
522-
}, skip: true); // [intended] https://github.com/flutter/flutter/issues/110107
527+
});
523528

524529
group('diffing', () {
525530
testWidgets('complains about duplicate keys', (WidgetTester tester) async {

‎packages/flutter/test/widgets/semantics_test.dart‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ void main() {
584584
flags
585585
..remove(SemanticsFlag.hasToggledState)
586586
..remove(SemanticsFlag.isToggled)
587-
..remove(SemanticsFlag.hasImplicitScrolling);
587+
..remove(SemanticsFlag.hasImplicitScrolling)
588+
..remove(SemanticsFlag.isCheckStateMixed);
588589

589590
TestSemantics expectedSemantics = TestSemantics.root(
590591
children: <TestSemantics>[
@@ -631,8 +632,50 @@ void main() {
631632
);
632633

633634
expect(semantics, hasSemantics(expectedSemantics, ignoreId: true));
635+
636+
await tester.pumpWidget(
637+
Semantics(
638+
key: const Key('a'),
639+
container: true,
640+
explicitChildNodes: true,
641+
// flags
642+
enabled: true,
643+
hidden: true,
644+
checked: false,
645+
mixed: true,
646+
selected: true,
647+
button: true,
648+
slider: true,
649+
keyboardKey: true,
650+
link: true,
651+
textField: true,
652+
readOnly: true,
653+
focused: true,
654+
focusable: true,
655+
inMutuallyExclusiveGroup: true,
656+
header: true,
657+
obscured: true,
658+
multiline: true,
659+
scopesRoute: true,
660+
namesRoute: true,
661+
image: true,
662+
liveRegion: true,
663+
),
664+
);
665+
flags
666+
..remove(SemanticsFlag.isChecked)
667+
..add(SemanticsFlag.isCheckStateMixed);
634668
semantics.dispose();
635-
}, skip: true); // [intended] https://github.com/flutter/flutter/issues/110107
669+
expectedSemantics = TestSemantics.root(
670+
children: <TestSemantics>[
671+
TestSemantics.rootChild(
672+
rect: TestSemantics.fullScreen,
673+
flags: flags,
674+
),
675+
],
676+
);
677+
expect(semantics, hasSemantics(expectedSemantics, ignoreId: true));
678+
});
636679

637680
testWidgets('Actions can be replaced without triggering semantics update', (WidgetTester tester) async {
638681
final SemanticsTester semantics = SemanticsTester(tester);

‎packages/flutter_test/lib/src/matchers.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ Matcher matchesSemantics({
542542
// Flags //
543543
bool hasCheckedState = false,
544544
bool isChecked = false,
545+
bool isCheckStateMixed = false,
545546
bool isSelected = false,
546547
bool isButton = false,
547548
bool isSlider = false,
@@ -617,6 +618,7 @@ Matcher matchesSemantics({
617618
// Flags
618619
hasCheckedState: hasCheckedState,
619620
isChecked: isChecked,
621+
isCheckStateMixed: isCheckStateMixed,
620622
isSelected: isSelected,
621623
isButton: isButton,
622624
isSlider: isSlider,
@@ -713,6 +715,7 @@ Matcher containsSemantics({
713715
// Flags
714716
bool? hasCheckedState,
715717
bool? isChecked,
718+
bool? isCheckStateMixed,
716719
bool? isSelected,
717720
bool? isButton,
718721
bool? isSlider,
@@ -788,6 +791,7 @@ Matcher containsSemantics({
788791
// Flags
789792
hasCheckedState: hasCheckedState,
790793
isChecked: isChecked,
794+
isCheckStateMixed: isCheckStateMixed,
791795
isSelected: isSelected,
792796
isButton: isButton,
793797
isSlider: isSlider,
@@ -2085,6 +2089,7 @@ class _MatchesSemanticsData extends Matcher {
20852089
// Flags
20862090
required bool? hasCheckedState,
20872091
required bool? isChecked,
2092+
required bool? isCheckStateMixed,
20882093
required bool? isSelected,
20892094
required bool? isButton,
20902095
required bool? isSlider,
@@ -2138,6 +2143,7 @@ class _MatchesSemanticsData extends Matcher {
21382143
}) : flags = <SemanticsFlag, bool>{
21392144
if (hasCheckedState != null) SemanticsFlag.hasCheckedState: hasCheckedState,
21402145
if (isChecked != null) SemanticsFlag.isChecked: isChecked,
2146+
if (isCheckStateMixed != null) SemanticsFlag.isCheckStateMixed: isCheckStateMixed,
21412147
if (isSelected != null) SemanticsFlag.isSelected: isSelected,
21422148
if (isButton != null) SemanticsFlag.isButton: isButton,
21432149
if (isSlider != null) SemanticsFlag.isSlider: isSlider,

‎packages/flutter_test/test/matchers_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ void main() {
656656
/* Flags */
657657
hasCheckedState: true,
658658
isChecked: true,
659+
isCheckStateMixed: true,
659660
isSelected: true,
660661
isButton: true,
661662
isSlider: true,

0 commit comments

Comments
 (0)