What if your app's layout could magically adjust itself perfectly on every device without extra work?
Why Expanded and Flexible in Flutter? - Purpose & Use Cases
Imagine you are designing a mobile app screen with several buttons and text fields. You want these elements to fill the available space nicely on different screen sizes, but you try to set fixed widths and heights manually.
On small phones, some buttons get cut off or overlap. On big tablets, the buttons stay tiny and leave too much empty space. It looks messy and inconsistent.
Manually setting fixed sizes means you must guess every screen size and orientation. This is slow and frustrating because you have to test on many devices.
Also, if you want to change the layout later, you must adjust many hardcoded values. It's easy to make mistakes and create a poor user experience.
Using Expanded and Flexible widgets in Flutter lets your UI elements automatically grow or shrink to fill available space.
They work like stretchy containers that adapt to screen size and other widgets around them. This means your layout stays neat and balanced on any device without guessing sizes.
Container(width: 100, child: Text('Button'))
Expanded(child: Container(child: Text('Button')))You can build responsive, flexible layouts that look great on all screen sizes with minimal effort.
Think of a music player app where the play, pause, and skip buttons always share the bottom space evenly, no matter if you hold your phone tall or wide.
Manually fixed sizes cause layout problems on different screens.
Expanded and Flexible widgets let UI elements adapt smoothly.
This makes your app look professional and user-friendly everywhere.
Practice
Expanded widget do inside a Row or Column in Flutter?Solution
Step 1: Understand Expanded behavior
TheExpandedwidget forces its child to fill the available space tightly inside aRoworColumn.Step 2: Compare with other options
Other options describe unrelated behaviors like padding or scrolling, whichExpandeddoes not do.Final Answer:
It makes the child widget fill all the available space tightly. -> Option CQuick Check:
Expanded = fills space tightly [OK]
- Confusing Expanded with Flexible
- Thinking Expanded adds padding
- Assuming Expanded makes widget scrollable
Flexible with a child widget in Flutter?Solution
Step 1: Recall Flexible widget syntax
The correct syntax requires the child widget passed as a named parameter:Flexible(child: ...).Step 2: Identify incorrect syntax
Options B, C, and D miss the named parameter or use invalid assignment syntax.Final Answer:
Flexible(child: Text('Hello')) -> Option AQuick Check:
Flexible uses child: parameter [OK]
- Omitting child: label
- Using assignment instead of constructor
- Passing child without parentheses
Row:
Row(
children: [
Expanded(child: Container(width: 50, color: Colors.red)),
Expanded(flex: 2, child: Container(width: 50, color: Colors.blue)),
],
)
What will be the relative widths of the red and blue containers on screen?Solution
Step 1: Understand Expanded and Expanded with flex
Expandedtakes 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 AQuick Check:
Flex ratio 1:2 means widths 1/3 and 2/3 [OK]
- Ignoring flex values
- Thinking width property overrides flex
- Assuming equal widths without flex
Row(
children: [
Flexible(flex: 1),
Container(color: Colors.green),
],
)Solution
Step 1: Check Flexible widget usage
Flexiblerequires 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 DQuick Check:
Flexible must have child [OK]
- Omitting child in Flexible
- Thinking flex must be >1
- Assuming Row needs 3 children
Row. The first button should take 1/4 of the width, the second button 3/4. Which Flutter widget setup achieves this best?Solution
Step 1: Understand flex distribution in Row
UsingFlexiblewith 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.Final Answer:
Row(children: [Flexible(flex: 1, child: Button1), Flexible(flex: 3, child: Button2)]) -> Option BQuick Check:
Flexible flex ratio 1:3 divides width correctly [OK]
- Using Expanded for unequal widths
- Using Column instead of Row
- Setting fixed widths instead of flex
Ready to prove it?
Pass the test and this pattern moves unknown
