Flutter Layout Cheat Sheet - Flutter Community - Medium PDF
Flutter Layout Cheat Sheet - Flutter Community - Medium PDF
[Link] 1/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
In case you are interested in a similar article about Animations, then visit Flutter
Animations Cheat Sheet.
. . .
Table of Contents
Row and Column
Stack
Expanded
ConstrainedBox
Container
decoration: BoxDecoration
• image: DecorationImage
• border: Border
• borderRadius: BorderRadius
• shape: BoxShape
• boxShadow: List<BoxShadow>
• gradient: RadialGradient
• backgroundBlendMode: BlendMode
Material
• shape: BeveledRectangleBorder
Slivers
• SliverFillRemaining
SizedBox
SafeArea
[Link] 2/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 3/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
. . .
[Link] 4/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 5/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 6/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 7/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 8/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
You should use [Link] if you require for the baseline of different
text be aligned.
Row(
crossAxisAlignment: [Link],
textBaseline: [Link],
children: <Widget>[
Text(
'Baseline',
style: [Link](context).textTheme.display3,
),
Text(
'Baseline',
style: [Link](context).textTheme.body1,
),
],
),
. . .
CrossAxisAlignment
[Link] 9/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 10/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 11/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 12/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
MainAxisSize
[Link] 13/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 14/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 15/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
But you would like to have all buttons as wide as the widest, just use IntrinsicWidth :
[Link] 16/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
In case you have a similar problem but you would like to have all the widgets as tall as
the tallest just use a combination of IntrinsicHeight and Row widgets.
. . .
Stack
Perfect for overlaying Widgets on top of each other
[Link] 17/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
@override
Widget build(BuildContext context) {
Widget main = Scaffold(
appBar: AppBar(title: Text('Stack')),
);
return Stack(
fit: [Link],
children: <Widget>[
main,
Banner(
message: "Top Start",
location: [Link],
),
Banner(
message: "Top End",
location: [Link],
),
Banner(
message: "Bottom Start",
location: [Link],
),
Banner(
message: "Bottom End",
location: [Link],
),
],
);
}
[Link] 18/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
With your own Widgets, you need to place them in Positioned Widget
[Link] 19/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
),
],
),
);
}
. . .
If you don’t want to guess the top/bottom values you can use LayoutBuilder to retrieve
them
fit: [Link],
children: <Widget>[
Material(color: [Link]),
Positioned(
top: 0,
child: Icon([Link], size: iconSize),
),
Positioned(
top: [Link] - iconSize,
left: [Link] - iconSize,
child: Icon([Link], size: iconSize),
),
],
),
),
);
}
. . .
Expanded
Expanded works with Flex\Flexbox layout and is great for distributing space between
multiple items.
[Link] 21/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Row(
children: <Widget>[
Expanded(
child: Container(
decoration: const BoxDecoration(color: [Link]),
),
flex: 3,
),
Expanded(
child: Container(
decoration: const BoxDecoration(color: [Link]),
),
flex: 2,
),
Expanded(
child: Container(
decoration: const BoxDecoration(color: [Link]),
),
flex: 1,
),
],
),
. . .
ConstrainedBox
By default, most of the widgets will use as little space as possible:
[Link] 22/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
[Link] 23/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
ConstrainedBox(
constraints: [Link](),
child: const Card(
child: const Text('Hello World!'),
color: [Link],
),
),
. . .
Using BoxConstraints you specify how much space a widget can have — you specify
min / max of height / width .
[Link] uses infinite (all the available) amount of space unless specified:
[Link] 24/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
ConstrainedBox(
constraints: [Link](height: 300),
child: const Card(
child: const Text('Hello World!'),
color: [Link],
),
),
ConstrainedBox(
constraints: BoxConstraints(
minWidth: [Link],
maxWidth: [Link],
minHeight: 300,
maxHeight: 300,
),
child: const Card(
child: const Text('Hello World!'),
color: [Link],
),
),
. . .
Container
One of the most used Widgets — and for good reasons:
child ’s size
[Link] 25/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
If you want to stretch the Container to match its parent, use [Link] for the
height and width properties
[Link] 26/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Container as decoration
You can use color property to affect Container ’s background but decoration and
foregroundDecoration . (With those two properties, you can completely change how
Containe r looks like but I will be talking about different decorations later as it quite a big
topic)
decoration is always placed behind the child, whereas foregroundDecoration is on top
of the child
[Link] 27/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
decoration
. . .
[Link] 28/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Container as Transform
If you don’t want to use Transform widget to change your layout, you can use transform
[Link] 29/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
BoxDecoration
The decoration is usually used on a Container widget to change how the container looks.
image: DecorationImage
Puts an image as a background:
[Link] 30/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('image: DecorationImage')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
color: [Link],
image: DecorationImage(
fit: [Link],
image: NetworkImage(
'[Link]
[Link]',
),
),
),
),
),
);
border: Border
Specifies how should the border of the Container look like.
[Link] 31/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('border: Border')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
color: [Link],
border: [Link](color: [Link], width: 3),
),
),
),
);
borderRadius: BorderRadius
Enables border corners to be rounded.
[Link] 32/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('borderRadius: BorderRadius')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
color: [Link],
border: [Link](color: [Link], width: 3),
borderRadius: [Link]([Link](18)),
),
),
),
);
shape: BoxShape
Box decoration can be either a rectangle/square or an ellipse/circle.
For any other shape, you can use ShapeDecoration instead of BoxDecoration
[Link] 33/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('shape: BoxShape')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
color: [Link],
shape: [Link],
),
),
),
);
boxShadow: List<BoxShadow>
Adds shadow to the Container.
This parameter is a list because you can specify multiple different shadows and merge
them together.
[Link] 34/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('boxShadow: List<BoxShadow>')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
color: [Link],
boxShadow: const [
BoxShadow(blurRadius: 10),
],
),
),
),
);
gradient
There are three types of gradients: LinearGradient , RadialGradient and SweepGradient .
[Link] 35/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
LinearGradient
Scaffold(
appBar: AppBar(title: Text('gradient: LinearGradient')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: const [
[Link],
[Link],
],
),
),
),
),
);
. . .
[Link] 36/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
RadialGradient
Scaffold(
appBar: AppBar(title: Text('gradient: RadialGradient')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
gradient: RadialGradient(
colors: const [[Link], [Link]],
stops: const [0.4, 1.0],
),
),
),
),
);
. . .
[Link] 37/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
SweepGradient
Scaffold(
appBar: AppBar(title: Text('gradient: SweepGradient')),
body: Center(
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
gradient: SweepGradient(
colors: const [
[Link],
[Link],
[Link],
[Link],
[Link],
],
stops: const [0.0, 0.25, 0.5, 0.75, 1.0],
),
),
),
),
);
backgroundBlendMode
backgroundBlendMode is the most complex property of BoxDecoration .
[Link] 38/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
With backgroundBlendMode you can use a long list of algorithms specified in BlendMode
enum.
Scaffold(
appBar: AppBar(title: Text('backgroundBlendMode')),
body: Center(
child: Container(
height: 200,
width: 200,
foregroundDecoration: BoxDecoration(
backgroundBlendMode: [Link],
gradient: LinearGradient(
colors: const [
[Link],
[Link],
],
),
),
[Link] 39/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
child: [Link](
'[Link]
),
),
),
);
backgroundBlendMode does not affect only the Container it’s located in.
backgroundBlendMode changes the color of anything that is up the widget tree from the
Container .
The following code has a parent Container that draws an image and child Container
that uses backgroundBlendMode . Still, you would get the same effect as previously.
Scaffold(
appBar: AppBar(title: Text('backgroundBlendMode')),
body: Center(
child: Container(
decoration: BoxDecoration(
[Link] 40/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
image: DecorationImage(
image: NetworkImage(
'[Link]
[Link]',
),
),
),
child: Container(
height: 200,
width: 200,
foregroundDecoration: BoxDecoration(
backgroundBlendMode: [Link],
gradient: LinearGradient(
colors: const [
[Link],
[Link],
],
),
),
),
),
),
);
. . .
Material
Border with cut corners
[Link] 41/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('shape: BeveledRectangleBorder')),
body: Center(
child: Material(
shape: const BeveledRectangleBorder(
borderRadius: [Link]([Link](20)),
side: BorderSide(color: [Link], width: 4),
),
color: [Link],
child: Container(
height: 200,
width: 200,
),
),
),
);
. . .
Slivers
SliverFillRemaining
This Widget is irreplaceable when you want to center your content even if there is not
enough space for it. Interactive example
[Link] 42/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('SliverFillRemaining')),
body: CustomScrollView(
slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
mainAxisAlignment: [Link],
children: const [
FlutterLogo(size: 200),
Text(
'This is some longest text that should be centered'
'together with the logo',
textAlign: [Link],
),
],
),
),
],
),
);
In case there is no enough space for the centred content, SliverFillRemaining will
become scrollable:
[Link] 43/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
If it was not for SliverFillRemaining , the content would overflow like this:
[Link] 44/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
[Link] 45/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Scaffold(
appBar: AppBar(title: Text('SliverFillRemaining')),
body: CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildListDelegate(const [
ListTile(title: Text('First item')),
ListTile(title: Text('Second item')),
ListTile(title: Text('Third item')),
ListTile(title: Text('Fourth item')),
]),
),
SliverFillRemaining(
hasScrollBody: false,
child: Container(
color: [Link],
child: Column(
mainAxisAlignment: [Link],
children: const [
FlutterLogo(size: 200),
Text(
'This is some longest text that should be centered'
'together with the logo',
textAlign: [Link],
),
],
),
),
),
],
),
);
. . .
SizedBox
It’s one of the simplest but most useful Widgets
[Link] 46/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
SizedBox as ConstrainedBox
SizedBox can work in a similar fashion as ConstrainedBox
[Link](
child: Card(
child: Text('Hello World!'),
color: [Link],
),
),
. . .
SizedBox as padding
When in need of adding padding or margin, you might choose Padding or Container
widgets. But they can be more verbose and less readable than adding a Sizedbox
[Link] 47/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Column(
children: <Widget>[
Icon([Link], size: 50),
const SizedBox(height: 100),
Icon([Link], size: 50),
Icon([Link], size: 50),
],
),
[Link] 48/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
Because SizedBox has a const constructor, using const SizedBox() is really cheap**.
** One cheaper solution would be to use Opacity widget and change the opacity value
to 0.0 . The drawback of this solution is that the given widget would be only invisible,
still would occupy the space.
. . .
SafeArea
[Link] 49/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
On different platforms, there are special areas like Status Bar on Android or the Notch on
iPhone X that we might avoid drawing under.
[Link] 50/51
20/04/2020 Flutter Layout Cheat Sheet - Flutter Community - Medium
. . .
In case you have a question that is important to you and don’t want me to miss it, you can
send me a private message at twitter.
[Link] 51/51