Skip to content

Commit 136b46b

Browse files
authored
Hint text semantics to be excluded in a11y read out if textfield in not empty and label text is provided (#115010)
* update hint semantics * Update input_date_picker_form_field_test.dart
1 parent 2d77ac5 commit 136b46b

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/flutter/lib/src/material/input_decorator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
21362136
opacity: (isEmpty && !_hasInlineLabel) ? 1.0 : 0.0,
21372137
duration: _kTransitionDuration,
21382138
curve: _kTransitionCurve,
2139-
alwaysIncludeSemantics: true,
2139+
alwaysIncludeSemantics: isEmpty || (decoration.labelText == null && decoration.label == null),
21402140
child: Text(
21412141
hintText,
21422142
style: hintStyle,

packages/flutter/test/material/input_date_picker_form_field_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void main() {
274274
await tester.pumpAndSettle();
275275

276276
expect(tester.getSemantics(find.byType(EditableText)), matchesSemantics(
277-
label: 'Enter Date\nmm/dd/yyyy',
277+
label: 'Enter Date',
278278
isTextField: true,
279279
isFocused: true,
280280
value: '01/15/2016',

packages/flutter/test/material/text_field_test.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6586,7 +6586,39 @@ void main() {
65866586
expect(tester.getBottomLeft(find.byKey(keyB)).dy, rowBottomY);
65876587
});
65886588

6589-
testWidgets('TextField semantics include label when unfocused and label/hint when focused', (WidgetTester tester) async {
6589+
testWidgets('TextField semantics include label when unfocused and label/hint when focused if input is empty', (WidgetTester tester) async {
6590+
final SemanticsTester semantics = SemanticsTester(tester);
6591+
final TextEditingController controller = TextEditingController(text: '');
6592+
final Key key = UniqueKey();
6593+
6594+
await tester.pumpWidget(
6595+
overlay(
6596+
child: TextField(
6597+
key: key,
6598+
controller: controller,
6599+
decoration: const InputDecoration(
6600+
hintText: 'hint',
6601+
labelText: 'label',
6602+
),
6603+
),
6604+
),
6605+
);
6606+
6607+
final SemanticsNode node = tester.getSemantics(find.byKey(key));
6608+
6609+
expect(node.label, 'label');
6610+
expect(node.value, '');
6611+
6612+
// Focus text field.
6613+
await tester.tap(find.byKey(key));
6614+
await tester.pump();
6615+
6616+
expect(node.label, 'label\nhint');
6617+
expect(node.value, '');
6618+
semantics.dispose();
6619+
});
6620+
6621+
testWidgets('TextField semantics alway include label and not hint when input value is not empty', (WidgetTester tester) async {
65906622
final SemanticsTester semantics = SemanticsTester(tester);
65916623
final TextEditingController controller = TextEditingController(text: 'value');
65926624
final Key key = UniqueKey();
@@ -6613,7 +6645,7 @@ void main() {
66136645
await tester.tap(find.byKey(key));
66146646
await tester.pump();
66156647

6616-
expect(node.label, 'label\nhint');
6648+
expect(node.label, 'label');
66176649
expect(node.value, 'value');
66186650
semantics.dispose();
66196651
});

0 commit comments

Comments
 (0)