Container
An all-purpose box: size, inner and outer spacing, fill or decoration, child alignment and constraints — in one element. When you need just one thing (only spacing, only a size), a specialized element is simpler: Padding , SizedBox , ColoredBox , DecoratedBox .
Type:
single-child
Container color:#4f46e5 width:200 height:100
Center
Text "Hello"
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
width
|
length | — |
Width: a number (pixels) or a string with a unit —
"50%"
,
"100svh"
|
height
|
length | — | Height, same units |
color
|
color | — |
Fill.
Ignored if
#decoration
is set
— then the color goes inside the decoration
|
alignment
|
string | — |
Where to put the child inside the box:
center
,
topLeft
,
bottomRight
and so on
|
clipBehavior
|
string | none |
Clipping the child to the bounds:
none
,
hardEdge
,
antiAlias
,
antiAliasWithSaveLayer
|
Lengths: a bare number and
px
are pixels;
vh
/
svh
/
lvh
/
dvh
,
vw
/
svw
/
lvw
/
dvw
,
vmin
/
vmax
are a share of the window;
%
is a share of the space the parent gives (if the parent is unbounded along that axis, there is no percentage). Other units (
rem
,
em
) are not supported.
Nested structures
#padding
and
#margin
Spacing inside the box and outside it. The format is the same as for
Padding
:
all
,
symmetric
,
only
,
fromLTRB
(the
fromLTRB
values are
positional
:
#padding fromLTRB 10 15 20 25
).
#decoration
Decoration: color, rounding, border, gradient, shadow, background image. Works the same way as for DecoratedBox .
#constraints
Lower and upper size limits:
minWidth
,
maxWidth
,
minHeight
,
maxHeight
(numbers or strings with a unit).
Container
#constraints minHeight:120 maxWidth:480
Text "No narrower than the text, but no wider than 480"
Examples
A card with spacing and rounding
Container
#padding all 16
#margin symmetric horizontal:24 vertical:8
#decoration color:#1e1b4b
#borderRadius circular 12
Column spacing:4
Text "Title"
Text "Card description"
Child in the corner
Container width:200 height:120 alignment:bottomRight color:#36345E
Text "Bottom right"
Clipping to the rounding
To keep the content inside the rounded corners, turn on clipping:
Container width:160 height:160 clipBehavior:antiAlias
#decoration color:#585593
#borderRadius circular 24
Image src:"https://example.com/poster.jpg" fit:cover
Differences between the app and the site
-
An empty
Containerwith no size. In the app a box with no child and nowidth/heighttakes all the available space. On the site it is an empty block with no height — its fill is not visible. Always give it a size or a child. -
clipBehavioron the site only distinguishes "clip" and "do not clip": any value other thannoneclips the same way. -
The named
#constraintsmodes (expand,tight,loose) are not read on the site — only the form withminWidth/maxWidth/minHeight/maxHeightworks.
Bad practices
color
together with
#decoration
// BAD — color is silently dropped: when there is a #decoration, the background comes only from it
Container color:#FF0000
#decoration
#borderRadius circular 8
Text "Red?"
// GOOD — the color lives in the decoration
Container
#decoration color:#FF0000
#borderRadius circular 8
Text "Red"
Best practices
-
Use a narrow element when it covers the task
: only spacing —
Padding, only a size —SizedBox.Containeris justified when you need several properties at once. -
Do not put
width/heighton aContainerthat has to stretch in aColumn/Row— wrap it in Expanded .
See also
- Padding — spacing only
- SizedBox — size only
- DecoratedBox — decoration only
- ConstrainedBox — size constraints only
- Align — alignment only