text
stringlengths
1
474
<code_start>UnconstrainedBox(
child: Container(color: red, width: 4000, height: 50),
)<code_end>
The screen forces the UnconstrainedBox to be exactly
the same size as the screen, and UnconstrainedBox
lets its child Container be any size it wants.Unfortunately, in this case the Container is
4000 pixels wide and is too big to fit in
the UnconstrainedBox, so the UnconstrainedBox displays
the much dreaded “overflow warning”.<topic_end>
<topic_start>Example 15
<code_start>OverflowBox(
minWidth: 0,
minHeight: 0,
maxWidth: double.infinity,
maxHeight: double.infinity,
child: Container(color: red, width: 4000, height: 50),
)<code_end>
The screen forces the OverflowBox to be exactly the same
size as the screen, and OverflowBox lets its child Container
be any size it wants.OverflowBox is similar to UnconstrainedBox;
the difference is that it won’t display any warnings
if the child doesn’t fit the space.In this case, the Container has 4000 pixels of width,
and is too big to fit in the OverflowBox,
but the OverflowBox simply shows as much as it can,
with no warnings given.<topic_end>
<topic_start>Example 16
<code_start>UnconstrainedBox(
child: Container(color: Colors.red, width: double.infinity, height: 100),
)<code_end>
This won’t render anything, and you’ll see an error in the console.The UnconstrainedBox lets its child be any size it wants,
however its child is a Container with infinite size.Flutter can’t render infinite sizes, so it throws an error with
the following message: BoxConstraints forces an infinite width.<topic_end>
<topic_start>Example 17
<code_start>UnconstrainedBox(
child: LimitedBox(
maxWidth: 100,
child: Container(
color: Colors.red,
width: double.infinity,
height: 100,
),
),
)<code_end>
Here you won’t get an error anymore,
because when the LimitedBox is given an
infinite size by the UnconstrainedBox;
it passes a maximum width of 100 down to its child.If you swap the UnconstrainedBox for a Center widget,
the LimitedBox won’t apply its limit anymore
(since its limit is only applied when it gets infinite
constraints), and the width of the Container
is allowed to grow past 100.This explains the difference between a LimitedBox
and a ConstrainedBox.<topic_end>
<topic_start>Example 18
<code_start>const FittedBox(
child: Text('Some Example Text.'),
)<code_end>
The screen forces the FittedBox to be exactly the same
size as the screen. The Text has some natural width
(also called its intrinsic width) that depends on the
amount of text, its font size, and so on.The FittedBox lets the Text be any size it wants,
but after the Text tells its size to the FittedBox,
the FittedBox scales the Text until it fills all of
the available width.<topic_end>
<topic_start>Example 19
<code_start>const Center(
child: FittedBox(
child: Text('Some Example Text.'),
),
)<code_end>
But what happens if you put the FittedBox inside of a
Center widget? The Center lets the FittedBox
be any size it wants, up to the screen size.The FittedBox then sizes itself to the Text,
and lets the Text be any size it wants.
Since both FittedBox and the Text have the same size,
no scaling happens.<topic_end>
<topic_start>Example 20
<code_start>const Center(
child: FittedBox(
child: Text(
'This is some very very very large text that is too big to fit a regular screen in a single line.'),
),
)<code_end>
However, what happens if FittedBox is inside of a Center
widget, but the Text is too large to fit the screen?FittedBox tries to size itself to the Text,
but it can’t be bigger than the screen.
It then assumes the screen size,
and resizes Text so that it fits the screen, too.<topic_end>
<topic_start>Example 21
<code_start>const Center(
child: Text(
'This is some very very very large text that is too big to fit a regular screen in a single line.'),
)<code_end>
If, however, you remove the FittedBox, the Text
gets its maximum width from the screen,
and breaks the line so that it fits the screen.<topic_end>
<topic_start>Example 22
<code_start>FittedBox(
child: Container(
height: 20,
width: double.infinity,