diff --git a/packages/flutter/lib/src/material/card.dart b/packages/flutter/lib/src/material/card.dart index 12940a888e..e9a18e449a 100644 --- a/packages/flutter/lib/src/material/card.dart +++ b/packages/flutter/lib/src/material/card.dart @@ -65,6 +65,7 @@ class Card extends StatelessWidget { this.color, this.elevation, this.shape, + this.margin: const EdgeInsets.all(4.0), this.child, }) : super(key: key); @@ -91,6 +92,14 @@ class Card extends StatelessWidget { /// radius of 4.0. final ShapeBorder shape; + /// The empty space that surrounds the card. + /// + /// Defines the card's outer [Container.margin]. + /// + /// The default margin is 4.0 logical pixels on all sides: + /// `EdgeInsets.all(4.0)`. + final EdgeInsetsGeometry margin; + /// The widget below this widget in the tree. /// /// {@macro flutter.widgets.child} @@ -101,7 +110,7 @@ class Card extends StatelessWidget { return new Semantics( container: true, child: new Container( - margin: const EdgeInsets.all(4.0), + margin: margin ?? const EdgeInsets.all(4.0), child: new Material( type: MaterialType.card, color: color ?? Theme.of(context).cardColor, diff --git a/packages/flutter/test/material/card_test.dart b/packages/flutter/test/material/card_test.dart index 078d9a1d19..b78005eb01 100644 --- a/packages/flutter/test/material/card_test.dart +++ b/packages/flutter/test/material/card_test.dart @@ -68,4 +68,51 @@ void main() { semantics.dispose(); }); + testWidgets('Card margin', (WidgetTester tester) async { + const Key contentsKey = const ValueKey('contents'); + + await tester.pumpWidget( + new Container( + alignment: Alignment.topLeft, + child: new Card( + child: new Container( + key: contentsKey, + color: const Color(0xFF00FF00), + width: 100.0, + height: 100.0, + ), + ), + ), + ); + + // Default margin is 4 + expect(tester.getTopLeft(find.byType(Card)), const Offset(0.0, 0.0)); + expect(tester.getSize(find.byType(Card)), const Size(108.0, 108.0)); + + expect(tester.getTopLeft(find.byKey(contentsKey)), const Offset(4.0, 4.0)); + expect(tester.getSize(find.byKey(contentsKey)), const Size(100.0, 100.0)); + + await tester.pumpWidget( + new Container( + alignment: Alignment.topLeft, + child: new Card( + margin: EdgeInsets.zero, + child: new Container( + key: contentsKey, + color: const Color(0xFF00FF00), + width: 100.0, + height: 100.0, + ), + ), + ), + ); + + // Specified margin is zero + expect(tester.getTopLeft(find.byType(Card)), const Offset(0.0, 0.0)); + expect(tester.getSize(find.byType(Card)), const Size(100.0, 100.0)); + + expect(tester.getTopLeft(find.byKey(contentsKey)), const Offset(0.0, 0.0)); + expect(tester.getSize(find.byKey(contentsKey)), const Size(100.0, 100.0)); + }); + }