Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 73 ยท Layout

Padding, SizedBox, Container and ConstrainedBox

Learn Flutter's spacing and sizing widgets: Padding, SizedBox, Container with BoxDecoration, margin vs padding, ConstrainedBox, LimitedBox and when to use each.

Flutter has no CSS. There is no margin: 8px property on every widget and no separate style sheet. Instead, spacing, sizing, backgrounds and borders are done by small, focused widgets that wrap their child. That feels unusual at first, but it follows from the idea that everything is a widget, and it keeps each widget simple to reason about.

In this lesson you will learn the four you will use most: Padding, SizedBox, Container and ConstrainedBox, plus LimitedBox for one special case. For each, the key question is the one from Constraints go down, sizes go up: what constraints does it pass to its child, and what size does it report back?

Real vs stand-in. All the widgets named here are real Flutter and the Dart code is read-only. The live examples are JavaScript stand-ins that compute sizes with the same constraint arithmetic.

Pick the small tool

Padding Space around a child.
SizedBox A fixed size or a gap.
Container Padding, size, color and border in one.
ConstrainedBox Extra min or max limits.
1 / 2

Padding and EdgeInsets

Padding shrinks the space available to its child by the given insets and then adds them back to the size it reports:

DART
Padding(
  padding: const EdgeInsets.all(16),
  child: Text('Hello'),
)

EdgeInsets has several constructors: all(16), symmetric(horizontal: 24, vertical: 8), only(left: 8, top: 4), fromLTRB(1, 2, 3, 4) and zero. For right-to-left support use EdgeInsetsDirectional.only(start: 8), which flips automatically.

The layout works like this: the child receives the parent's constraints minus the padding, and the Padding widget's own size is the child's size plus the padding. With 16 on every side and a child that is 100 by 20, the Padding is 132 by 52.

Padding is invisible: it has no color. If you want a colored area around the child, use a Container (below) or put a ColoredBox outside the Padding.

SizedBox: sizes and gaps

SizedBox forces its child to an exact size by giving it tight constraints (still clamped into whatever its own parent allows):

DART
const SizedBox(width: 120, height: 48, child: FilledButton(onPressed: null, child: Text('Go')))

const SizedBox(height: 16)       // an empty vertical gap in a Column
const SizedBox(width: 8)         // an empty horizontal gap in a Row
const SizedBox.expand(child: ColoredBox(color: Colors.black12))  // as big as allowed
const SizedBox.shrink()          // zero size: "render nothing"
const SizedBox.square(dimension: 40)

