Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,170 --> 00:00:07,390
In the last lecture, we played around with our chart and we had the strange behavior that one column
2
00:00:07,390 --> 00:00:12,760
was getting bigger than the others because it had longer text in there and that distorted our chart
3
00:00:12,820 --> 00:00:15,400
and we could fix this with the flexible widget
4
00:00:15,430 --> 00:00:19,180
but of course it's important to understand why we were able to fix it with that
5
00:00:19,180 --> 00:00:23,110
and for that, I prepared a very basic playground here.
6
00:00:23,110 --> 00:00:27,990
It's a simple app and you of course find the code for that attached, it's a normal app
7
00:00:28,030 --> 00:00:33,030
as we created with Flutter create and I only change the main.dart file to container row here
8
00:00:33,160 --> 00:00:39,760
as the body, I'm spacing my items with space around, so I'm aligning them on the main axis with space
9
00:00:39,760 --> 00:00:40,240
around
10
00:00:40,240 --> 00:00:41,800
and then I have three containers in there,
11
00:00:41,950 --> 00:00:46,960
each container has a height but no width and a different color on each container and then we have some text
12
00:00:46,960 --> 00:00:47,280
in there and
13
00:00:47,290 --> 00:00:53,320
the first container has a bigger text which is why that box is bigger than the other boxes.
14
00:00:53,320 --> 00:00:56,570
This is a comparable setup to what we had on the chart right,
15
00:00:56,590 --> 00:01:01,390
we have three different elements in this case here and one of them is getting bigger than the others.
16
00:01:01,750 --> 00:01:08,860
Now first of all, it's important to realize that inside of a column or a row, every item is just as big
17
00:01:08,890 --> 00:01:09,730
as it needs to be
18
00:01:09,730 --> 00:01:15,250
or as you tell it to be, for example on a container we could also set a width of 300
19
00:01:15,280 --> 00:01:17,980
and now we would have a way bigger container over there
20
00:01:17,980 --> 00:01:22,240
that even leads to that row not being wide enough to fit everything in there.
21
00:01:22,240 --> 00:01:24,700
So that's the first important takeaway.
22
00:01:24,730 --> 00:01:28,780
If you don't assign any widths and so on, then the items are as big as they need to be.
23
00:01:28,810 --> 00:01:36,430
Now sometimes however, you want to tell an item to take up more space in that row or column than it would
24
00:01:36,430 --> 00:01:37,450
normally take
25
00:01:37,660 --> 00:01:42,700
and you can do that by wrapping it with a widget. Let's say we will wrap the second item,
26
00:01:42,700 --> 00:01:47,950
the second container here with a new widget and that is the flexible widget which we used in the last
27
00:01:48,070 --> 00:01:49,120
lecture.
28
00:01:49,120 --> 00:01:52,860
Now there, you learned that you can set a fit property and
29
00:01:52,890 --> 00:01:58,710
now what does fit do or the fit argument do? Fit takes a Flexfit configuration
30
00:01:58,720 --> 00:02:02,650
and that's an enum with basically two values, loose and tight.
31
00:02:02,650 --> 00:02:06,290
The default is loose, so setting loose makes no difference here,
32
00:02:06,310 --> 00:02:07,780
what does loose mean?
33
00:02:07,780 --> 00:02:15,280
It means that the child of that flexible item basically should keep its size and use that size in the
34
00:02:15,280 --> 00:02:17,140
surrounding row or column.
35
00:02:17,140 --> 00:02:22,280
So as I said, it's the default and you rarely need to set loose explicitly,
36
00:02:22,450 --> 00:02:27,030
you would typically only set this if you kind of derive the value here dynamically and you also
37
00:02:27,040 --> 00:02:30,760
sometimes switch it to its alternative which would be tight.
38
00:02:30,850 --> 00:02:37,190
Now what does tight do? If I wrap my item to here with flexible fit set to Flexfit.tight,
39
00:02:37,250 --> 00:02:43,230
then you see all of a sudden this blue container takes up all the remaining space here in our row.
40
00:02:43,270 --> 00:02:49,510
Please note that it only takes as much space as fits in there without squeezing any items off the screen
41
00:02:49,510 --> 00:02:54,620
which is why we're not getting this yellow black warning marker here.
42
00:02:54,700 --> 00:02:59,860
So now blue essentially knows okay, I should take all the space I can get
43
00:02:59,860 --> 00:03:04,030
and indeed, if you have a look at the description of tight, then this is forced to fill the available
44
00:03:04,030 --> 00:03:09,250
space. Loose doesn't force the child to fill the available space and therefore, it will fill the space
45
00:03:09,250 --> 00:03:13,170
it needs to fit its content in and which in this case is only this text here
46
00:03:13,270 --> 00:03:16,890
or if we had set a width on the container, it would be the width we set here
47
00:03:17,110 --> 00:03:22,920
but now even when we set a width of let's say 100 here, with Flexfit.tight,
48
00:03:22,930 --> 00:03:28,760
this is basically ignored and it is forced to take all the available remaining space here.
49
00:03:28,780 --> 00:03:31,240
Now you can also do other cool things.
50
00:03:31,240 --> 00:03:37,570
Let's say we also have the third container wrapped with that. So I wrapped this with a widget too, give it
51
00:03:37,570 --> 00:03:45,430
a flexible widget around it and set fit to Flexfit.tight, so to the exact same configuration as
52
00:03:45,430 --> 00:03:46,910
my item too.
53
00:03:46,990 --> 00:03:51,830
Now you see, these two items here split the available space
54
00:03:52,030 --> 00:03:58,180
and now if I remove that extra text on the red element, on the first element, you see that this now gets
55
00:03:58,180 --> 00:04:04,060
squeezed to only have the bare minimum of space it needs to fit its text in there, which would be the
56
00:04:04,060 --> 00:04:10,780
default space it takes because we have no flexible widget around the first container with Flexfit.tight,
57
00:04:10,870 --> 00:04:16,720
we have no such set up there and therefore, this only takes the space it needs. The other two widgets,
58
00:04:16,740 --> 00:04:22,780
the other two containers however are configured to fill the available remaining space and that means
59
00:04:22,810 --> 00:04:27,850
right now since there are two containers which each want to take the available remaining space, they
60
00:04:27,850 --> 00:04:30,880
split the remaining space amongst them.
61
00:04:30,880 --> 00:04:36,460
Now sometimes, you want to have that behavior of taking the available space but you also know yes,
62
00:04:36,490 --> 00:04:42,340
both should take the available space but the blue container should get twice as much of that available
63
00:04:42,340 --> 00:04:44,860
space as the orange one
64
00:04:44,980 --> 00:04:49,990
and for that you can add another argument here on the blue container then and that's the flex argument
65
00:04:50,080 --> 00:04:56,020
which I only explained in theory in the last lecture. Flex by default is set to one on every element,
66
00:04:56,020 --> 00:04:59,170
so setting this to one won't make a difference.
67
00:04:59,170 --> 00:05:04,890
However, watch what happens if I set flex to two here. If I do set this to two,
68
00:05:05,030 --> 00:05:07,240
now you see the blue container grew
69
00:05:07,400 --> 00:05:14,360
and now if that here, the blue and orange area together is our remaining free space after deducting the
70
00:05:14,360 --> 00:05:20,840
red container from the full row, then you see that blue is now twice as big as orange because we gave
71
00:05:20,840 --> 00:05:22,550
it a flex value of two.
72
00:05:22,700 --> 00:05:26,230
Now important, two does not mean twice as big as the rest,
73
00:05:26,330 --> 00:05:29,190
instead this works a bit like flexbox for the web
74
00:05:29,240 --> 00:05:33,290
if you know that from CSS, if not that's no problem.
75
00:05:33,290 --> 00:05:38,780
Flutter goes ahead and basically adds the flex values of all items in the row,
76
00:05:38,810 --> 00:05:44,000
which means the first item here has no flex value because it's not wrapped and flexible
77
00:05:44,000 --> 00:05:48,540
but then we have a flex value of one here which is the default for our third container,
78
00:05:48,590 --> 00:05:53,650
so we have flex two plus flex one which means we have a total sum of three.
79
00:05:53,900 --> 00:06:00,590
Then Flutter knows that since this here has a flexible value of two, it wants to take two of three available
80
00:06:00,590 --> 00:06:01,350
segments
81
00:06:01,460 --> 00:06:05,560
and this one has the default value of one, it takes one of the three available segments.
82
00:06:05,600 --> 00:06:08,410
So this takes one-third of the available size,
83
00:06:08,450 --> 00:06:10,040
this takes two-thirds.
84
00:06:10,040 --> 00:06:16,400
If we for example added Flex 2 on the last container as well, they would split the size equally again.
85
00:06:16,400 --> 00:06:16,910
Why?
86
00:06:17,180 --> 00:06:23,660
Because we have a total flex value of four, two plus two and Flutter now finds out that the middle container
87
00:06:23,840 --> 00:06:28,900
wants to take two segments of these four available segments to split the available space
88
00:06:29,030 --> 00:06:35,210
and the last container wants to take the other two available segments, so therefore now, they're evenly split.
89
00:06:35,240 --> 00:06:38,870
So these flex values have to be seen in relation to each other,
90
00:06:38,930 --> 00:06:46,390
if we have 5 and 2 here, this means this is 5 to 2 in terms of how much space it takes.
91
00:06:46,580 --> 00:06:52,490
We have a total of seven segments into which the available remaining space is split up and this container
92
00:06:52,490 --> 00:06:56,700
here takes five of these seven segments, this one two of these segments,
93
00:06:56,720 --> 00:07:01,130
so this is two and a half times as big as the last container.
94
00:07:01,130 --> 00:07:06,020
This is how you can play around with that and that's of course super useful for sizing your elements
95
00:07:06,350 --> 00:07:08,590
in a row or column.
96
00:07:08,600 --> 00:07:11,200
Now back to the fit here now,
97
00:07:11,240 --> 00:07:19,550
if I change the item three to a Flexfit of loose, you now see the container itself now only takes as much
98
00:07:19,550 --> 00:07:21,650
space as it needs for its child
99
00:07:21,920 --> 00:07:28,680
and yet we see that there now is some empty space, some whitespace. The reason for that is that the
100
00:07:28,680 --> 00:07:32,050
flex values here are actually still taken into account.
101
00:07:32,120 --> 00:07:38,000
If I set the last item here back to flex 1, you'll see now there is less available free space and
102
00:07:38,150 --> 00:07:44,270
the reason for that is that we still have Flexfit.tight on the blue container, which means take all
103
00:07:44,270 --> 00:07:52,250
the space you can get but we at the same time tell it but you only get five-sixths because 5 plus 1, five-sixths of the
104
00:07:52,250 --> 00:07:53,540
available space.
105
00:07:53,540 --> 00:08:00,710
So even if the last item doesn't fully occupy the remaining available space, so the one-sixth that it could
106
00:08:00,710 --> 00:08:07,340
take, even if that's not the case, this middle container is still configured to only get five-sixths of the
107
00:08:07,340 --> 00:08:13,470
available space and that means that one-sixth, which the last item could take if it would be set to tight,
108
00:08:13,610 --> 00:08:20,660
that is simply left open which is why we have whitespace all over our row because that whitespace together
109
00:08:20,750 --> 00:08:27,410
would add up to the size the last box could take if it would be set to Flexfit.tight which it isn't,
110
00:08:27,410 --> 00:08:33,320
therefore it only takes the space it needs for its content and the space it could take is left empty
111
00:08:33,800 --> 00:08:39,680
and because we have our alignment set to space around, that empty available space which is not taken
112
00:08:40,070 --> 00:08:45,820
is split across the entire row and not just somewhere here at the end.
113
00:08:46,160 --> 00:08:50,290
So I hope this makes a bit of sense. As always, playing around with that,
114
00:08:50,300 --> 00:08:56,150
building a demo app like this and simply trying out different configurations is the best way to dive
115
00:08:56,150 --> 00:09:01,880
into this and to really master it and get a feeling for this. That's way better than learning all these
116
00:09:01,880 --> 00:09:07,460
different rules and combinations by heart, you won't be able to remember all of that and also there is
117
00:09:07,460 --> 00:09:08,930
no value in doing so,
118
00:09:08,990 --> 00:09:13,860
instead build a simple application like this and play around with these settings to get a feeling for
119
00:09:13,860 --> 00:09:19,600
how Flexfit.tight and Flexfit.loose and the different flex values work together.
120
00:09:19,670 --> 00:09:24,410
One last word, if you have flexible with fit set to Flexfit.tight,
121
00:09:24,530 --> 00:09:28,320
there also is another widget which you could use which is a replacement for it,
122
00:09:28,340 --> 00:09:31,700
there is the expanded widget in Flutter.
123
00:09:31,860 --> 00:09:39,120
Expanded is simply flexible with Flexfit.tight and therefore, expanded has no fit argument,
124
00:09:39,140 --> 00:09:40,760
you can't set it here,
125
00:09:40,760 --> 00:09:46,790
instead expanded only knows the flex configuration here which we used on flexible. So expanded is a perfect
126
00:09:46,790 --> 00:09:52,610
replacement for flexible with fit set to Flexfit.tight and therefore if I save this, you see no difference
127
00:09:52,610 --> 00:09:54,960
on the screen because it is the exact same.
128
00:09:55,060 --> 00:10:00,860
So if you need Flexfit.tight, instead of using flexible with that configuration, you could simply use
129
00:10:00,940 --> 00:10:03,090
expanded instead
130
00:10:03,390 --> 00:10:10,470
and that's it for now with rows and columns and flex and flexible and how to size the items in there.
131
00:10:10,660 --> 00:10:11,280
Again,
132
00:10:11,310 --> 00:10:16,440
definitely fire up your own demo app and play around with these values to get a better feeling for how
133
00:10:16,440 --> 00:10:20,790
this all behaves and how you can distribute available space in your rows and columns.
15100
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.