text
stringlengths
1
474
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 GlasbergMarcelo 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
<topic_end>
<topic_start>Hot reload
Flutter’s hot reload feature helps you quickly and
easily experiment, build UIs, add features, and fix bugs.
Hot reload works by injecting updated source code files
into the running Dart Virtual Machine (VM).
After the VM updates classes with the new versions of fields and functions,
the Flutter framework automatically rebuilds the widget tree,
allowing you to quickly view the effects of your changes.<topic_end>
<topic_start>
How to perform a hot reload
To hot reload a Flutter app:If you’re working in an IDE/editor that supports Flutter’s IDE tools,
select Save All (cmd-s/ctrl-s),
or click the hot reload button on the toolbar.If you’re running the app at the command line using flutter run,
enter r in the terminal window.After a successful hot reload operation,
you’ll see a message in the console similar to:The app updates to reflect your change,
and the current state of the app is preserved.
Your app continues to execute from where it was prior
to run the hot reload command.
The code updates and execution continues.What is the difference between hot reload, hot restart,
and full restart?Flutter web currently supports hot restart but not
hot reload.
Controls for run, run debug, hot reload, and hot restart in Android StudioA code change has a visible effect only if the modified
Dart code is run again after the change. Specifically,
a hot reload causes all the existing widgets to rebuild.
Only code involved in the rebuilding of the widgets
is automatically re-executed. The main() and initState()
functions, for example, are not run again.<topic_end>
<topic_start>
Special cases
The next sections describe specific scenarios that involve
hot reload. In some cases, small changes to the Dart code
enable you to continue using hot reload for your app.
In other cases, a hot restart, or a full restart is needed.<topic_end>
<topic_start>
An app is killed
Hot reload can break when the app is killed.
For example, if the app was in the background for too long.<topic_end>
<topic_start>
Compilation errors
When a code change introduces a compilation error,
hot reload generates an error message similar to:In this situation, simply correct the errors on the