Intermediate · Layouts · topic 18 of 50 · 30 min
Container, Padding, SizedBox, Expanded and Flexible
Explanation
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.
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'),
)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
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)),
],
)Expanded only works as a direct child of a Row, Column or Flex.
flex factors
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
],
)Flexible versus Expanded
| Widget | Behaviour |
|---|---|
| Expanded | Must fill its share of the space (FlexFit.tight) |
| Flexible | May be smaller than its share, up to its natural size (FlexFit.loose) |
| Spacer | An 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.
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.
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'),
),
],
)Practice (0/3 done)
Two Expanded children in a Row have flex 3 and flex 1. What share of the width does the first get?
A Container has both color: Colors.red and decoration: BoxDecoration(...). What happens?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Stack, Positioned and AlignLayouts · same module
- Understanding constraints and fixing overflowLayouts · same module
- The box modelFrontend Web Development · CSS basics
- FlexboxFrontend Web Development · CSS layout and responsive design
- Layouts: Column, Row, Box and ScaffoldKotlin and Android App Development · Jetpack Compose UI
- Colours, backgrounds and unitsFrontend Web Development · CSS basics
Questions people ask about this
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
Which widget lets chips flow onto the next line instead of overflowing?Related
What is the best way to add equal space between flex items?Related
Is nesting Rows and Columns expensive in Compose?Related
Answer: No, nesting is cheap; write clear code
Compose measures each child once.
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
- 3 more topics in “Layouts” before its exam: Stack, Positioned and Align, Understanding constraints and fixing overflow, Images, icons, fonts and themes.
- Module 5 exam: Layouts · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Lists, navigation and forms. 50 topics left in the course before the final exam.