Diff between flexible and expanded?
Answer
Overview
Both
text
Flexibletext
Expandedtext
Rowtext
Columntext
FlexKey Differences
| Feature | Expanded | Flexible |
|---|---|---|
| Fills available space | ✅ Always fills | ⚙️ Optional (via text |
| Default fit | text | text |
| Child can be smaller | ❌ No | ✅ Yes |
| Shorthand for | text | — |
Expanded Example
dartRow( children: [ Expanded( child: Container(color: Colors.blue, height: 50), ), Expanded( child: Container(color: Colors.red, height: 50), ), ], ) // Both containers split the space equally and FILL it
Flexible Example
dartRow( children: [ Flexible( child: Container( color: Colors.blue, width: 50, // Can be SMALLER than its allocated space height: 50, ), ), Container(color: Colors.red, width: 100, height: 50), ], ) // Blue container only takes 50px, not the full flexible space
Using flex Property
dartRow( children: [ Expanded(flex: 2, child: Container(color: Colors.blue)), // 2/3 of space Expanded(flex: 1, child: Container(color: Colors.red)), // 1/3 of space ], )
FlexFit values
dart// Flexible with tight fit = same as Expanded Flexible( fit: FlexFit.tight, // Child MUST fill allocated space child: Container(), ) // Flexible with loose fit (default) Flexible( fit: FlexFit.loose, // Child CAN be smaller child: Container(), )
When to Use Which?
| Scenario | Use |
|---|---|
| Child must fill all allocated space | text |
| Child may be smaller than allocated space | text |
| Proportional sizing (2:1 ratio) | text text |
| Wrapping text that may not need full width | text |
Quick Rule: Use
when you want it to fill space. UsetextExpandedwhen the child might need less space than allocated.textFlexible