Commit f20c3d10 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Add a `color` argument to `Container`. (#8396)

It's common to just want a simple colored box. Simple thing should be simple,
so this patch adds a convenience argument to Continer for creating a box
decoration that is just a color.

Fixes #5555
parent 76394630
...@@ -324,7 +324,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -324,7 +324,7 @@ class CardCollectionState extends State<CardCollection> {
child: new SingleChildScrollView( child: new SingleChildScrollView(
child: new Container( child: new Container(
height: cardModel.height, height: cardModel.height,
decoration: new BoxDecoration(backgroundColor: theme.primaryColor), color: theme.primaryColor,
child: new Row( child: new Row(
children: <Widget>[ children: <Widget>[
leftArrowIcon, leftArrowIcon,
...@@ -378,7 +378,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -378,7 +378,7 @@ class CardCollectionState extends State<CardCollection> {
Widget body = new Container( Widget body = new Container(
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0), padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
decoration: new BoxDecoration(backgroundColor: _primaryColor[50]), color: _primaryColor[50],
child: cardCollection, child: cardCollection,
); );
......
...@@ -58,7 +58,7 @@ class ColorItem extends StatelessWidget { ...@@ -58,7 +58,7 @@ class ColorItem extends StatelessWidget {
return new Container( return new Container(
height: kColorItemHeight, height: kColorItemHeight,
padding: const EdgeInsets.symmetric(horizontal: 16.0), padding: const EdgeInsets.symmetric(horizontal: 16.0),
decoration: new BoxDecoration(backgroundColor: color), color: color,
child: new Row( child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
......
...@@ -102,13 +102,13 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> { ...@@ -102,13 +102,13 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
)); ));
}, },
background: new Container( background: new Container(
decoration: new BoxDecoration(backgroundColor: theme.primaryColor), color: theme.primaryColor,
child: new ListItem( child: new ListItem(
leading: new Icon(Icons.delete, color: Colors.white, size: 36.0) leading: new Icon(Icons.delete, color: Colors.white, size: 36.0)
) )
), ),
secondaryBackground: new Container( secondaryBackground: new Container(
decoration: new BoxDecoration(backgroundColor: theme.primaryColor), color: theme.primaryColor,
child: new ListItem( child: new ListItem(
trailing: new Icon(Icons.archive, color: Colors.white, size: 36.0) trailing: new Icon(Icons.archive, color: Colors.white, size: 36.0)
) )
......
...@@ -116,7 +116,7 @@ class _DatePickerHeader extends StatelessWidget { ...@@ -116,7 +116,7 @@ class _DatePickerHeader extends StatelessWidget {
width: width, width: width,
height: height, height: height,
padding: padding, padding: padding,
decoration: new BoxDecoration(backgroundColor: backgroundColor), color: backgroundColor,
child: new Column( child: new Column(
mainAxisAlignment: mainAxisAlignment, mainAxisAlignment: mainAxisAlignment,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
......
...@@ -275,7 +275,7 @@ class _PopupMenu<T> extends StatelessWidget { ...@@ -275,7 +275,7 @@ class _PopupMenu<T> extends StatelessWidget {
Widget item = route.items[i]; Widget item = route.items[i];
if (route.initialValue != null && route.initialValue == route.items[i].value) { if (route.initialValue != null && route.initialValue == route.items[i].value) {
item = new Container( item = new Container(
decoration: new BoxDecoration(backgroundColor: Theme.of(context).highlightColor), color: Theme.of(context).highlightColor,
child: item child: item
); );
} }
......
...@@ -334,7 +334,7 @@ class _TimePickerHeader extends StatelessWidget { ...@@ -334,7 +334,7 @@ class _TimePickerHeader extends StatelessWidget {
width: width, width: width,
height: height, height: height,
padding: padding, padding: padding,
decoration: new BoxDecoration(backgroundColor: backgroundColor), color: backgroundColor,
child: new CustomMultiChildLayout( child: new CustomMultiChildLayout(
delegate: new _TimePickerHeaderLayout(orientation), delegate: new _TimePickerHeaderLayout(orientation),
children: <Widget>[ children: <Widget>[
......
...@@ -80,11 +80,18 @@ class Container extends StatelessWidget { ...@@ -80,11 +80,18 @@ class Container extends StatelessWidget {
/// Creates a widget that combines common painting, positioning, and sizing widgets. /// Creates a widget that combines common painting, positioning, and sizing widgets.
/// ///
/// The `height` and `width` values include the padding. /// The `height` and `width` values include the padding.
///
/// The `color` argument is a shorthand for
/// `decoration: new BoxDecoration(backgroundColor: color)`, which means you
/// cannot supply both a `color` and a `decoration` argument. If you want to
/// have both a `color` and a `decoration`, you can pass the color as the
/// `backgroundColor` argument to the `BoxDecoration`.
Container({ Container({
Key key, Key key,
this.alignment, this.alignment,
this.padding, this.padding,
this.decoration, Color color,
Decoration decoration,
this.foregroundDecoration, this.foregroundDecoration,
double width, double width,
double height, double height,
...@@ -92,7 +99,8 @@ class Container extends StatelessWidget { ...@@ -92,7 +99,8 @@ class Container extends StatelessWidget {
this.margin, this.margin,
this.transform, this.transform,
this.child, this.child,
}) : constraints = }) : decoration = decoration ?? (color != null ? new BoxDecoration(backgroundColor: color) : null),
constraints =
(width != null || height != null) (width != null || height != null)
? constraints?.tighten(width: width, height: height) ? constraints?.tighten(width: width, height: height)
?? new BoxConstraints.tightFor(width: width, height: height) ?? new BoxConstraints.tightFor(width: width, height: height)
...@@ -102,6 +110,10 @@ class Container extends StatelessWidget { ...@@ -102,6 +110,10 @@ class Container extends StatelessWidget {
assert(padding == null || padding.isNonNegative); assert(padding == null || padding.isNonNegative);
assert(decoration == null || decoration.debugAssertIsValid()); assert(decoration == null || decoration.debugAssertIsValid());
assert(constraints == null || constraints.debugAssertIsValid()); assert(constraints == null || constraints.debugAssertIsValid());
assert(color == null || decoration == null,
'Cannot provide both a color and a decoration\n'
'The color argument is just a shorthand for "decoration: new BoxDecoration(backgroundColor: color)".'
);
} }
/// The [child] contained by the container. /// The [child] contained by the container.
......
...@@ -24,7 +24,7 @@ Widget buildSliverAppBarApp({ bool floating, bool pinned, double expandedHeight ...@@ -24,7 +24,7 @@ Widget buildSliverAppBarApp({ bool floating, bool pinned, double expandedHeight
new SliverToBoxAdapter( new SliverToBoxAdapter(
child: new Container( child: new Container(
height: 1200.0, height: 1200.0,
decoration: new BoxDecoration(backgroundColor: Colors.orange[400]), color: Colors.orange[400],
), ),
), ),
], ],
......
...@@ -189,7 +189,7 @@ void main() { ...@@ -189,7 +189,7 @@ void main() {
theme: new ThemeData(platform: TargetPlatform.android), theme: new ThemeData(platform: TargetPlatform.android),
home: new Scaffold( home: new Scaffold(
appBar: new AppBar( appBar: new AppBar(
title: new Text('Title') title: new Text('Title'),
), ),
body: new Builder( body: new Builder(
builder: (BuildContext context) { builder: (BuildContext context) {
...@@ -198,16 +198,16 @@ void main() { ...@@ -198,16 +198,16 @@ void main() {
Scaffold.of(context).showBottomSheet<Null>((BuildContext context) { Scaffold.of(context).showBottomSheet<Null>((BuildContext context) {
return new Container( return new Container(
key: sheetKey, key: sheetKey,
decoration: new BoxDecoration(backgroundColor: Colors.blue[500]) color: Colors.blue[500],
); );
}); });
}, },
child: new Text('X') child: new Text('X'),
); );
} },
) ),
) ),
) ),
); );
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
......
...@@ -23,21 +23,21 @@ void main() { ...@@ -23,21 +23,21 @@ void main() {
child: new Container( child: new Container(
width: 100.0, width: 100.0,
height: 40.0, height: 40.0,
decoration: new BoxDecoration(backgroundColor: Colors.blue[500]) color: Colors.blue[500],
) ),
), ),
new GestureDetector( new GestureDetector(
onTap: () { log.add('right'); }, onTap: () { log.add('right'); },
child: new Container( child: new Container(
width: 75.0, width: 75.0,
height: 65.0, height: 65.0,
decoration: new BoxDecoration(backgroundColor: Colors.blue[500]) color: Colors.blue[500],
) ),
), ),
] ],
) ),
) ),
) ),
); );
RenderBox box = tester.renderObject(find.byKey(rotatedBoxKey)); RenderBox box = tester.renderObject(find.byKey(rotatedBoxKey));
......
...@@ -165,8 +165,7 @@ void main() { ...@@ -165,8 +165,7 @@ void main() {
new Container( new Container(
key: childKey, key: childKey,
height: 5000.0, height: 5000.0,
decoration: color: Colors.green[500],
new BoxDecoration(backgroundColor: Colors.green[500]),
), ),
], ],
), ),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment