Skip to content

Intermediate · Layouts · topic 18 of 50 · 30 min

Container, Padding, SizedBox, Expanded and Flexible

Explanation

Container combines padding, margin, size, colour and decoration (BoxDecoration with rounded corners, borders and shadows). Padding adds space inside, SizedBox sets a fixed size or gap. Inside a Row or Column, Expanded makes a child fill the remaining space, Flexible lets it take up to its share, flex factors divide space in proportions, and Spacer pushes siblings apart.

Container: a box with style

Container is a convenience widget that combines several simpler ones: Padding, a size, a colour or decoration, a margin and alignment. Use it when you need a styled box, like a coloured card or a rounded badge.

dart
Container(
  margin: const EdgeInsets.all(8),                  // space outside the box
  padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),   // space inside
  decoration: BoxDecoration(
    color: Colors.orange.shade50,
    borderRadius: BorderRadius.circular(12),
    border: Border.all(color: Colors.orange),
  ),
  child: const Text('Diwali offer: 20% off sweets'),
)
Do not set both color and decoration on the same Container; put the colour inside BoxDecoration.

EdgeInsets describes space on each side: EdgeInsets.all(8), EdgeInsets.symmetric(horizontal: 16, vertical: 12), or EdgeInsets.only(left: 12, top: 4). Margin is outside the decoration; padding is inside it.

Padding and SizedBox

If you only need space, Padding is clearer and lighter than a Container. SizedBox forces a size on its child (SizedBox(width: 120, child: ...)) or, without a child, makes an empty gap. SizedBox.expand() fills all available space.

BoxDecoration can also add a boxShadow for a raised look, a gradient instead of a flat colour, or shape: BoxShape.circle for round badges. For most cards, though, the Material Card widget already gives a good default shape and elevation that follows the theme.

Expanded: fill the rest

dart
Row(
  children: [
    const Icon(Icons.search),
    const SizedBox(width: 8),
    Expanded(                                   // takes all remaining width
      child: Text('Search for kurtas, sarees, dupattas and more', overflow: TextOverflow.ellipsis),
    ),
    IconButton(onPressed: () {}, icon: const Icon(Icons.mic)),
  ],
)
Without Expanded, long text in a Row overflows; with it, the text gets exactly the space left over.

Expanded only works as a direct child of a Row, Column or Flex.

flex factors

dart
Row(
  children: [
    Expanded(flex: 2, child: Container(height: 40, color: Colors.green)),   // 2 parts
    Expanded(flex: 1, child: Container(height: 40, color: Colors.amber)),   // 1 part
  ],
)
The green box gets two thirds of the width, the amber box one third.

Flexible versus Expanded

WidgetBehaviour
ExpandedMust fill its share of the space (FlexFit.tight)
FlexibleMay be smaller than its share, up to its natural size (FlexFit.loose)
SpacerAn empty Expanded: pushes neighbours apart

Expanded is simply Flexible with fit: FlexFit.tight. Use Flexible when a child, such as a short label, should not be stretched but must still shrink if space is tight. Row and Column are both special cases of the Flex widget, which takes a direction parameter.

Think of it like this: Think of a Row as a bench. Fixed-size widgets are people who need exactly their seat. Expanded widgets are people who spread out to use all the remaining bench; with flex 2 and 1, the first takes twice as much as the second. Flexible people take only what they need, up to their share.

Common mistakes

  • Setting both color and decoration on a Container: an assertion error. Move the colour into BoxDecoration.
  • Using Expanded outside a Row, Column or Flex: "Incorrect use of ParentDataWidget".
  • Using Container only for padding. Prefer Padding; it says what you mean.
  • Forgetting overflow: TextOverflow.ellipsis for long text inside Expanded, so it wraps or overflows awkwardly.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Container, Padding, SizedBox, Expanded and Flexible · 1/6

Container(
  padding: const EdgeInsets.all(12),
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(12)),
)

Container is a box with padding, margin, size and decoration.

Prefer a video by a teacher? Find free videos on “flutter container padding sizedbox expanded flexible flex tutorial” on YouTube ↗

In real life: A product row in a shopping list

A grocery list screen shows each product with a fixed-size image, the name taking the remaining space (cut with an ellipsis if too long), and a quantity badge on the right. Expanded handles names of any length, and a decorated Container makes the badge.

dart
Row(
  children: [
    const SizedBox(width: 48, height: 48, child: Placeholder()),
    const SizedBox(width: 12),
    const Expanded(child: Text('Aashirvaad Shudh Chakki Atta 10 kg', overflow: TextOverflow.ellipsis)),
    Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(color: Colors.green.shade100, borderRadius: BorderRadius.circular(16)),
      child: const Text('x2'),
    ),
  ],
)
Fixed, flexible and fixed: the classic list-row pattern.

Practice (0/3 done)

Task 1 · Question

Two Expanded children in a Row have flex 3 and flex 1. What share of the width does the first get?

Task 2 · Question

A Container has both color: Colors.red and decoration: BoxDecoration(...). What happens?

Task 3 · Practice

Build a "wallet balance" card: a rounded Container with a gradient or colour, padding inside and margin outside; inside, a Row with "Balance" on the left and the amount on the right (use Spacer), and below a Row of two Expanded buttons (Add money, Send) with flex 1 each.

Quick quiz

Question 1

1.What does EdgeInsets.symmetric(horizontal: 16) add?

Question 2

2.What is the difference between Expanded and Flexible?

Question 3

3.Where can Expanded be used?

Question 4

4.Which widget is an empty space that pushes its neighbours apart in a Row?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does EdgeInsets.symmetric(horizontal: 16) add?

Answer: 16 on left and right

Horizontal means left and right.

What is the difference between Expanded and Flexible?

Answer: Expanded must fill its share; Flexible may be smaller

Expanded is Flexible with a tight fit.

Where can Expanded be used?

Answer: Directly inside a Row, Column or Flex

Otherwise you get a ParentDataWidget error.

Which widget is an empty space that pushes its neighbours apart in a Row?

Answer: Spacer

Spacer is an empty Expanded.

With box-sizing: border-box and width: 200px, padding: 20px, how wide is the box?Related

Answer: 200px

border-box makes width include padding and border.

From The box model

Which widget lets chips flow onto the next line instead of overflowing?Related

Answer: Wrap

Wrap moves children to new lines.

From Understanding constraints and fixing overflow

What is the best way to add equal space between flex items?Related

Answer: gap on the container

gap adds space only between items, never at the ends.

From Flexbox

Is nesting Rows and Columns expensive in Compose?Related

Answer: No, nesting is cheap; write clear code

Compose measures each child once.

From Layouts: Column, Row, Box and Scaffold

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Layouts” before its exam: Stack, Positioned and Align, Understanding constraints and fixing overflow, Images, icons, fonts and themes.
  2. Module 5 exam: Layouts · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Lists, navigation and forms. 50 topics left in the course before the final exam.

← Previous: Row and Column