Protected against bots by Yandex SmartCaptcha. Data processing policy
ConstrainedBox
Imposes additional size constraints on the child: "no narrower than", "no wider than", "no shorter than", "no taller than". Unlike SizedBox , it does not set the size but only sets the bounds within which the child chooses it itself.
Type:
single-child
ConstrainedBox
#constraints minWidth:100 minHeight:50
ColoredBox color:#4f46e5
Center
Text "At least 100×50"
Nested structures
#constraints
| Parameter | Type | Default | Description |
|---|---|---|---|
minWidth
|
length | 0 | Minimum width |
maxWidth
|
length | unlimited | Maximum width |
minHeight
|
length | 0 | Minimum height |
maxHeight
|
length | unlimited | Maximum height |
A length is a number (pixels) or a string with a unit:
"100svh"
,
"50%"
.
The first positional word can pick a ready-made mode:
expand
(take everything, optionally with
width:
/
height:
),
tight
,
tightFor
,
loose
:
ConstrainedBox
#constraints expand width:200
Text "The width is fixed, the height is all that is available"
There are no parameters directly on
ConstrainedBox
(
maxWidth:150
and the like) — only
#constraints
.
Examples
A column no wider than a readable width
Center
ConstrainedBox
#constraints maxWidth:640
Column spacing:8 crossAxisAlignment:start
Text "Heading"
Text "A long paragraph does not stretch wider than 640 pixels on a wide screen."
A button no narrower than 120
ConstrainedBox
#constraints minWidth:120 minHeight:44
ElevatedButton onPressed:"navigate:/home"
Center
Text "OK"
Differences between the app and the site
-
The site reads only
minWidth,maxWidth,minHeight,maxHeight. The named modes (expand,tight,tightFor,loose) have no effect on the site — no constraints appear. -
If the parent sets a tight size (
SizedBox width:800), in the app it overrides the child'smaxWidth, while on the sitemaxWidthstays in force — you get 640, not 800.
Best practices
-
A readable text width on a page is a
ConstrainedBoxwithmaxWidthinside aCenter; on a narrow screen it simply shrinks to the window width. -
Need an exact size —
SizedBox
; need only bounds —
ConstrainedBox.
See also
- SizedBox — a fixed size
-
Container
— the same through
#constraints, plus padding and decoration - AspectRatio — size from the aspect ratio