text
stringlengths
1
474
);
}
}
//////////////////////////////////////////////////
class Example29 extends Example {
const Example29({super.key});
@override
final code = 'Scaffold(\n'
' body: Container(color: blue,\n'
' child: SizedBox.expand(\n'
' child: Column(\n'
' children: [\n'
' Text(\'Hello!\'),\n'
' Text(\'Goodbye!\')]))))';
@override
final String explanation =
'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.'
'\n\n'
'When a widget tells its child that it must be of a certain size, '
'we say the widget supplies "tight" constraints to its child. More on that later.';
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: Container(
color: blue,
child: const Column(
children: [
Text('Hello!'),
Text('Goodbye!'),
],
),
),
),
);
}
}
//////////////////////////////////////////////////<code_end>
If you prefer, you can grab the code from
this GitHub repo.The examples are explained in the following sections.<topic_end>
<topic_start>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>