Flutter Container
The Container widget is extremely versatile and serves as the building block for most Flutter UIs, providing a convenient way to combine multiple styling and layout properties in a single widget.
Container Overview
Section titled “Container Overview”A Container is a versatile widget that combines common painting, positioning, and sizing functionalities. It’s one of the most frequently used widgets in Flutter for creating visual elements.
Most Used Attributes and Values
Section titled “Most Used Attributes and Values”| Attribute | Description | Common Values |
|---|---|---|
child | The single widget contained by this container | Any Widget (Text, Image, Column, etc.) |
alignment | Aligns the child within the container | Alignment.center, topLeft, topRight, bottomLeft, bottomRight, centerLeft, centerRight |
padding | Empty space to surround the child | EdgeInsets.all(10), only(), symmetric(), fromLTRB() |
margin | Empty space to surround the container | EdgeInsets.all(10), only(), symmetric(), fromLTRB() |
color | Background color of the container | Colors.red, Colors.blue[500], Color(0xFFAABBCC) |
decoration | The decoration to paint behind the child | BoxDecoration with color, border, gradient, etc. |
foregroundDecoration | The decoration to paint in front of the child | BoxDecoration |
width | The width of the container | double values, double.infinity |
height | The height of the container | double values, double.infinity |
constraints | Additional constraints on the container | BoxConstraints.expand(), tight(), loose() |
transform | Transform matrix to apply before painting | Matrix4.rotationZ(), translation(), scale() |
clipBehavior | How to clip the decoration and child | Clip.none, hardEdge, antiAlias, antiAliasWithSaveLayer |
BoxDecoration Properties
Section titled “BoxDecoration Properties”Since decoration is a key property, here are its common attributes:
| Attribute | Description | Common Values |
|---|---|---|
color | Background color (overrides Container’s color) | Colors.blue, Colors.green[400] |
border | Border around the container | Border.all(), Border() |
borderRadius | Rounded corners | BorderRadius.circular(10), only(), all() |
boxShadow | Shadow effects | [BoxShadow()] list |
gradient | Color gradient background | LinearGradient(), RadialGradient() |
shape | Shape of the container | BoxShape.rectangle, circle |
image | Background image | DecorationImage() |
Example Usage
Section titled “Example Usage”Container( width: 200, height: 100, padding: EdgeInsets.all(16), margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20), decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.black26, blurRadius: 6, offset: Offset(0, 2), ), ], gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.blue, Colors.lightBlue], ), ), alignment: Alignment.center, child: Text( 'Hello Container!', style: TextStyle(color: Colors.white, fontSize: 16), ),)Visual Representation with Mermaid Diagrams
Section titled “Visual Representation with Mermaid Diagrams”Container Structure Visualization
Section titled “Container Structure Visualization”flowchart TD
subgraph LayoutFlow[Container Layout Flow]
direction LR
Step1[Apply Constraints] --> Step2[Apply Padding] --> Step3[Apply Alignment] --> Step4[Draw Child]
end
flowchart TD
subgraph ContainerAnatomy[Container Anatomy]
direction TB
subgraph ContainerWidget[Container Widget]
direction LR
MarginSpace[Margin Space] --> PaddingSpace[Padding Space]
PaddingSpace --> ContentArea[Content Area]
end
ContentArea --> ChildWidget[Child Widget]
subgraph DecorationLayers[Decoration Layers]
direction TB
Background[Background Color/Gradient] --> BorderLayer[Border] --> BoxShadowLayer[Box Shadow]
end
ContainerWidget --> DecorationLayers
end
Container with Decoration Properties
Section titled “Container with Decoration Properties”flowchart TD
subgraph BorderTypes[Border Types]
direction LR
AllBorder[Border.all
width: 2, color: black] --> SpecificBorder[Border
top, bottom, left, right] --> Rounded[BorderRadius.circular
10]
end
subgraph ShadowProperties[BoxShadow Properties]
direction TB
ShadowColor[color: Colors.black] --> ShadowOffset[offset: Offset2, 2] --> ShadowBlur[blurRadius: 4] --> ShadowSpread[spreadRadius: 1]
end
flowchart TD
subgraph ContainerDecorations[Container Decoration Properties]
direction TB
ContainerBase[Container] --> BoxDecoration[BoxDecoration]
subgraph DecorationProperties[Decoration Properties]
direction LR
BGColor[Background Color] --> Gradient[Gradient] --> BorderProp[Border]
BorderProp --> BorderRadius[BorderRadius] --> BoxShadow[BoxShadow]
BoxShadow --> Shape[Shape] --> Image[Background Image]
end
BoxDecoration --> DecorationProperties
end
Container Sizing and Constraints
Section titled “Container Sizing and Constraints”flowchart LR
subgraph ConstraintTypes[Constraint Types]
direction LR
Expand[BoxConstraints.expand
Fill available space] --> Tight[BoxConstraints.tight
Exact size] --> Loose[BoxConstraints.loose
Max size]
end
flowchart LR
subgraph ContainerSizing[Container Sizing Behavior]
direction TB
subgraph WithChild[Container with Child]
direction LR
WC1[No width/height
Size to child] --> WC2[Width/height set
Fixed size] --> WC3[Constraints set
Constrained size]
end
subgraph WithoutChild[Container without Child]
direction LR
WOC1[No size constraints
Size to parent] --> WOC2[Width/height set
Fixed size] --> WOC3[Constraints set
Constrained size]
end
SizingTitle[Container Sizing] --> WithChild
SizingTitle --> WithoutChild
end
Practical Container Examples
Section titled “Practical Container Examples”Key Behaviors and Characteristics
Section titled “Key Behaviors and Characteristics”1. Sizing Behavior
Section titled “1. Sizing Behavior”- With child: Container sizes itself to its child (unless dimensions are specified)
- Without child: Container expands to fill available space (unless dimensions are specified)
- With dimensions: Container uses explicit width/height values
2. Decoration vs Color
Section titled “2. Decoration vs Color”- Use
colorfor simple background colors - Use
decorationfor advanced styling (borders, gradients, shadows) - Cannot use both - if
decorationis provided, anycolormust be specified in the decoration
3. Layout Order
Section titled “3. Layout Order”- Apply constraints
- Apply padding
- Apply alignment
- Draw child (if any)
Common Use Cases
Section titled “Common Use Cases”1. Basic Styled Box
Section titled “1. Basic Styled Box”Container( padding: EdgeInsets.all(16), margin: EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8), border: Border.all(color: Colors.grey), ), child: Text('Content'),)2. Card with Shadow
Section titled “2. Card with Shadow”Container( margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.black12, blurRadius: 6, offset: Offset(0, 3), ), ], ), child: // content)3. Circle Avatar
Section titled “3. Circle Avatar”Container( width: 100, height: 100, decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.blue, image: DecorationImage( image: NetworkImage('url'), fit: BoxFit.cover, ), ),)4. Gradient Background
Section titled “4. Gradient Background”Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Colors.blue, Colors.purple], ), ),)Performance Considerations
Section titled “Performance Considerations”- Avoid excessive nesting: Deep container trees can impact performance
- Use const constructors: When possible, use
const Container()for better performance - Reuse decorations: Consider reusing
BoxDecorationobjects if they’re identical - Clip behavior: Use appropriate clipBehavior to avoid unnecessary clipping operations