text
stringlengths
0
197
}
}
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.