Component - Expanded and Flexible
This Flutter UI component demonstrates how Expanded and Flexible widgets control the size of children inside a Row. They help distribute space in a flexible way, like sharing a pizza slice fairly among friends.
This Flutter UI component demonstrates how Expanded and Flexible widgets control the size of children inside a Row. They help distribute space in a flexible way, like sharing a pizza slice fairly among friends.
Scaffold
├─ AppBar
│ └─ Text
└─ Body
└─ Row
├─ Expanded
│ └─ Container
├─ Flexible
│ └─ Container
└─ ContainerExpanded widget do inside a Row or Column in Flutter?Expanded widget forces its child to fill the available space tightly inside a Row or Column.Expanded does not do.Flexible with a child widget in Flutter?Flexible(child: ...).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?Expanded takes available space tightly with default flex=1. Expanded(flex:2) takes twice the space tightly.Row(
children: [
Flexible(flex: 1),
Container(color: Colors.green),
],
)Flexible requires a child widget to display. Here, it is missing.Row. The first button should take 1/4 of the width, the second button 3/4. Which Flutter widget setup achieves this best?Flexible with flex values 1 and 3 divides the Row width into 4 parts, assigning 1/4 and 3/4 respectively.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.Pass the test and this pattern moves unknown