From 136b46ba9176e316af8b85cdf46243b174a0d32a Mon Sep 17 00:00:00 2001 From: hangyu Date: Tue, 15 Nov 2022 12:32:32 -0800 Subject: [PATCH] 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 --- .../lib/src/material/input_decorator.dart | 2 +- .../input_date_picker_form_field_test.dart | 2 +- .../test/material/text_field_test.dart | 36 +++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 87475592c5..d35560bab0 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -2136,7 +2136,7 @@ class _InputDecoratorState extends State with TickerProviderStat opacity: (isEmpty && !_hasInlineLabel) ? 1.0 : 0.0, duration: _kTransitionDuration, curve: _kTransitionCurve, - alwaysIncludeSemantics: true, + alwaysIncludeSemantics: isEmpty || (decoration.labelText == null && decoration.label == null), child: Text( hintText, style: hintStyle, diff --git a/packages/flutter/test/material/input_date_picker_form_field_test.dart b/packages/flutter/test/material/input_date_picker_form_field_test.dart index 43fe9c2d37..0cfbd88043 100644 --- a/packages/flutter/test/material/input_date_picker_form_field_test.dart +++ b/packages/flutter/test/material/input_date_picker_form_field_test.dart @@ -274,7 +274,7 @@ void main() { await tester.pumpAndSettle(); expect(tester.getSemantics(find.byType(EditableText)), matchesSemantics( - label: 'Enter Date\nmm/dd/yyyy', + label: 'Enter Date', isTextField: true, isFocused: true, value: '01/15/2016', diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index f3bdcc7dad..f4e3701e0d 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -6586,7 +6586,39 @@ void main() { expect(tester.getBottomLeft(find.byKey(keyB)).dy, rowBottomY); }); - testWidgets('TextField semantics include label when unfocused and label/hint when focused', (WidgetTester tester) async { + testWidgets('TextField semantics include label when unfocused and label/hint when focused if input is empty', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + final TextEditingController controller = TextEditingController(text: ''); + final Key key = UniqueKey(); + + await tester.pumpWidget( + overlay( + child: TextField( + key: key, + controller: controller, + decoration: const InputDecoration( + hintText: 'hint', + labelText: 'label', + ), + ), + ), + ); + + final SemanticsNode node = tester.getSemantics(find.byKey(key)); + + expect(node.label, 'label'); + expect(node.value, ''); + + // Focus text field. + await tester.tap(find.byKey(key)); + await tester.pump(); + + expect(node.label, 'label\nhint'); + expect(node.value, ''); + semantics.dispose(); + }); + + testWidgets('TextField semantics alway include label and not hint when input value is not empty', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); final TextEditingController controller = TextEditingController(text: 'value'); final Key key = UniqueKey(); @@ -6613,7 +6645,7 @@ void main() { await tester.tap(find.byKey(key)); await tester.pump(); - expect(node.label, 'label\nhint'); + expect(node.label, 'label'); expect(node.value, 'value'); semantics.dispose(); });