text
stringlengths 1
372
|
|---|
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
|
with an unbounded constraint is within a flex box
|
(row or column),
|
and within a scrollable region
|
(such as ListView and other ScrollView subclasses).
|
ListView, for example,
|
tries to expand to fit the space available
|
in its cross-direction
|
(perhaps it’s a vertically-scrolling block and
|
tries to be as wide as its parent).
|
if you nest a vertically scrolling ListView
|
inside a horizontally scrolling ListView,
|
the inner list tries to be as wide as possible,
|
which is infinitely wide,
|
since the outer one is scrollable in that direction.
|
the next section describes the error you might
|
encounter with unbounded constraints in a flex widget.
|
<topic_end>
|
<topic_start>
|
flex
|
a flex box (row and column) behaves
|
differently depending on whether its
|
constraint is bounded or unbounded in
|
its primary direction.
|
a flex box with a bounded constraint in its
|
primary direction tries to be as big as possible.
|
a flex box with an unbounded constraint
|
in its primary direction tries to fit its children
|
in that space. each child’s flex value must be
|
set to zero, meaning that you can’t use
|
expanded when the flex box is inside
|
another flex box or a scrollable;
|
otherwise it throws an exception.
|
the cross direction
|
(width for column or height for row),
|
must never be unbounded,
|
or it can’t reasonably align its children.
|
<topic_end>
|
<topic_start>
|
learning the layout rules for specific widgets
|
knowing the general layout rule is necessary, but it’s not enough.
|
each widget has a lot of freedom when applying the general rule,
|
so there is no way of knowing how it behaves by just reading
|
the widget’s name.
|
if you try to guess, you’ll probably guess wrong.
|
you can’t know exactly how a widget behaves unless
|
you’ve read its documentation, or studied its source-code.
|
the layout source-code is usually complex,
|
so it’s probably better to just read the documentation.
|
however, if you decide to study the layout source-code,
|
you can easily find it by using the navigating capabilities
|
of your IDE.
|
here’s an example:
|
find a column in your code and navigate to its
|
source code. to do this, use command+B (macos)
|
or control+B (windows/linux) in android studio or IntelliJ.
|
you’ll be taken to the basic.dart file.
|
since column extends flex, navigate to the flex
|
source code (also in basic.dart).
|
scroll down until you find a method called
|
createRenderObject(). as you can see,
|
this method returns a RenderFlex.
|
this is the render-object for the column.
|
now navigate to the source-code of RenderFlex,
|
which takes you to the flex.dart file.
|
scroll down until you find a method called
|
performLayout(). this is the method that does
|
the layout for the column.
|
original article by marcelo glasberg
|
marcelo originally published this content as
|
flutter: the advanced layout rule even beginners must know
|
on medium. we loved it and asked that he allow us to publish
|
in on docs.flutter.dev, to which he graciously agreed. thanks, marcelo!
|
you can find marcelo on GitHub and pub.dev.
|
also, thanks to simon lightfoot for creating the
|
header image at the top of the article.
|
info note
|
to better understand how flutter implements layout
|
constraints, check out the following 5-minute video:
|
decoding flutter: unbounded height and width
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.