Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit b227df3

Browse files
authored
Hint text semantics to be excluded in a11y read out if hintText is not visible. (#119198)
* Update input_decorator.dart * Update text_field_test.dart * Update time_picker.dart Update time_picker.dart
1 parent 6c12e39 commit b227df3

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,6 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
21932193
opacity: (isEmpty && !_hasInlineLabel) ? 1.0 : 0.0,
21942194
duration: _kTransitionDuration,
21952195
curve: _kTransitionCurve,
2196-
alwaysIncludeSemantics: isEmpty || (decoration.labelText == null && decoration.label == null),
21972196
child: Text(
21982197
hintText,
21992198
style: hintStyle,

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,12 +2050,9 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora
20502050

20512051
final InputDecorationTheme inputDecorationTheme = timePickerTheme.inputDecorationTheme ?? defaultTheme.inputDecorationTheme;
20522052
InputDecoration inputDecoration = const InputDecoration().applyDefaults(inputDecorationTheme);
2053-
// If screen reader is in use, make the hint text say hours/minutes.
2054-
// Otherwise, remove the hint text when focused because the centered cursor
2053+
// Remove the hint text when focused because the centered cursor
20552054
// appears odd above the hint text.
2056-
final String? hintText = MediaQuery.accessibleNavigationOf(context) || View.of(context).platformDispatcher.semanticsEnabled
2057-
? widget.semanticHintText
2058-
: (focusNode.hasFocus ? null : _formattedValue);
2055+
final String? hintText = focusNode.hasFocus ? null : _formattedValue;
20592056

20602057
// Because the fill color is specified in both the inputDecorationTheme and
20612058
// the TimePickerTheme, if there's one in the user's input decoration theme,
@@ -2102,26 +2099,29 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora
21022099
data: MediaQuery.of(context).copyWith(textScaleFactor: 1),
21032100
child: UnmanagedRestorationScope(
21042101
bucket: bucket,
2105-
child: TextFormField(
2106-
restorationId: 'hour_minute_text_form_field',
2107-
autofocus: widget.autofocus ?? false,
2108-
expands: true,
2109-
maxLines: null,
2110-
inputFormatters: <TextInputFormatter>[
2111-
LengthLimitingTextInputFormatter(2),
2112-
],
2113-
focusNode: focusNode,
2114-
textAlign: TextAlign.center,
2115-
textInputAction: widget.inputAction,
2116-
keyboardType: TextInputType.number,
2117-
style: effectiveStyle,
2118-
controller: controller.value,
2119-
decoration: inputDecoration,
2120-
validator: widget.validator,
2121-
onEditingComplete: () => widget.onSavedSubmitted(controller.value.text),
2122-
onSaved: widget.onSavedSubmitted,
2123-
onFieldSubmitted: widget.onSavedSubmitted,
2124-
onChanged: widget.onChanged,
2102+
child: Semantics(
2103+
label: widget.semanticHintText,
2104+
child: TextFormField(
2105+
restorationId: 'hour_minute_text_form_field',
2106+
autofocus: widget.autofocus ?? false,
2107+
expands: true,
2108+
maxLines: null,
2109+
inputFormatters: <TextInputFormatter>[
2110+
LengthLimitingTextInputFormatter(2),
2111+
],
2112+
focusNode: focusNode,
2113+
textAlign: TextAlign.center,
2114+
textInputAction: widget.inputAction,
2115+
keyboardType: TextInputType.number,
2116+
style: effectiveStyle,
2117+
controller: controller.value,
2118+
decoration: inputDecoration,
2119+
validator: widget.validator,
2120+
onEditingComplete: () => widget.onSavedSubmitted(controller.value.text),
2121+
onSaved: widget.onSavedSubmitted,
2122+
onFieldSubmitted: widget.onSavedSubmitted,
2123+
onChanged: widget.onChanged,
2124+
),
21252125
),
21262126
),
21272127
),

packages/flutter/test/material/text_field_test.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6862,7 +6862,7 @@ void main() {
68626862
await tester.tap(find.byKey(key));
68636863
await tester.pump();
68646864

6865-
expect(node.label, 'label\nhint');
6865+
expect(node.label, 'label');
68666866
expect(node.value, '');
68676867
semantics.dispose();
68686868
});
@@ -6930,7 +6930,7 @@ void main() {
69306930
semantics.dispose();
69316931
});
69326932

6933-
testWidgets('TextField semantics always include hint when no label is given', (WidgetTester tester) async {
6933+
testWidgets('TextField semantics only include hint when it is visible', (WidgetTester tester) async {
69346934
final SemanticsTester semantics = SemanticsTester(tester);
69356935
final TextEditingController controller = TextEditingController(text: 'value');
69366936
final Key key = UniqueKey();
@@ -6949,15 +6949,23 @@ void main() {
69496949

69506950
final SemanticsNode node = tester.getSemantics(find.byKey(key));
69516951

6952-
expect(node.label, 'hint');
6952+
expect(node.label, '');
69536953
expect(node.value, 'value');
69546954

69556955
// Focus text field.
69566956
await tester.tap(find.byKey(key));
69576957
await tester.pump();
69586958

6959-
expect(node.label, 'hint');
6959+
expect(node.label, '');
69606960
expect(node.value, 'value');
6961+
6962+
// Clear the Text.
6963+
await tester.enterText(find.byType(TextField), '');
6964+
await tester.pumpAndSettle();
6965+
6966+
expect(node.value, '');
6967+
expect(node.label, 'hint');
6968+
69616969
semantics.dispose();
69626970
});
69636971

@@ -7698,7 +7706,7 @@ void main() {
76987706
expect(semantics, hasSemantics(TestSemantics.root(
76997707
children: <TestSemantics>[
77007708
TestSemantics.rootChild(
7701-
label: 'label\nhint',
7709+
label: 'label',
77027710
id: 1,
77037711
textDirection: TextDirection.ltr,
77047712
textSelection: const TextSelection(baseOffset: 0, extentOffset: 0),

0 commit comments

Comments
 (0)