Learn/Core Widgets/Expanded & Flexible

Expanded & Flexible

Control how widgets share space

4 min~5 min read+25 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. Flex Property
  2. 2. Flexible Widget
  3. 3. Common Patterns
  4. 4. Responsive Space Sharing Strategy
  5. 5. This Lesson In Simple Words
  6. 6. Your level focus
  7. 7. One practical habit
  8. 8. Deep Understanding Guide
  9. 9. Why this lesson matters
  10. 10. Study flow for better retention
  11. 11. Quick Glossary

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Expanded Widget

The Expanded widget forces its child to fill all available space inside a Row or Column. This is essential when you want widgets to share horizontal or vertical space proportionally.

dart
1Row(
2 children: [
3 Container(width: 100, color: Colors.red),
4 Expanded(
5 child: Container(color: Colors.blue), // Takes remaining space
6 ),
7 ],
8)
9
10// Multiple Expanded widgets share space equally
11Row(
12 children: [
13 Expanded(child: Container(color: Colors.red)),
14 Expanded(child: Container(color: Colors.blue)),
15 Expanded(child: Container(color: Colors.green)),
16 ],
17) // Each gets 1/3 of the width

Flex Property

The flex property controls the ratio of space each Expanded widget receives. By default, every Expanded widget has a flex value of 1, meaning they share space equally.

dart
1Row(
2 children: [
3 Expanded(
4 flex: 2, // Gets 2 parts
5 child: Container(color: Colors.red),
6 ),
7 Expanded(
8 flex: 1, // Gets 1 part
9 child: Container(color: Colors.blue),
10 ),
11 ],
12)
13// Red gets 2/3, Blue gets 1/3 of the width

Flexible Widget

Flexible is similar to Expanded, but the child is allowed to be smaller than the available space. This is useful when a widget has a preferred size but should shrink if needed.

dart
1Row(
2 children: [
3 Flexible(
4 child: Container(
5 width: 100, // Will be 100px if space allows
6 color: Colors.red,
7 ),
8 ),
9 Expanded(
10 child: Container(color: Colors.blue),
11 ),
12 ],
13)
14
15// Expanded = must fill space (FlexFit.tight)
16// Flexible = can be smaller (FlexFit.loose)

When to Use Which

💡 When to Use Which

Use Expanded when the widget should fill all available space. Use Flexible when the widget has a preferred size but can shrink if there is not enough room.

Common Patterns

Here are some frequently used patterns that combine Expanded and Flexible with other widgets.

dart
1// Search bar with button
2Row(
3 children: [
4 Expanded(
5 child: TextField(
6 decoration: InputDecoration(hintText: 'Search...'),
7 ),
8 ),
9 SizedBox(width: 8),
10 ElevatedButton(onPressed: () {}, child: Text('Search')),
11 ],
12)
13
14// 70-30 split
15Row(
16 children: [
17 Expanded(flex: 7, child: Image.network('https://example.com/image.jpg')),
18 Expanded(flex: 3, child: Text('Description')),
19 ],
20)

Responsive Space Sharing Strategy

Use flex ratios to express product priority:

  • Important content gets larger flex values
  • Supporting content gets smaller flex values
  • Action buttons often keep natural size while content expands

This approach creates adaptive layouts that still preserve business priorities on smaller screens.

Flex Layout Checklist

  • Critical content remains visible at small widths.
  • Flex values represent real importance, not guesswork.
  • Expanded is used only where full fill is expected.
  • Flexible is used where content can stay smaller naturally.

This Lesson In Simple Words

Control how widgets share space. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.

Your level focus

Build confidence with basics first; speed comes later.

One practical habit

Sketch the widget tree first, then code from top to bottom.

Deep Understanding Guide

Why this lesson matters

Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.

Study flow for better retention

  1. Read one section, run one tiny example, then explain the idea in your own words.
  2. When a UI bug appears, inspect constraints and parent-child relations before changing random values.
  3. If you can teach it in 45 seconds without notes, you understood it.

Common Mistakes To Avoid

  • Nesting too many widgets without extracting reusable pieces.
  • Ignoring constraints and guessing with random SizedBox heights.
  • Using one giant build method for unrelated UI sections.
  • Putting heavy calculations directly in build methods.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one card layout with Row/Column and rebuild it using Stack for comparison.
  • Split a large widget into 3 smaller widgets with clear responsibilities.
  • Use const constructors where possible and observe rebuild behavior.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "What does Expanded do?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Expanded & Flexible" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Nesting too many widgets without extracting reusable pieces.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

What does Expanded do?

Expanded forces its child to fill all remaining space in a Row or Column.

If Expanded widgets have flex: 2 and flex: 3, what ratio do they get?

Flex values create ratios: 2 parts vs 3 parts = 2:3.

Difference between Expanded and Flexible?

Flexible uses FlexFit.loose - child can be smaller. Expanded uses FlexFit.tight - must fill.

What is the default flex value for Expanded?

Default flex is 1, meaning all Expanded widgets share space equally unless specified otherwise.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue