SizedBox
A box with a specified size that constrains its child.
Overview
SizedBox
is a simple widget that forces its child to have a specific width and/or height. It’s commonly used to add fixed spacing or constrain widget sizes.
Basic Usage
// Fixed width and height
SizedBox(width: 100, height: 100) {
ColoredBox(color: Color(0xFF_FF6B6B))
}
// Width only
SizedBox(width: 200) {
Text("200px wide")
}
// Height only
SizedBox(height: 50) {
Text("50px tall")
}
// Empty spacer
SizedBox(width: 20) // Horizontal space
SizedBox(height: 20) // Vertical space
SizedBox Examples
Special Constructors
SizedBox.shrink()
Creates a zero-sized box:
SizedBox.shrink() // 0×0 box
Useful as placeholder when you want to conditionally show widgets:
condition ? Widget() : SizedBox.shrink()
SizedBox.expand()
Creates a box that’s as large as possible:
SizedBox.expand() {
ColoredBox(color: Color(0xFF_E0E0E0))
}
Equivalent to setting both width and height to infinity
.
Parameters
Parameter | Type | Description |
---|---|---|
width | Float? | Fixed width constraint |
height | Float? | Fixed height constraint |
child | Widget? | Optional child widget |