What is Container in Flutter?
Container is a versatile widget that combines common painting, positioning, and sizing widgets. It's one of the most frequently used widgets in Flutter because it provides a simple way to add padding, margins, borders, background color, or decoration to a child widget. Container can be thought of as a box that can hold a single child and style it according to your needs.
Basic Usage
In its simplest form, you provide a child and some styling. The above creates a blue box with 16 pixels of padding around the text.
Common Properties
child: The widget inside the container (can be null).
padding: Empty space inside the container around the child.
margin: Empty space outside the container.
color: Background color (cannot be used together withdecoration).
decoration: ABoxDecorationfor more complex styling (border, gradient, shadow).
width/height: Fixed dimensions (in logical pixels).
alignment: Aligns the child within the container (e.g.,Alignment.center).
constraints: Additional sizing constraints (e.g.,BoxConstraints(maxWidth: 200)).
transform: Applies a transformation (e.g.,Matrix4.rotationZ(0.2)).
Padding and Margin
padding adds space inside the container, between the container's edges and its child. margin adds space outside the container, separating it from neighboring widgets. Both are EdgeInsetsGeometry objects.
Decoration: BoxDecoration
For rich styling, use decoration with BoxDecoration. It supports:
color(background, but can't be used ifcolorproperty is set)borderRadius(rounded corners)boxShadow(drop shadows)gradient(linear, radial, sweep)border(border around the container)image(background image)
Note: You cannot set both color and decoration. If you need a background color with decoration, include the color inside the BoxDecoration.
Sizing and Constraints
Container tries to size itself to its child, but you can override this by providing width, height, or constraints. If no child is given and no explicit size, the container expands to fill the available space (like a SizedBox.expand). The actual size is also affected by the parent's constraints.
Container vs Other Widgets
- SizedBox: Use when you only need a fixed size or a simple space.
- ColoredBox: If you only need a background color,
ColoredBoxis more efficient. - DecoratedBox: For decoration without padding or margin.
- Padding / Align: Specialized widgets for single purposes.
- Container is convenient but can be overkill for simple use cases. Use the most specific widget for clarity and performance.
Common Mistakes
- Using both
coloranddecoration(throws an assertion).
- Using both
- Forgetting that
paddingandmarginare separate; mixing them up leads to unexpected spacing.
- Forgetting that
- Assuming a
Containerwithout a child will always fill the available space (it will, but only if constraints allow).
- Assuming a
- Using
Containerwhen a simpler widget likeSizedBoxorColoredBoxwould suffice – this can hurt performance in large lists.
- Using
Best Practices
- Always use
constconstructors when possible for compile‑time constants (e.g.,const Container()).
- Always use
- For reusable styles, consider extracting the decoration into a variable or using a
Theme.
- For reusable styles, consider extracting the decoration into a variable or using a
- Use
SizedBox.shrink()orContainer.shrink()to create empty widgets when needed.
- Use
- Prefer
EdgeInsets.only,EdgeInsets.symmetric, orEdgeInsets.fromLTRBfor specific paddings.
- Prefer
Complete Example
Key Takeaways
Containeris a multi‑purpose widget for styling and positioning a child.
- Use
paddingfor internal spacing,marginfor external spacing.
- Use
- For complex styling (borders, gradients, shadows), use
decorationwithBoxDecoration.
- For complex styling (borders, gradients, shadows), use
- You cannot set both
coloranddecoration; usedecorationwith acolorinside.
- You cannot set both
- Container sizes itself to its child, but can be constrained by
width,height, orconstraints.
- Container sizes itself to its child, but can be constrained by
- Always consider using a more specific widget for performance when possible.