Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,510 --> 00:00:08,760
Now it's all very well and good using a container to lay out a single widget, such as a piece of text
1
2
00:00:09,090 --> 00:00:15,970
or a image. And we can define it padding, its margin, its size, its background color etc..
2
3
00:00:16,170 --> 00:00:22,610
But what if you had lots of different things that you want to lay out relative to each other?
3
4
00:00:22,620 --> 00:00:29,010
Well, in that case, you need something that can take lots of children, and not just a single one.
4
5
00:00:29,010 --> 00:00:36,420
And the easiest way to do this is using columns and rows, so that we can lay out our widgets in a vertical
5
6
00:00:36,420 --> 00:00:42,720
direction or a horizontal direction, and determine how they should be sized.
6
7
00:00:42,780 --> 00:00:49,320
Going back to our widgets catalog and looking specifically at the layout widgets, we're gonna go down
7
8
00:00:49,320 --> 00:00:56,040
to the multi-child layout widget section. And you can see the most commonly used ones to layout multiple
8
9
00:00:56,040 --> 00:01:05,250
child widgets, are rows and columns. Rows go horizontal, columns will go vertical. Let's go ahead and create
9
10
00:01:05,340 --> 00:01:07,890
a column of our own.
10
11
00:01:08,070 --> 00:01:15,510
What if instead of only having a single container, I had lots of different containers? So I'm going to
11
12
00:01:15,510 --> 00:01:21,490
go ahead and get rid of the margin and padding, so we don't confuse ourselves, and I'm gonna name it
12
13
00:01:21,530 --> 00:01:24,350
Container 1.
13
14
00:01:24,450 --> 00:01:27,490
So here's Container 1, and it's white.
14
15
00:01:27,690 --> 00:01:34,320
And I'm gonna collapse my container. So you can see that all the code that's related to setting the properties
15
16
00:01:34,620 --> 00:01:40,830
of our container, is all enclosed in these parentheses. When I click on it, I can expand it. But I can also
16
17
00:01:40,830 --> 00:01:44,790
compress it like this. Instead of having the child of our safe area,
17
18
00:01:44,790 --> 00:01:48,450
So what's inside our safe area being just a container,
18
19
00:01:48,600 --> 00:01:55,440
I'm going to cut that and paste it down here, for a little bit later on. And instead the child is going
19
20
00:01:55,440 --> 00:02:02,190
to be a column. Now a column crucially, doesn't have a child.
20
21
00:02:02,430 --> 00:02:10,200
It has children, because we can put lots of things inside our column. And it's got this angle bracket
21
22
00:02:10,200 --> 00:02:14,330
to tell you that this is actually a list of items that we can put in here.
22
23
00:02:14,340 --> 00:02:20,850
We can put lots of widgets in between these square brackets. And what we're going to start with is that
23
24
00:02:20,850 --> 00:02:22,850
container that we moved out just now.
24
25
00:02:22,860 --> 00:02:29,060
So I'm gonna hit COMMAND + X to cut and paste it in here, and then I'm gonna collapse it down.
25
26
00:02:29,350 --> 00:02:34,920
And at this point when I hit save, you'll see that nothing much really happens on screen because we have
26
27
00:02:34,920 --> 00:02:41,400
a column that only really comes into action when you have more than one widget.
27
28
00:02:41,610 --> 00:02:49,000
Let's leave our first container and let's build another container as the second child of our column.
28
29
00:02:49,020 --> 00:02:57,860
Now this container is again going to have a width of 100 and a height of 100. And notice that
29
30
00:02:57,860 --> 00:03:07,130
when you add a comma to the end of say you know every line, and you hit save, it makes it really easy
30
31
00:03:07,160 --> 00:03:10,640
for Dart to know how to lay out your code.
31
32
00:03:10,730 --> 00:03:15,890
Let us also give our container a color so that we can actually see it.
32
33
00:03:15,950 --> 00:03:18,860
So let's change this to a blue color.
33
34
00:03:22,130 --> 00:03:30,600
And hit save. And you can see we now have two containers onscreen, and I'm going to give this a child which
34
35
00:03:30,600 --> 00:03:39,880
is a piece of text, and I'm going to tell it to write the words 'Container 2'. So we have container 1 and
35
36
00:03:39,880 --> 00:03:41,040
container 2.
36
37
00:03:41,210 --> 00:03:46,080
And I'm simply going to copy this container to create a third one.
37
38
00:03:46,750 --> 00:03:52,380
And our third one is going to be red. And it's going to be container 3.
38
39
00:03:53,230 --> 00:03:57,130
So now we have three containers all a 100 by 100.
39
40
00:03:57,130 --> 00:04:05,080
All with different colors, and it's being laid out in a vertical direction. Which is because, we have all
40
41
00:04:05,080 --> 00:04:14,650
the containers inside a column. Now our columns will automatically try and take up as much of the vertical
41
42
00:04:14,650 --> 00:04:16,590
space as possible.
42
43
00:04:16,630 --> 00:04:21,970
So if we go into Flutter Inspector and we check out what the column actually looks like, you can see
43
44
00:04:22,090 --> 00:04:23,820
it's this box right here.
44
45
00:04:23,860 --> 00:04:30,790
It's trying to take up all of the space that is available vertically. But horizontally,
45
46
00:04:30,790 --> 00:04:33,760
it's limiting itself to the size of its children.
46
47
00:04:33,760 --> 00:04:35,440
We can change this however.
47
48
00:04:35,440 --> 00:04:41,770
We can change its mainAxisSize, and main axis for a column, is of course vertical.
48
49
00:04:42,190 --> 00:04:45,460
So let's change this to the minimum size.
49
50
00:04:46,060 --> 00:04:52,990
And you can see now, when I hit save, my column actually shrinks to be the size that's only required to
50
51
00:04:52,990 --> 00:04:57,080
fit my three containers, all of its children.
51
52
00:04:57,490 --> 00:05:05,450
Currently, our column is laying out its children from top to bottom, so container 1, 2 and 3.
52
53
00:05:05,470 --> 00:05:08,520
Now what if we wanted to go the other direction instead?
53
54
00:05:08,980 --> 00:05:11,830
Well, we can change its vertical direction.
54
55
00:05:11,830 --> 00:05:18,770
We can either change it to go up, which means it goes from the bottom to the top.
55
56
00:05:18,790 --> 00:05:27,010
So here's 1, 2 and 3 . Or we can change it as down, which is the default, where it goes from top
56
57
00:05:27,010 --> 00:05:29,090
to bottom.
57
58
00:05:29,180 --> 00:05:35,840
Now what if you didn't want to change the direction of it, but instead you wanted to change the spacing
58
59
00:05:36,380 --> 00:05:38,300
between our containers.
59
60
00:05:38,300 --> 00:05:45,950
Well then you would use something called mainAxisAlignment, and by default all of our containers are
60
61
00:05:45,950 --> 00:05:48,950
aligned to the start.
61
62
00:05:48,950 --> 00:05:52,340
So it's mainAlignment.start.
62
63
00:05:52,520 --> 00:05:58,700
And this will try and place the children as close to the start of the main axis as possible.
63
64
00:05:58,700 --> 00:06:00,310
So this is a vertical axis,
64
65
00:06:00,350 --> 00:06:03,200
and this is the start of the vertical axis.
65
66
00:06:03,320 --> 00:06:06,170
However we can also change it to end.
66
67
00:06:06,170 --> 00:06:12,710
So if we had MainAxisAlignment.end then our containers will all be bunched down here, towards the
67
68
00:06:12,830 --> 00:06:16,640
end of our main axis.
68
69
00:06:16,640 --> 00:06:24,290
Now you can also move it all to the center. And you can use a variety of these to determine how you want
69
70
00:06:24,290 --> 00:06:26,640
your screen to be laid out.
70
71
00:06:26,750 --> 00:06:32,450
Now what if we didn't actually want all of our children to be bunched together like this, at the center
71
72
00:06:32,510 --> 00:06:36,990
at the start or at the end. What if we wanted them to be spread out a bit more.
72
73
00:06:37,250 --> 00:06:41,430
Well we can use something that's called 'spaceEvenly.'
73
74
00:06:41,690 --> 00:06:48,620
And this will calculate the amount of free space that the column has, and it'll space all of our children
74
75
00:06:48,740 --> 00:06:51,440
evenly throughout the column.
75
76
00:06:51,440 --> 00:06:56,330
But what if we wanted something to be at the start and something to be at the bottom, and then another
76
77
00:06:56,330 --> 00:06:57,830
one in the center?
77
78
00:06:57,830 --> 00:07:01,590
Well we would then use spaceBetween instead.
78
79
00:07:02,210 --> 00:07:10,040
Now what if I wanted to change the way that my containers are aligned on the horizontal axis?
79
80
00:07:10,070 --> 00:07:15,140
Well in this case, instead of using the main axis alignment property, I would be using something called
80
81
00:07:15,140 --> 00:07:16,840
the cross axis alignment.
81
82
00:07:16,970 --> 00:07:20,930
And this is a cross or perpendicular to the main axis.
82
83
00:07:20,930 --> 00:07:23,880
So for columns, they'll be the horizontal.
83
84
00:07:23,930 --> 00:07:32,850
Now here, I also have access to the start CrossAxisAlignment, and also end CrossAxisAlignment.
84
85
00:07:33,050 --> 00:07:38,780
But you'll notice that if I just go ahead and put this in, and I hit save to hot reload, nothing really
85
86
00:07:38,780 --> 00:07:41,410
happens on the horizontal axis.
86
87
00:07:41,540 --> 00:07:50,430
And the reason for this is that my axis alignment is actually trying to align to my children.
87
88
00:07:51,080 --> 00:08:00,440
So that means that say, if I had a child that was much wider, let's say 300 across, then all
88
89
00:08:00,440 --> 00:08:06,490
of my children will align themselves to the right edge of each other.
89
90
00:08:07,160 --> 00:08:14,150
But if they're all the same size, then they're already aligned on the end, so they won't pop over to the
90
91
00:08:14,150 --> 00:08:15,050
right side.
91
92
00:08:15,050 --> 00:08:16,430
In this case.
92
93
00:08:16,430 --> 00:08:24,920
Now if you wanted all your three containers to go over to the right, one way of doing this might be creating
93
94
00:08:24,920 --> 00:08:35,030
simply a invisible container, where you give it a width of infinity, and to give it a width of infinity,
94
95
00:08:35,060 --> 00:08:38,690
we write double.infinity
95
96
00:08:38,690 --> 00:08:43,970
And what this will do, is it will create a container down here.
96
97
00:08:44,000 --> 00:08:45,310
You can see it has no height.
97
98
00:08:45,440 --> 00:08:51,540
So it's just got a width, and it's as wide as the screen will allow it to be.
98
99
00:08:51,560 --> 00:08:58,280
So if I give it a small height so that you can see it more easily, in this case, it's just this last bit.
99
100
00:08:58,280 --> 00:08:59,210
It doesn't have a color.
100
101
00:08:59,240 --> 00:09:06,710
So it's completely invisible but it pushes all of our children to the right, because we're now all aligning
101
102
00:09:07,130 --> 00:09:11,050
to the end of our column on the horizontal axis
102
103
00:09:11,150 --> 00:09:16,670
and one of our containers is as wide as the screen allows now.
103
104
00:09:16,700 --> 00:09:22,490
What if instead of doing this, you wanted actually to stretch all your containers, so that they fill the
104
105
00:09:22,490 --> 00:09:24,110
entire width?
105
106
00:09:24,170 --> 00:09:30,950
Now you could provide a width for each of the containers, to be double.infinity
106
107
00:09:30,950 --> 00:09:33,500
So let's change the width to
107
108
00:09:39,320 --> 00:09:46,070
So now, all of my containers will stretch to the entire width that's possible. But there's a much easier
108
109
00:09:46,070 --> 00:09:53,420
way of doing this actually, by using a property that comes with our column. And instead of using Cross
109
110
00:09:53,420 --> 00:10:02,120
AxisAlignment.end, we can use CrossAxisAlignment.stretch. And this will stretch our children so that
110
111
00:10:02,120 --> 00:10:09,870
they can be the best versions of themselves, or in this case, actually stretch them across the screen.
111
112
00:10:09,890 --> 00:10:14,360
So you can see that even though I've reloaded, everything looks exactly the same.
112
113
00:10:14,360 --> 00:10:20,510
And I no longer need to provide a width property for my containers because it's being stretched by its
113
114
00:10:20,510 --> 00:10:22,900
parent, the column.
114
115
00:10:22,940 --> 00:10:28,640
Now what if you wanted a little bit of spacing between your containers. Because at the moment, they're
115
116
00:10:28,640 --> 00:10:30,250
all very much in each other's business
116
117
00:10:30,290 --> 00:10:33,710
and right next to each other with no space in between.
117
118
00:10:34,400 --> 00:10:41,150
Well if you knew exactly how much space you want to have between your containers, you can use a Sized
118
119
00:10:41,240 --> 00:10:46,480
box. And a sized box can have a height and width of course.
119
120
00:10:46,640 --> 00:10:54,380
But in the case of where it's being put inside a column, which is aligned vertically, then we only really
120
121
00:10:54,380 --> 00:11:01,580
need to provide a height. So we can give it a height of 20, and leave it width property as zero.
121
122
00:11:02,150 --> 00:11:08,840
And you can see, this goes in here between container 1 and container 2, and gives us a little bit of spacing
122
123
00:11:08,900 --> 00:11:10,730
between those two things.
123
124
00:11:11,000 --> 00:11:16,420
And it helps us when we want a little bit of space between our children. Now,
124
125
00:11:16,500 --> 00:11:22,370
everything that I've spoken about columns also apply to rows.
125
126
00:11:22,500 --> 00:11:30,560
So in fact, I can straight up just change our column to a row, and you'll see it does exactly the same
126
127
00:11:30,560 --> 00:11:33,880
thing on the horizontal axis.
127
128
00:11:33,890 --> 00:11:38,030
So in this case though, we're saying that the cross axis alignment.
128
129
00:11:38,030 --> 00:11:43,460
So for a row, the main axis of is of course horizontal but the cross axis alignment.
129
130
00:11:43,460 --> 00:11:50,030
So the vertical alignment has to be stretched so all our containers are stretched vertically.
130
131
00:11:50,030 --> 00:11:57,140
And for all the children, they each have a height which doesn't really get taken into account anymore
131
132
00:11:58,160 --> 00:12:03,720
because it's being stretched, but we can also give it a width for example.
132
133
00:12:03,740 --> 00:12:07,120
So let's say we give it a width of 30 for the first one.
133
134
00:12:07,250 --> 00:12:14,590
You can see that gets affected, because my containers are being aligned on the horizontal axis.
134
135
00:12:14,630 --> 00:12:20,330
Now you can see that my sized box disappeared, because I'm giving it a height but that doesn't really
135
136
00:12:20,330 --> 00:12:21,980
matter when it doesn't have a width.
136
137
00:12:22,460 --> 00:12:30,770
So in a row, in order to give it space, we're gonna be using a sized box with a width in order to give
137
138
00:12:30,770 --> 00:12:33,230
it a little bit of spacing.
138
139
00:12:33,230 --> 00:12:41,780
So the best way of getting to know columns and rows and containers is really to try it out for yourself.
139
140
00:12:41,780 --> 00:12:45,340
So in the next lesson, I've got a challenge for you.
140
141
00:12:45,620 --> 00:12:51,980
I've got a layout that I want you to replicate using containers, columns and rows and everything that
141
142
00:12:51,980 --> 00:12:54,140
we've learned about so far.
142
143
00:12:54,170 --> 00:13:01,100
Now while you're playing around with this, as humans, we tend to be quite visual. And it's usually easier
143
144
00:13:01,100 --> 00:13:06,880
for us to see what something will do before actually writing out the code.
144
145
00:13:06,980 --> 00:13:13,550
So I recommend pulling up this Flutter layout cheat sheet that's created by Tomek, and all of course
145
146
00:13:13,550 --> 00:13:19,610
linked to it in the course resources. And what he's done, is he's taken things such as the column and
146
147
00:13:19,610 --> 00:13:24,290
row, and he's tried to show you what it looks like before you write the code.
147
148
00:13:24,710 --> 00:13:29,030
So in this case, when you use main Alignment.start on a column,
148
149
00:13:29,030 --> 00:13:32,890
this is what it looks like. When you use main alignment don't start on a row,
149
150
00:13:32,900 --> 00:13:38,150
this is what it looks like. And you can see that he's put in all of the things that we've talked about,
150
151
00:13:38,270 --> 00:13:44,180
at least for columns and rows, and you can identify it visually before you commit it into writing the
151
152
00:13:44,180 --> 00:13:50,410
code. In order to complete the challenge, head over to the next lesson and check out the layout
152
153
00:13:50,430 --> 00:13:56,700
I want you to achieve. And then go ahead and write the code yourself, and see if you can make it work
153
154
00:13:56,760 --> 00:13:58,550
and make it look exactly the same.
154
155
00:13:58,980 --> 00:14:00,560
So, I'll see you on the next lesson.
17930
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.