Skip to content

Commit

Permalink
[a11y] Add isEnabled semantics flag to text field (#143334)
Browse files Browse the repository at this point in the history
Add a semantics flag to   text field to fix flutter/flutter#143337 (in IOS the disabled text field is not read `dimmed`)

internal: b/322345393
  • Loading branch information
hannah-hyj authored Feb 13, 2024
1 parent 5025681 commit 14bce28
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/flutter/lib/src/material/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
animation: controller, // changes the _currentLength
builder: (BuildContext context, Widget? child) {
return Semantics(
enabled: _isEnabled,
maxValueLength: semanticsMaxValueLength,
currentValueLength: _currentLength,
onTap: widget.readOnly ? null : () {
Expand Down
7 changes: 5 additions & 2 deletions packages/flutter/test/cupertino/text_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,11 @@ void main() {
children: <TestSemantics>[
TestSemantics(
id: 4,
flags: <SemanticsFlag>[SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled,],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
actions: <SemanticsAction>[SemanticsAction.tap,
SemanticsAction.didGainAccessibilityFocus,],
textDirection: TextDirection.ltr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ void main() {
expect(tester.getSemantics(find.byType(EditableText)), matchesSemantics(
label: 'Enter Date',
isTextField: true,
hasEnabledState: true,
isEnabled: true,
isFocused: true,
value: '01/15/2016',
hasTapAction: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter/test/material/search_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ void main() {
id: 9,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
SemanticsFlag.isHeader,
if (debugDefaultTargetPlatformOverride != TargetPlatform.iOS &&
Expand Down Expand Up @@ -818,6 +820,8 @@ void main() {
id: 11,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
SemanticsFlag.isHeader,
if (debugDefaultTargetPlatformOverride != TargetPlatform.iOS &&
Expand Down
73 changes: 65 additions & 8 deletions packages/flutter/test/material/text_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ void main() {
children: <TestSemantics>[
TestSemantics(
id: 4,
flags: <SemanticsFlag>[SemanticsFlag.isTextField],
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
actions: <SemanticsAction>[
SemanticsAction.tap,
SemanticsAction.didGainAccessibilityFocus,
Expand Down Expand Up @@ -1856,7 +1856,12 @@ void main() {
children: <TestSemantics>[
TestSemantics(
id: 1,
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.isFocused],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
actions: <SemanticsAction>[
SemanticsAction.tap,
SemanticsAction.moveCursorBackwardByCharacter,
Expand Down Expand Up @@ -5189,6 +5194,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
),
TestSemantics.rootChild(
Expand Down Expand Up @@ -6530,7 +6537,7 @@ void main() {
),
);

expect(semantics, includesNodeWith(flags: <SemanticsFlag>[SemanticsFlag.isTextField]));
expect(semantics, includesNodeWith(flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled]));

semantics.dispose();
});
Expand Down Expand Up @@ -6944,7 +6951,7 @@ void main() {
);

expect(semantics, includesNodeWith(
flags: <SemanticsFlag>[SemanticsFlag.isTextField],
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
maxValueLength: 10,
currentValueLength: 0,
));
Expand All @@ -6959,7 +6966,12 @@ void main() {
await tester.pump();

expect(semantics, includesNodeWith(
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.isFocused],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
maxValueLength: 10,
currentValueLength: 3,
));
Expand All @@ -6985,7 +6997,12 @@ void main() {

expect(
semantics,
includesNodeWith(flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.isReadOnly]),
includesNodeWith(flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isReadOnly,
]),
);

semantics.dispose();
Expand Down Expand Up @@ -8050,6 +8067,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
),
],
Expand All @@ -8069,6 +8088,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
),
],
Expand All @@ -8094,6 +8115,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8122,6 +8145,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8149,6 +8174,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand All @@ -8174,6 +8201,8 @@ void main() {
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
value: 'Hello',
)
Expand All @@ -8189,6 +8218,8 @@ void main() {
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isObscured,
],
)
Expand All @@ -8204,6 +8235,8 @@ void main() {
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
value: 'Hello',
)
Expand Down Expand Up @@ -8246,6 +8279,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8281,6 +8316,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
),
],
Expand All @@ -8307,6 +8344,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8337,6 +8376,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8386,6 +8427,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8434,6 +8477,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
),
Expand Down Expand Up @@ -8470,7 +8515,7 @@ void main() {
children: <TestSemantics>[
TestSemantics(
id: inputFieldId,
flags: <SemanticsFlag>[SemanticsFlag.isTextField],
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
actions: <SemanticsAction>[SemanticsAction.tap],
value: textInTextField,
textDirection: TextDirection.ltr,
Expand All @@ -8490,6 +8535,8 @@ void main() {
id: inputFieldId,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
actions: <SemanticsAction>[
Expand Down Expand Up @@ -8543,7 +8590,7 @@ void main() {
children: <TestSemantics>[
TestSemantics(
id: inputFieldId,
flags: <SemanticsFlag>[SemanticsFlag.isTextField],
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
actions: <SemanticsAction>[SemanticsAction.tap],
value: textInTextField,
textDirection: TextDirection.ltr,
Expand All @@ -8563,6 +8610,8 @@ void main() {
id: inputFieldId,
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
actions: <SemanticsAction>[
Expand Down Expand Up @@ -8743,6 +8792,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
children: <TestSemantics>[
TestSemantics(
Expand Down Expand Up @@ -8778,6 +8829,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocused,
],
children: <TestSemantics>[
Expand Down Expand Up @@ -8835,6 +8888,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
children: <TestSemantics>[
TestSemantics(
Expand Down Expand Up @@ -8882,6 +8937,8 @@ void main() {
],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
],
children: <TestSemantics>[
TestSemantics(
Expand Down
14 changes: 12 additions & 2 deletions packages/flutter/test/material/time_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,12 @@ void main() {
label: 'Hour',
value: '07',
actions: <SemanticsAction>[SemanticsAction.tap],
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.isMultiline],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isMultiline,
],
),
);
expect(
Expand All @@ -1352,7 +1357,12 @@ void main() {
label: 'Minute',
value: '00',
actions: <SemanticsAction>[SemanticsAction.tap],
flags: <SemanticsFlag>[SemanticsFlag.isTextField, SemanticsFlag.isMultiline],
flags: <SemanticsFlag>[
SemanticsFlag.isTextField,
SemanticsFlag.hasEnabledState,
SemanticsFlag.isEnabled,
SemanticsFlag.isMultiline,
],
),
);

Expand Down
9 changes: 5 additions & 4 deletions packages/flutter_test/test/controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ void main() {
group('simulatedTraversal', () {
final List<Matcher> fullTraversalMatchers = <Matcher>[
containsSemantics(isHeader: true, label: 'Semantics Test'),
containsSemantics(label: 'Text Field'),
containsSemantics(isTextField: true),
containsSemantics(label: 'Off Switch'),
containsSemantics(hasToggledState: true),
Expand Down Expand Up @@ -913,7 +914,7 @@ void main() {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));

// We're expecting the traversal to start where the slider is.
final List<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers]..removeRange(0, 8);
final List<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers]..removeRange(0, 9);

expect(
tester.semantics.simulatedAccessibilityTraversal(start: find.byType(Slider)),
Expand All @@ -935,7 +936,7 @@ void main() {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));

// We're expecting the traversal to end where the slider is, inclusive.
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers].getRange(0, 9);
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers].getRange(0, 10);

expect(
tester.semantics.simulatedAccessibilityTraversal(end: find.byType(Slider)),
Expand All @@ -957,7 +958,7 @@ void main() {
await tester.pumpWidget(const MaterialApp(home: _SemanticsTestWidget()));

// We're expecting the traversal to start at the text field and end at the slider.
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers].getRange(1, 9);
final Iterable<Matcher> expectedMatchers = <Matcher>[...fullTraversalMatchers].getRange(1, 10);

expect(
tester.semantics.simulatedAccessibilityTraversal(
Expand Down Expand Up @@ -1661,7 +1662,7 @@ class _SemanticsTestWidget extends StatelessWidget {
child: Column(
children: <Widget>[
const _SemanticsTestCard(
label: 'TextField',
label: 'Text Field',
widget: TextField(),
),
_SemanticsTestCard(
Expand Down

0 comments on commit 14bce28

Please sign in to comment.