Skip to content

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.

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.

AttributeDescriptionCommon Values
childThe single widget contained by this containerAny Widget (Text, Image, Column, etc.)
alignmentAligns the child within the containerAlignment.center, topLeft, topRight, bottomLeft, bottomRight, centerLeft, centerRight
paddingEmpty space to surround the childEdgeInsets.all(10), only(), symmetric(), fromLTRB()
marginEmpty space to surround the containerEdgeInsets.all(10), only(), symmetric(), fromLTRB()
colorBackground color of the containerColors.red, Colors.blue[500], Color(0xFFAABBCC)
decorationThe decoration to paint behind the childBoxDecoration with color, border, gradient, etc.
foregroundDecorationThe decoration to paint in front of the childBoxDecoration
widthThe width of the containerdouble values, double.infinity
heightThe height of the containerdouble values, double.infinity
constraintsAdditional constraints on the containerBoxConstraints.expand(), tight(), loose()
transformTransform matrix to apply before paintingMatrix4.rotationZ(), translation(), scale()
clipBehaviorHow to clip the decoration and childClip.none, hardEdge, antiAlias, antiAliasWithSaveLayer

Since decoration is a key property, here are its common attributes:

AttributeDescriptionCommon Values
colorBackground color (overrides Container’s color)Colors.blue, Colors.green[400]
borderBorder around the containerBorder.all(), Border()
borderRadiusRounded cornersBorderRadius.circular(10), only(), all()
boxShadowShadow effects[BoxShadow()] list
gradientColor gradient backgroundLinearGradient(), RadialGradient()
shapeShape of the containerBoxShape.rectangle, circle
imageBackground imageDecorationImage()
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”
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
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
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

Container Example

  • 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
  • Use color for simple background colors
  • Use decoration for advanced styling (borders, gradients, shadows)
  • Cannot use both - if decoration is provided, any color must be specified in the decoration
  1. Apply constraints
  2. Apply padding
  3. Apply alignment
  4. Draw child (if any)
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'),
)
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
)
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue,
image: DecorationImage(
image: NetworkImage('url'),
fit: BoxFit.cover,
),
),
)
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.blue, Colors.purple],
),
),
)
  1. Avoid excessive nesting: Deep container trees can impact performance
  2. Use const constructors: When possible, use const Container() for better performance
  3. Reuse decorations: Consider reusing BoxDecoration objects if they’re identical
  4. Clip behavior: Use appropriate clipBehavior to avoid unnecessary clipping operations