Expanded

Fill available space in a Row, Column, or Flex.

Overview

Expanded makes its child widget fill the available space along the main axis of a Row, Column, or Flex. When multiple Expanded widgets are present, space is divided according to their flex factors.

Basic Usage

Row {
    Text("Fixed")
    
    Expanded {
        Text("This fills remaining space")
    }
    
    Text("Fixed")
}

The Expanded child takes all remaining horizontal space after fixed-size children are laid out.

Expanded Examples

Flex Factor

Control space distribution with flex:

Row {
    Expanded(flex: 1) {
        ColoredBox(color: Color(0xFF_FF6B6B))
    }
    
    Expanded(flex: 2) {
        ColoredBox(color: Color(0xFF_4ECDC4))
    }
    
    Expanded(flex: 1) {
        ColoredBox(color: Color(0xFF_FFE66D))
    }
}

Space is divided: 1/(1+2+1) = 25%, 2/4 = 50%, 1/4 = 25%

Parameters

ParameterTypeDescriptionDefault
flexFloatFlex factor for space distribution1
childWidgetThe widget to expandrequired

How It Works

  1. Layout inflexible children - Fixed-size widgets get their natural size
  2. Calculate remaining space - Subtract inflexible sizes from total
  3. Divide by flex sum - Each Expanded gets (flex / sum) of remaining
  4. Force child size - Child must fill its allocated space (tight constraints)

Expanded vs Flexible

Expanded is identical to Flexible with fit: .tight:

// These are equivalent
Expanded {
    Widget()
}

Flexible(fit: .tight) {
    Widget()
}

Use Flexible with fit: .loose when child can be smaller than allocated space.

See Also