text
stringlengths
1
372
example 1
<code_start>
container(color: red)
<code_end>
the screen is the parent of the container, and it
forces the container to be exactly the same size as the screen.
so the container fills the screen and paints it red.
<topic_end>
<topic_start>
example 2
<code_start>
container(width: 100, height: 100, color: red)
<code_end>
the red container wants to be 100 × 100,
but it can’t, because the screen forces it to be
exactly the same size as the screen.
so the container fills the screen.
<topic_end>
<topic_start>
example 3
<code_start>
center(
child: container(width: 100, height: 100, color: red),
)
<code_end>
the screen forces the center to be exactly the same size
as the screen, so the center fills the screen.
the center tells the container that it can be any size it
wants, but not bigger than the screen. now the container
can indeed be 100 × 100.
<topic_end>
<topic_start>
example 4
<code_start>
align(
alignment: Alignment.bottomRight,
child: container(width: 100, height: 100, color: red),
)
<code_end>
this is different from the previous example in that it uses
align instead of center.
align also tells the container that it can be any size it
wants, but if there is empty space it won’t center the container.
instead, it aligns the container to the bottom-right of the
available space.
<topic_end>
<topic_start>
example 5
<code_start>
center(
child: container(
width: double.infinity, height: double.infinity, color: red),
)
<code_end>
the screen forces the center to be exactly the
same size as the screen, so the center fills the screen.
the center tells the container that it can be any size it wants,
but not bigger than the screen. the container wants to be
of infinite size, but since it can’t be bigger than the screen,
it just fills the screen.
<topic_end>
<topic_start>
example 6
<code_start>
center(
child: container(color: red),
)
<code_end>
the screen forces the center to be exactly the
same size as the screen, so the center fills the screen.
the center tells the container that it can be any
size it wants, but not bigger than the screen.
since the container has no child and no fixed size,
it decides it wants to be as big as possible,
so it fills the whole screen.
but why does the container decide that?
simply because that’s a design decision by those who
created the container widget. it could have been
created differently, and you have to read the
container API documentation to understand
how it behaves, depending on the circumstances.
<topic_end>
<topic_start>
example 7
<code_start>
center(
child: container(
color: red,
child: container(color: green, width: 30, height: 30),
),
)
<code_end>
the screen forces the center to be exactly the same
size as the screen, so the center fills the screen.
the center tells the red container that it can be any size
it wants, but not bigger than the screen. since the red
container has no size but has a child,
it decides it wants to be the same size as its child.
the red container tells its child that it can be any size
it wants, but not bigger than the screen.