Expanded and Flexible help you control how widgets grow and share space inside a row or column. They make your app look neat on different screen sizes.
You want buttons or boxes to fill available space evenly in a row.
You want one widget to take more space than others inside a column.
You want to avoid overflow errors when screen size changes.
You want to create responsive layouts that adjust nicely on phones and tablets.
Syntax
Flutter
Expanded(
flex: 1, // optional, default is 1
child: Widget(),
)
Flexible(
flex: 1, // optional, default is 1
fit: FlexFit.loose, // or FlexFit.tight
child: Widget(),
)
Expanded forces the child to fill the available space.
Flexible lets the child be smaller if it wants, depending on the fit.
This app shows a row with two colored boxes. The red box uses Expanded with flex 2, so it fills two parts of the space. The blue box uses Flexible with flex 1 and loose fit, so it only takes 50 pixels wide even if more space is available.
What will be the relative widths of the red and blue containers on screen?
medium
A. Red fills 1/3, blue fills 2/3 of the row width
B. Red fills half the row, blue fills the other half
C. Red and blue both have fixed width 50 pixels
D. Red fills all space, blue is ignored
Solution
Step 1: Understand Expanded and Expanded with flex
Expanded takes available space tightly with default flex=1. Expanded(flex:2) takes twice the space tightly.
Step 2: Calculate relative widths
Total flex = 1 (first Expanded) + 2 (second Expanded) = 3. Red gets 1/3, blue gets 2/3 of the row width. The width: 50 is overridden by the tight constraints.
Final Answer:
Red fills 1/3, blue fills 2/3 of the row width -> Option A
Quick Check:
Flex ratio 1:2 means widths 1/3 and 2/3 [OK]
Hint: Sum flex values, divide space proportionally [OK]
Common Mistakes:
Ignoring flex values
Thinking width property overrides flex
Assuming equal widths without flex
4. This Flutter code throws an error. What is the problem?
Flexible requires a child widget to display. Here, it is missing.
Step 2: Validate other options
Container can be inside Row, flex can be 1, and Row can have any number of children.
Final Answer:
Flexible widget is missing a child widget. -> Option D
Quick Check:
Flexible must have child [OK]
Hint: Flexible always needs a child widget [OK]
Common Mistakes:
Omitting child in Flexible
Thinking flex must be >1
Assuming Row needs 3 children
5. You want two buttons side by side in a Row. The first button should take 1/4 of the width, the second button 3/4. Which Flutter widget setup achieves this best?
hard
A. Row(children: [Expanded(child: Button1), Expanded(child: Button2)])
B. Row(children: [Flexible(flex: 1, child: Button1), Flexible(flex: 3, child: Button2)])
C. Row(children: [Button1, Button2]) with fixed widths
D. Column(children: [Flexible(flex: 1, child: Button1), Flexible(flex: 3, child: Button2)])
Solution
Step 1: Understand flex distribution in Row
Using Flexible with flex values 1 and 3 divides the Row width into 4 parts, assigning 1/4 and 3/4 respectively.
Step 2: Evaluate other options
Row(children: [Expanded(child: Button1), Expanded(child: Button2)]) gives equal widths (1/2 each). Row(children: [Button1, Button2]) with fixed widths is less flexible. Column(children: [Flexible(flex: 1, child: Button1), Flexible(flex: 3, child: Button2)]) uses Column, which stacks vertically, not side by side.