diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 712245228d..b198a805a4 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -698,12 +698,20 @@ class _AppBarState extends State { appBar = Stack( fit: StackFit.passthrough, children: [ - widget.flexibleSpace, - // Creates a material widget to prevent the flexibleSpace from - // obscuring the ink splashes produced by appBar children. - Material( - type: MaterialType.transparency, - child: appBar, + Semantics( + sortKey: const OrdinalSortKey(1.0), + explicitChildNodes: true, + child: widget.flexibleSpace, + ), + Semantics( + sortKey: const OrdinalSortKey(0.0), + explicitChildNodes: true, + // Creates a material widget to prevent the flexibleSpace from + // obscuring the ink splashes produced by appBar children. + child: Material( + type: MaterialType.transparency, + child: appBar, + ), ), ], ); diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index 1f8e4cc7d9..7cb6241fee 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -1649,12 +1649,94 @@ void main() { TestSemantics( children: [ TestSemantics( - label: 'Leading', - textDirection: TextDirection.ltr, + children: [ + TestSemantics( + label: 'Leading', + textDirection: TextDirection.ltr, + ), + TestSemantics( + label: 'Action 1', + textDirection: TextDirection.ltr, + ), + ], + ), + TestSemantics(), + ], + ), + TestSemantics( + flags: [SemanticsFlag.hasImplicitScrolling], + ), + ], + ), + ], + ), + ], + ), + ], + ), + ], + ), + ignoreRect: true, + ignoreTransform: true, + ignoreId: true, + )); + + semantics.dispose(); + }); + + testWidgets('SliverAppBar with flexable space has correct semantics order', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/64922. + final SemanticsTester semantics = SemanticsTester(tester); + + await tester.pumpWidget( + const MaterialApp( + home: CustomScrollView( + slivers: [ + SliverAppBar( + leading: Text('Leading'), + flexibleSpace: Text('Flexible space'), + actions: [Text('Action 1')], + ), + ], + ), + ), + ); + + expect(semantics, hasSemantics( + TestSemantics.root( + children: [ + TestSemantics( + textDirection: TextDirection.ltr, + children: [ + TestSemantics( + children: [ + TestSemantics( + flags: [SemanticsFlag.scopesRoute], + children: [ + TestSemantics( + children: [ + TestSemantics( + children: [ + TestSemantics( + children: [ + TestSemantics( + label: 'Leading', + textDirection: TextDirection.ltr, + ), + TestSemantics( + label: 'Action 1', + textDirection: TextDirection.ltr, + ), + ], ), TestSemantics( - label: 'Action 1', - textDirection: TextDirection.ltr, + children: [ + TestSemantics( + flags: [SemanticsFlag.isHeader], + label: 'Flexible space', + textDirection: TextDirection.ltr, + ), + ], ), ], ), diff --git a/packages/flutter/test/material/flexible_space_bar_test.dart b/packages/flutter/test/material/flexible_space_bar_test.dart index bdc77511bb..879c2dd620 100644 --- a/packages/flutter/test/material/flexible_space_bar_test.dart +++ b/packages/flutter/test/material/flexible_space_bar_test.dart @@ -178,25 +178,37 @@ void main() { rect: const Rect.fromLTRB(0.0, 0.0, 800.0, expandedHeight), children: [ TestSemantics( - id: 11, - rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 20.0), - flags: [ - SemanticsFlag.isHeader, - SemanticsFlag.namesRoute + id: 12, + rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 200.0), + children: [ + TestSemantics( + id: 13, + rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 20.0), + flags: [ + SemanticsFlag.isHeader, + SemanticsFlag.namesRoute + ], + label: 'Title', + textDirection: TextDirection.ltr, + ), ], - label: 'Title', - textDirection: TextDirection.ltr, ), TestSemantics( id: 10, - rect: const Rect.fromLTRB(0.0, 0.0, 800.0, expandedHeight), - label: 'Expanded title', - textDirection: TextDirection.ltr, + rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 200.0), + children: [ + TestSemantics( + id: 11, + rect: const Rect.fromLTRB(0.0, 0.0, 800.0, expandedHeight), + label: 'Expanded title', + textDirection: TextDirection.ltr, + ), + ], ), ], ), TestSemantics( - id: 12, + id: 14, flags: [SemanticsFlag.hasImplicitScrolling], rect: TestSemantics.fullScreen, actions: [SemanticsAction.scrollUp], @@ -272,27 +284,39 @@ void main() { rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 56.0), children: [ TestSemantics( - id: 11, - rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 20.0), - flags: [ - SemanticsFlag.isHeader, - SemanticsFlag.namesRoute + id: 12, + rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 56.0), + children: [ + TestSemantics( + id: 13, + rect: const Rect.fromLTRB(0.0, 0.0, 100.0, 20.0), + flags: [ + SemanticsFlag.isHeader, + SemanticsFlag.namesRoute + ], + label: 'Title', + textDirection: TextDirection.ltr, + ), ], - label: 'Title', - textDirection: TextDirection.ltr, ), // The flexible space bar still persists in the // semantic tree even if it is collapsed. TestSemantics( id: 10, - rect: const Rect.fromLTRB(0.0, 36.0, 800.0, 92.0), - label: 'Expanded title', - textDirection: TextDirection.ltr, + rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 56.0), + children: [ + TestSemantics( + id: 11, + rect: const Rect.fromLTRB(0.0, 36.0, 800.0, 92.0), + label: 'Expanded title', + textDirection: TextDirection.ltr, + ), + ], ), ], ), TestSemantics( - id: 12, + id: 14, flags: [SemanticsFlag.hasImplicitScrolling], rect: TestSemantics.fullScreen, actions: [SemanticsAction.scrollUp, SemanticsAction.scrollDown], @@ -324,20 +348,20 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( - id: 13, + id: 15, rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 200.0), label: 'Item 4', textDirection: TextDirection.ltr, ), TestSemantics( - id: 14, + id: 16, rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 200.0), flags: [SemanticsFlag.isHidden], label: 'Item 5', textDirection: TextDirection.ltr, ), TestSemantics( - id: 15, + id: 17, rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0), flags: [SemanticsFlag.isHidden], label: 'Item 6', diff --git a/packages/flutter/test/widgets/sliver_semantics_test.dart b/packages/flutter/test/widgets/sliver_semantics_test.dart index 285b282079..2f2b5e40c2 100644 --- a/packages/flutter/test/widgets/sliver_semantics_test.dart +++ b/packages/flutter/test/widgets/sliver_semantics_test.dart @@ -986,13 +986,18 @@ void _tests() { TestSemantics( tags: [RenderViewport.excludeFromScrolling], children: [ + TestSemantics(), TestSemantics( - flags: [ - SemanticsFlag.namesRoute, - SemanticsFlag.isHeader, + children: [ + TestSemantics( + flags: [ + SemanticsFlag.namesRoute, + SemanticsFlag.isHeader, + ], + label: 'Forward app bar', + textDirection: TextDirection.ltr, + ), ], - label: 'Forward app bar', - textDirection: TextDirection.ltr, ), ], ), @@ -1096,13 +1101,18 @@ void _tests() { TestSemantics( tags: [RenderViewport.excludeFromScrolling], children: [ + TestSemantics(), TestSemantics( - flags: [ - SemanticsFlag.namesRoute, - SemanticsFlag.isHeader, + children: [ + TestSemantics( + flags: [ + SemanticsFlag.namesRoute, + SemanticsFlag.isHeader, + ], + label: 'Backward app bar', + textDirection: TextDirection.ltr, + ), ], - label: 'Backward app bar', - textDirection: TextDirection.ltr, ), ], ),