diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index dce8c75ea7..0c11d8ae72 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -1865,9 +1865,11 @@ class _InputDecoratorState extends State with TickerProviderStat late final CurvedAnimation _floatingLabelAnimation; late final AnimationController _shakingLabelController; final _InputBorderGap _borderGap = _InputBorderGap(); - static const OrdinalSortKey _kPrefixSemanticsSortOrder = OrdinalSortKey(0); - static const OrdinalSortKey _kInputSemanticsSortOrder = OrdinalSortKey(1); - static const OrdinalSortKey _kSuffixSemanticsSortOrder = OrdinalSortKey(2); + // Provide a unique name to avoid mixing up sort order with sibling input + // decorators. + late final OrdinalSortKey _prefixSemanticsSortOrder = OrdinalSortKey(0, name: hashCode.toString()); + late final OrdinalSortKey _inputSemanticsSortOrder = OrdinalSortKey(1, name: hashCode.toString()); + late final OrdinalSortKey _suffixSemanticsSortOrder = OrdinalSortKey(2, name: hashCode.toString()); static const SemanticsTag _kPrefixSemanticsTag = SemanticsTag('_InputDecoratorState.prefix'); static const SemanticsTag _kSuffixSemanticsTag = SemanticsTag('_InputDecoratorState.suffix'); @@ -2219,7 +2221,7 @@ class _InputDecoratorState extends State with TickerProviderStat labelIsFloating: widget._labelShouldWithdraw, text: decoration.prefixText, style: MaterialStateProperty.resolveAs(decoration.prefixStyle, materialState) ?? hintStyle, - semanticsSortKey: needsSemanticsSortOrder ? _kPrefixSemanticsSortOrder : null, + semanticsSortKey: needsSemanticsSortOrder ? _prefixSemanticsSortOrder : null, semanticsTag: _kPrefixSemanticsTag, child: decoration.prefix, ) @@ -2230,7 +2232,7 @@ class _InputDecoratorState extends State with TickerProviderStat labelIsFloating: widget._labelShouldWithdraw, text: decoration.suffixText, style: MaterialStateProperty.resolveAs(decoration.suffixStyle, materialState) ?? hintStyle, - semanticsSortKey: needsSemanticsSortOrder ? _kSuffixSemanticsSortOrder : null, + semanticsSortKey: needsSemanticsSortOrder ? _suffixSemanticsSortOrder : null, semanticsTag: _kSuffixSemanticsTag, child: decoration.suffix, ) @@ -2238,7 +2240,7 @@ class _InputDecoratorState extends State with TickerProviderStat if (input != null && needsSemanticsSortOrder) { input = Semantics( - sortKey: _kInputSemanticsSortOrder, + sortKey: _inputSemanticsSortOrder, child: input, ); } diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index bb5a60e513..d70ede21f9 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -1831,6 +1831,56 @@ void main() { expect(handle.opacity.value, equals(0.0)); }); + testWidgets('multiple text fields with prefix and suffix have correct semantics order.', (WidgetTester tester) async { + final TextEditingController controller1 = _textEditingController( + text: 'abc', + ); + final TextEditingController controller2 = _textEditingController( + text: 'def', + ); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Column( + children: [ + TextField( + decoration: const InputDecoration( + prefixText: 'prefix1', + suffixText: 'suffix1', + ), + enabled: false, + controller: controller1, + ), + TextField( + decoration: const InputDecoration( + prefixText: 'prefix2', + suffixText: 'suffix2', + ), + enabled: false, + controller: controller2, + ), + ], + ), + ), + ), + ); + final List orders = tester.semantics.simulatedAccessibilityTraversal( + startNode: find.semantics.byLabel('prefix1'), + ).map((SemanticsNode node) => node.label + node.value).toList(); + + expect( + orders, + [ + 'prefix1', + 'abc', + 'suffix1', + 'prefix2', + 'def', + 'suffix2', + ], + ); + }); + testWidgets('selection handles are excluded from the semantics', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); final TextEditingController controller = _textEditingController();