If you give only a width, the height follows the child (or the parent's maximum if there is no child). SizedBox with a const constructor costs nothing to rebuild (remember the const rule from Widget, Element and RenderObject trees), which is why it is preferred over an empty Container for gaps.

Container: the Swiss army knife

Container is not a primitive. It is a convenience widget that builds a chain of simpler ones, depending on which properties you set:

Property Behind the scenes
alignment Align
padding Padding
color ColoredBox
decoration DecoratedBox
width, height, constraints ConstrainedBox
margin Padding (outside everything else)
transform Transform
DART
Container(
  margin: const EdgeInsets.all(12),
  padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  decoration: BoxDecoration(
    color: Colors.indigo.shade50,
    borderRadius: BorderRadius.circular(12),
    border: Border.all(color: Colors.indigo, width: 1.5),
    boxShadow: const [BoxShadow(blurRadius: 6, offset: Offset(0, 2), color: Colors.black26)],
  ),
  child: const Text('Tag'),
)

Things to know about Container:

  • Margin is outside, padding is inside. The decoration (color, border, shadow) is painted on the area between them: it covers padding and child, but not the margin.
  • You cannot set both color and decoration. Put the color inside BoxDecoration(color: ...).
  • BoxDecoration supports color, gradient, border, borderRadius, boxShadow, image and shape: BoxShape.circle. A circle cannot have a borderRadius.
  • Sizing is context dependent. With no child and no size, a Container expands to fill the available space. With a child, it wraps the child. With width/height, it asks for that size, but, as always, only within its parent's constraints.
  • If you only need one of these features, use the specific widget. Padding instead of Container(padding: ...) is simpler and lets the framework do less work.

A button-like card that shows a ripple when tapped is the exception worth knowing: a Container's decoration is painted above a Material's ink splash and hides it. For tappable colored surfaces use Material with InkWell (or the Card/Ink widgets), covered in Material 3 widgets tour.

Margin versus padding in numbers

The stand-in below computes the box model of the Container above. Padding is added inside the decoration, margin outside, and the child's size is whatever is left.

JS Container box model

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both versions occupy the same total layout space, but in the second one the background shrinks to hug the text, because margin is never painted.

ConstrainedBox and LimitedBox

ConstrainedBox adds extra constraints to its child. Crucially, the extra constraints are combined with the incoming ones, and the parent still wins: Flutter clamps your min/max values into what the parent allows.

DART
ConstrainedBox(
  constraints: const BoxConstraints(minWidth: 120, maxWidth: 320, minHeight: 48),
  child: FilledButton(onPressed: save, child: const Text('Save')),
)

This is how you write "at least 120 px wide, at most 320" without fixing one value. Common uses are a minimum tap target of 48 by 48, a maximum content width on wide screens (for example maxWidth: 600 centered with Center), and a minimum height for a text area.

LimitedBox is narrower. Its maxWidth and maxHeight apply only when the incoming constraint is unbounded in that direction, and are ignored otherwise. It prevents the "unbounded" errors for widgets that would otherwise try to be infinite, for example a decorative box inside a scrollable list:

DART
LimitedBox(maxHeight: 200, child: Container(color: Colors.amber))

In a ListView, which gives unbounded height, the box becomes 200 tall. In a normal bounded parent, LimitedBox does nothing. Related widgets are UnconstrainedBox (removes constraints from the child and then overflows if the child is too big) and OverflowBox (lets the child ignore the parent's maximum). Reach for those rarely.

JS ConstrainedBox is clamped by the parent

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Which widget should I use?

  • A gap between siblings: SizedBox(height: 16) or width.
  • Space around something: Padding.
  • A fixed or flexible size: SizedBox / ConstrainedBox.
  • Background, border, radius, shadow, gradient: Container or DecoratedBox.
  • Several of these at once: Container.
  • Fill the remaining space inside a Row or Column: Expanded, not Container (Row, Column, Expanded and Flexible).
  • Position something in a corner: Align or Stack (Stack, Align and Positioned).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a Container, which part is painted by the BoxDecoration?

    Margin is outside the decoration and is never painted. The decoration covers the padding and the child.
  2. 2. What happens if you set both color and decoration on a Container?

    Container asserts that color and decoration are not both given. Use BoxDecoration(color: ...).
  3. 3. Why is const SizedBox(height: 16) preferred over an empty Container for a gap?

    SizedBox is a lightweight primitive and const instances are skipped during rebuilds.
  4. 4. A ConstrainedBox asks for minWidth: 400, but its parent only allows a maximum width of 300. What width does the child get?

    Requested constraints are clamped into the parent's constraints.
  5. 5. When does LimitedBox(maxHeight: 200) have an effect?

    LimitedBox ignores its limits when the parent already gives a bounded size.

Summary

  • Flutter styles by wrapping: Padding for space, SizedBox for sizes and gaps, Container for decoration and combinations, ConstrainedBox for min/max limits.
  • Margin sits outside the decoration, padding inside; you cannot use color and decoration together.
  • Requested constraints are always clamped into the parent's, so the parent has the last word.
  • LimitedBox only limits unbounded directions, like inside scrollables.
  • Prefer the most specific, const widget for the job.

Next: ListView, GridView and Slivers