text
stringlengths 1
474
|
---|
Flexible( |
child: Container( |
color: green, |
child: const Text( |
'Goodbye!', |
style: big, |
), |
), |
), |
], |
)<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 |
the tight constraints it received from its parent |
(the screen) to loose constraints for its child |
(the Container).If you revisit Example 3, |
the Center allows the red Container to be smaller, |
but not bigger than the screen.<topic_end> |
<topic_start>Unbounded constraints |
info Note |
You might be directed here if the framework |
detects a problem involving box constraints. |
The Flex section below might also apply.In certain situations, |
a box’s constraint is unbounded, or infinite. |
This means that either the maximum width or |
the maximum height is set to double.infinity.A box that tries to be as big as possible won’t |
function usefully when given an unbounded constraint and, |
in debug mode, throws an exception.The most common case where a render box ends up |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.