Understanding Constraints - Flutter
Understanding Constraints - Flutter
See
What's new in docs, including a list of the new instructor-led video workshops!
arrow_drop_downUser interface
Introduction to widgets
arrow_drop_downBuilding layouts
Layouts in Flutter
Tutorial
Understanding constraints
Box constraints
Adding interactivity
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Platform integration
Migration notes
When someone learning Flutter asks you why some widget
with width:100 isn’t 100 pixels wide,
the default answer is to tell them
put that widget
inside of a Center, right?
Testing & debugging keyboard_arrow_down
Don’t do that.
Performance & optimization keyboard_arrow_down
If you do, they’ll come back again and again,
asking why some FittedBox isn’t working,
why that Column is overflowing, or what
Deployment keyboard_arrow_down IntrinsicWidth is supposed to be doing.
flutter CLI reference A widget gets its own constraints from its parent.
A constraint is just a set of 4 doubles:
a minimum and maximum width, a
Package site
minimum and maximum height.
Then the widget goes through its own list of children.
One by one, the widget tells its children what their
constraints are (wh
can be different for each child),
and then asks each child what size it wants to be.
Then, the widget positions its children
(horizontally in the x axis, and vertically in the y axis),
one by one.
And, finally, the widget tells its parent about its own size
(within the original constraints, of course).
Introduction to widgets Widget: “Hmmm, since I want to put my second child below the
first one, this leaves only 55 pixels of height for
my second child.”
arrow_drop_downBuilding layouts Widget: “Hey second child, You must be from 0 to 290 wide,
and 0 to 55 tall.”
Layouts in Flutter
Second child: “OK, I wish to be 140 pixels wide,
and 30 pixels tall.”
Tutorial
Creating adaptive and responsive Widget: “Very well. My first child has position x: 5 and y: 5,
and my second child has x: 80 and y: 25.”
apps
Widget: “Hey parent, I’ve decided that my size is going to be 300
pixels wide, and 60 pixels tall.”
Building adaptive apps [NEW]
Understanding constraints
Limitations
Box constraints
Adding interactivity
Animations
A widget can decide its own size only within the
constraints given to it by its parent.
This means a widget usually can’t have
Advanced UI size it wants.
Widget catalog
A widget can’t know and doesn’t decide its own position
in the screen, since it’s the widget’s parent who decides
the positio
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Add Flutter to existing app If a child wants a different size from its parent and the parent doesn’t have enough information to align it,
then the child’s siz
might be ignored.
Be specific when defining alignment.
Tools & features
Migration notes
7
Console expand_less Null safety no issue
Example 1
Get started keyboard_arrow_down
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down
Development
arrow_drop_downUser interface
Introduction to widgets Container(color: red)
arrow_drop_downBuilding layouts
Layouts in Flutter The screen is the parent of the Container, and it
forces the Container to be exactly the same size as the screen.
Tutorial
So the Container fills the screen and paints it red.
Creating adaptive and responsive
apps
Box constraints
Adding interactivity
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Platform integration
Container(width: 100, height: 100, color: red)
Migration notes
Who is Dash?
Widget index
API reference
Example 4
Align(
alignment: Alignment.bottomRight,
child: Container(width: 100, height: 100, color: red),
)
Get started keyboard_arrow_down
This is different from the previous example in that it uses
Align instead of Center.
Samples & tutorials keyboard_arrow_down 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
keyboard_arrow_down aligns the container to the bottom-right of the
available space.
Development
arrow_drop_downBuilding layouts
Layouts in Flutter
Tutorial
Understanding constraints
Box constraints
Adding interactivity
Accessibility & internationalization 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 infinit
Platform integration size, but since it can’t be bigger than the screen,
it just fills the screen.
Migration notes
Who is Dash?
Center(
child: Container(color: red),
Widget index )
API reference
flutter CLI reference The screen forces the Center to be exactly the
same size as the screen, so the Center fills the screen.
Package site
Example 7
Center(
child: Container(
color: red,
child: Container(color: green, width: 30, height: 30),
),
Get started keyboard_arrow_down )
Samples & tutorials keyboard_arrow_down The screen forces the Center to be exactly the same
size as the screen, so the Center fills the screen.
keyboard_arrow_down
Development The Center tells the red Container that it can be any size
it wants, but not bigger than the screen. Since the red
Container has no
but has a child,
it decides it wants to be the same size as its child.
arrow_drop_downUser interface
Introduction to widgets The red Container tells its child that it can be any size
it wants, but not bigger than the screen.
Understanding constraints
Box constraints
Adding interactivity
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Who is Dash?
Widget index
API reference
Package site
ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 70,
minHeight: 70,
maxWidth: 150,
maxHeight: 150,
),
child: Container(color: red, width: 10, height: 10),
)
Example 10
Get started keyboard_arrow_down
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down
Development
arrow_drop_downUser interface
Introduction to widgets Center(
child: ConstrainedBox(
arrow_drop_downBuilding layouts constraints: const BoxConstraints(
Layouts in Flutter minWidth: 70,
Tutorial minHeight: 70,
maxWidth: 150,
Creating adaptive and responsive
maxHeight: 150,
apps
),
Building adaptive apps [NEW] child: Container(color: red, width: 10, height: 10),
Understanding constraints ),
)
Box constraints
Adding interactivity
Now, Center allows ConstrainedBox to be any size up to
the screen size. The ConstrainedBox imposes additional
constraints from
Assets and images constraints parameter onto its child.
arrow_drop_down arrow_drop_down arrow_drop_down
Advanced UI
Platform integration
Migration notes
Example 12
Center(
child: ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 70,
minHeight: 70,
Get started keyboard_arrow_down maxWidth: 150,
maxHeight: 150,
Samples & tutorials keyboard_arrow_down ),
keyboard_arrow_down child: Container(color: red, width: 100, height: 100),
Development ),
)
arrow_drop_downUser interface
Introduction to widgets Center allows ConstrainedBox to be any size up to the
screen size. The ConstrainedBox imposes additional
constraints from its
constraints parameter onto its child.
arrow_drop_downBuilding layouts
Layouts in Flutter
The Container must be between 70 and 150 pixels.
It wants to have 100 pixels, and that’s the size it has,
since that’s between 70
Tutorial 150.
Creating adaptive and responsive
apps
Box constraints
Adding interactivity
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Who is Dash?
Widget index
API reference
Example 15
OverflowBox(
minWidth: 0.0,
minHeight: 0.0,
maxWidth: double.infinity,
maxHeight: double.infinity,
Get started keyboard_arrow_down child: Container(color: red, width: 4000, height: 50),
)
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down The screen forces the OverflowBox to be exactly the same
size as the screen, and OverflowBox lets its child Container
be any size
Development
wants.
Tutorial
Understanding constraints
Box constraints
Adding interactivity
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Migration notes Flutter can’t render infinite sizes, so it throws an error with
the following message: BoxConstraints forces an infinite width.
Who is Dash?
Widget index
API reference
Package site
UnconstrainedBox(
child: LimitedBox(
maxWidth: 100,
child: Container(
color: Colors.red,
width: double.infinity,
height: 100,
),
),
)
Example 18
Get started keyboard_arrow_down
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down
Development
arrow_drop_downUser interface
Introduction to widgets const FittedBox(
child: Text('Some Example Text.'),
arrow_drop_downBuilding layouts )
Layouts in Flutter
Box constraints
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Platform integration
Performance & optimization keyboard_arrow_down But what happens if you put the FittedBox inside of a
Center widget? The Center lets the FittedBox
be any size it wants, up to the
screen size.
Deployment keyboard_arrow_down
The FittedBox then sizes itself to the Text,
and lets the Text be any size it wants.
Since both FittedBox and the Text have the sam
Resources keyboard_arrow_down size,
no scaling happens.
keyboard_arrow_down
Reference
Who is Dash?
Example 20
Widget index
API reference
Package site
const Center(
child: FittedBox(
child: Text(
'This is some very very very large text that is too big to fit a regular screen in a single line.'),
),
)
Example 21
Get started keyboard_arrow_down
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down
Development
arrow_drop_downUser interface
Introduction to widgets const Center(
child: Text(
arrow_drop_downBuilding layouts 'This is some very very very large text that is too big to fit a regular screen in a single line.'),
Layouts in Flutter )
Tutorial
Creating adaptive and responsive If, however, you remove the FittedBox, the Text
gets its maximum width from the screen,
and breaks the line so that it fits the scr
apps
Understanding constraints
Example 22
Box constraints
Adding interactivity
Animations
Advanced UI
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Platform integration
FittedBox(
Packages & plugins child: Container(
Add Flutter to existing app
height: 20.0,
width: double.infinity,
Tools & features color: Colors.red,
Migration notes ),
)
Testing & debugging keyboard_arrow_down
FittedBox can only scale a widget that is bounded
(has non-infinite width and height). Otherwise,
it won’t render anything,
and you
Performance & optimization keyboard_arrow_down see an error in the console.
Deployment keyboard_arrow_down
Example 23
Resources keyboard_arrow_down
keyboard_arrow_down
Reference
Who is Dash?
Widget index
API reference
Package site
Row(
children: [
Container(color: red, child: const Text('Hello!', style: big)),
Container(color: green, child: const Text('Goodbye!', style: big)),
],
)
Example 24
Get started keyboard_arrow_down
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down
Development
arrow_drop_downUser interface
Introduction to widgets Row(
children: [
arrow_drop_downBuilding layouts Container(
Layouts in Flutter color: red,
Tutorial child: const Text(
'This is a very long text that '
Creating adaptive and responsive
'won\'t fit the line.',
apps
style: big,
Building adaptive apps [NEW] ),
Understanding constraints ),
Container(color: green, child: const Text('Goodbye!', style: big)),
Box constraints ],
Adding interactivity )
Assets and images
Since Row won’t impose any constraints onto its children,
it’s quite possible that the children might too big to fit
the available width
arrow_drop_down arrow_drop_down arrow_drop_down
Advanced UI
Platform integration
Migration notes
),
flutter CLI reference ),
Package site
Container(color: green, child: const Text('Goodbye!', style: big)),
],
)
Example 26
Row(
children: [
Expanded(
child: Container(
color: red,
Get started keyboard_arrow_down child: const Text(
'This is a very long text that won\'t fit the line.',
Samples & tutorials keyboard_arrow_down style: big,
keyboard_arrow_down ),
Development ),
),
arrow_drop_downUser interface Expanded(
Introduction to widgets child: Container(
color: green,
arrow_drop_downBuilding layouts child: const Text(
Layouts in Flutter 'Goodbye!',
Tutorial
style: big,
),
Creating adaptive and responsive ),
apps ),
Building adaptive apps [NEW] ],
)
Understanding constraints
Box constraints
If all of Row’s children are wrapped in Expanded widgets,
each Expanded has a size proportional to its flex parameter,
and only then
Adding interactivity each Expanded widget forces its child to have
the Expanded’s width.
Assets and images
In other words, Expanded ignores the preferred width of
its children.
arrow_drop_down arrow_drop_down arrow_drop_down
Animations
Advanced UI Example 27
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Platform integration
Migration notes
child: Container(
flutter CLI reference color: green,
child: const Text(
Package site
'Goodbye!',
style: big,
),
),
),
],
)
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.
Example 28
Get started keyboard_arrow_down
Samples & tutorials keyboard_arrow_down
keyboard_arrow_down
Development
arrow_drop_downUser interface
Introduction to widgets Scaffold(
body: Container(
arrow_drop_downBuilding layouts color: blue,
Layouts in Flutter
child: Column(
Tutorial children: const [
Creating adaptive and responsive Text('Hello!'),
apps Text('Goodbye!'),
],
Building adaptive apps [NEW] ),
Understanding constraints ),
)
Box constraints
Adding interactivity
The screen forces the Scaffold to be exactly the same size
as the screen, so the Scaffold fills the screen.
The Scaffold tells the
Assets and images
Container that it can be any size it wants,
but not bigger than the screen.
arrow_drop_down arrow_drop_down arrow_drop_down
Animations
Note:
When a widget tells its child that it can be smaller than a
certain size, we say the widget supplies loose constraints
to
Advanced UI
its child. More on that later.
Widget catalog
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Example 29
Accessibility & internationalization
Platform integration
Migration notes
Text('Goodbye!'),
flutter CLI reference ],
),
Package site
),
),
)
Note:
When a widget tells its child that it must be of
a certain size, we say the widget supplies tight
constraints to its child.
Adding interactivity
Animations
Knowing the general layout rule is necessary, but it’s not enough.
Advanced UI
Widget catalog Each widget has a lot of freedom when applying the general rule,
so there is no way of knowing what it will do by just reading
the
widget’s name.
arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down arrow_drop_down
Accessibility & internationalization If you try to guess, you’ll probably guess wrong.
You can’t know exactly how a widget behaves unless
you’ve read its documentati
Platform integration or studied its source-code.
Packages & plugins
The layout source-code is usually complex,
so it’s probably better to just read the documentation.
However, if you decide to study
Add Flutter to existing app layout source-code,
you can easily find it by using the navigating capabilities
of your IDE.
Tools & features
Here is an example:
Migration notes
Find a Column in your code and navigate to its
source code. To do this, use command+B (macOS)
or control+B (Windows/Linu
Testing & debugging keyboard_arrow_down 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).
Performance & optimization keyboard_arrow_down
Scroll down until you find a method called
createRenderObject(). As you can see,
this method returns a RenderFlex.
This is
Deployment keyboard_arrow_down render-object for the Column.
Now navigate to the source-code of RenderFlex,
which takes you to the flex.dart file.
Who is Dash?
flutter-dev@ •
terms •
brand usage •
security •
privacy •
español •
社区中文资源 • 한국어 •
We stand in solidarity with the Black community. Black Lives Matter.