From 730a534fbbe1f6a0dba36e25b7c336fe4d3e42b2 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 26 Jul 2018 17:27:45 -0700 Subject: [PATCH] Make Card explicitChildNodes vs container be configurable (#19693) --- packages/flutter/lib/src/material/card.dart | 17 ++++- packages/flutter/test/material/card_test.dart | 76 +++++++++++++++---- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/packages/flutter/lib/src/material/card.dart b/packages/flutter/lib/src/material/card.dart index 501cae8c4d2..a5918b54ab9 100644 --- a/packages/flutter/lib/src/material/card.dart +++ b/packages/flutter/lib/src/material/card.dart @@ -67,6 +67,7 @@ class Card extends StatelessWidget { this.shape, this.margin = const EdgeInsets.all(4.0), this.child, + this.semanticContainer = false, }) : super(key: key); /// The card's background color. @@ -100,6 +101,19 @@ class Card extends StatelessWidget { /// `EdgeInsets.all(4.0)`. final EdgeInsetsGeometry margin; + /// Whether this widget represents a single semantic container, or if false + /// a collection of individual semantic nodes. + /// + /// Defaults to false. + /// + /// Setting this flag to true will attempt to merge all child semantics into + /// this node. Setting this flag to false will force all child semantic nodes + /// to be explicit. + /// + /// This flag should be false if the card contains multiple different types + /// of content. + final bool semanticContainer; + /// The widget below this widget in the tree. /// /// {@macro flutter.widgets.child} @@ -108,7 +122,8 @@ class Card extends StatelessWidget { @override Widget build(BuildContext context) { return new Semantics( - container: true, + container: semanticContainer, + explicitChildNodes: !semanticContainer, child: new Container( margin: margin ?? const EdgeInsets.all(4.0), child: new Material( diff --git a/packages/flutter/test/material/card_test.dart b/packages/flutter/test/material/card_test.dart index 60b2c7f168e..33eae2e4ba1 100644 --- a/packages/flutter/test/material/card_test.dart +++ b/packages/flutter/test/material/card_test.dart @@ -18,6 +18,7 @@ void main() { child: new Material( child: new Center( child: new Card( + semanticContainer: false, child: new Column( children: [ const Text('I am text!'), @@ -37,25 +38,68 @@ void main() { expect(semantics, hasSemantics( new TestSemantics.root( children: [ - new TestSemantics.rootChild( + new TestSemantics( id: 1, - label: 'I am text!\nMoar text!!1', + label: 'I am text!', textDirection: TextDirection.ltr, - children: [ - new TestSemantics( - id: 2, - label: 'Button', - textDirection: TextDirection.ltr, - actions: [ - SemanticsAction.tap, - ], - flags: [ - SemanticsFlag.isButton, - SemanticsFlag.hasEnabledState, - SemanticsFlag.isEnabled, - ], - ), + ), + new TestSemantics( + id: 2, + label: 'Moar text!!1', + textDirection: TextDirection.ltr, + ), + new TestSemantics( + id: 3, + label: 'Button', + textDirection: TextDirection.ltr, + actions: [ + SemanticsAction.tap, ], + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + ], + ), + ], + ), + ignoreTransform: true, + ignoreRect: true, + )); + + semantics.dispose(); + }); + + testWidgets('Card merges children when it is a semanticContainer', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + debugResetSemanticsIdCounter(); + + await tester.pumpWidget( + new Directionality( + textDirection: TextDirection.ltr, + child: new Material( + child: new Center( + child: new Card( + semanticContainer: true, + child: new Column( + children: const [ + Text('First child'), + Text('Second child') + ], + ) + ), + ), + ), + ), + ); + + expect(semantics, hasSemantics( + new TestSemantics.root( + children: [ + new TestSemantics( + id: 1, + label: 'First child\nSecond child', + textDirection: TextDirection.ltr, ), ], ),