text
stringlengths
1
372
),
],
)
<code_end>
the only difference if you use flexible instead of expanded,
is that flexible lets its child have the same or smaller
width than the flexible itself, while expanded forces
its child to have the exact same width of the expanded.
but both expanded and flexible ignore their children’s width
when sizing themselves.
info note
this means that it’s impossible to expand row children
proportionally to their sizes. the row either uses
the exact child’s width, or ignores it completely
when you use expanded or flexible.
<topic_end>
<topic_start>
example 28
<code_start>
scaffold(
body: container(
color: blue,
child: const column(
children: [
Text('Hello!'),
Text('Goodbye!'),
],
),
),
)
<code_end>
the screen forces the scaffold to be exactly the same size
as the screen, so the scaffold fills the screen.
the scaffold tells the container that it can be any size it wants,
but not bigger than the screen.
info note
when a widget tells its child that it can be smaller than a
certain size, we say the widget supplies loose constraints
to its child. more on that later.
<topic_end>
<topic_start>
example 29
<code_start>
scaffold(
body: SizedBox.expand(
child: container(
color: blue,
child: const column(
children: [
Text('Hello!'),
Text('Goodbye!'),
],
),
),
),
)
<code_end>
if you want the scaffold’s child to be exactly the same size
as the scaffold itself, you can wrap its child with
SizedBox.expand.
<topic_end>
<topic_start>
tight vs loose constraints
it’s very common to hear that some constraint is
“tight” or “loose”, so what does that mean?
<topic_end>
<topic_start>
tight constraints
a tight constraint offers a single possibility,
an exact size. in other words, a tight constraint
has its maximum width equal to its minimum width;
and has its maximum height equal to its minimum height.
an example of this is the app widget,
which is contained by the RenderView class:
the box used by the child returned by the
application’s build function is given a constraint
that forces it to exactly fill the application’s content area
(typically, the entire screen).
another example: if you nest a bunch of boxes inside
each other at the root of your application’s render tree,
they’ll all exactly fit in each other,
forced by the box’s tight constraints.
if you go to flutter’s box.dart file and search for
the BoxConstraints constructors,
you’ll find the following:
if you revisit example 2,
the screen forces the red container to be
exactly the same size as the screen.
the screen achieves that, of course, by passing tight
constraints to the container.
<topic_end>
<topic_start>
loose constraints
a loose constraint is one that has a minimum
of zero and a maximum non-zero.
some boxes loosen the incoming constraints,
meaning the maximum is maintained but the
minimum is removed, so the widget can have
a minimum width and height both equal to zero.
ultimately, center’s purpose is to transform