Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,210 --> 00:00:08,150
So let's style our question text and for this, here on text, we can actually pass more than just the text
2
00:00:08,150 --> 00:00:09,530
that should be output.
3
00:00:09,530 --> 00:00:15,820
That is the positional argument of the text widget which is built into Flutter and the material.dart file,
4
00:00:16,460 --> 00:00:18,910
however this also is a couple of named arguments,
5
00:00:18,950 --> 00:00:25,880
for example the style argument. The style argument if you hover over it, takes a text style object
6
00:00:25,880 --> 00:00:33,110
and now this is not a widget but a normal object based on the TextStyle class which in turn is provided
7
00:00:33,290 --> 00:00:34,900
by material.dart.
8
00:00:35,030 --> 00:00:41,030
You simply create a new instance by instantiating text style like this, like you always instantiate classes
9
00:00:41,030 --> 00:00:41,300
right,
10
00:00:41,300 --> 00:00:48,060
you always do that by using the class names and adding parentheses, let's add a comma here and now text style
11
00:00:48,070 --> 00:00:53,220
in turn takes a couple of named arguments that allow you to configure the text style,
12
00:00:53,220 --> 00:00:56,130
for example you can set a font size.
13
00:00:56,130 --> 00:01:00,160
Now as always, you can hit control space to get a list of things you can set,
14
00:01:00,270 --> 00:01:04,720
you can for example change the color and so on but here, I'll set the font size
15
00:01:04,830 --> 00:01:11,260
and I just want to set this to let's say 28. If we do that and we save this,
16
00:01:11,380 --> 00:01:13,050
you see now that's way bigger
17
00:01:13,120 --> 00:01:15,460
and that's already better I'd say.
18
00:01:15,460 --> 00:01:18,810
It would also be nice if that were centered.
19
00:01:18,970 --> 00:01:24,190
We can easily do that, not with the help of style but with the help of another named argument on the
20
00:01:24,190 --> 00:01:27,810
text widget and that would be text align.
21
00:01:28,210 --> 00:01:33,670
Here we can set text align and this requires a text align value.
22
00:01:33,670 --> 00:01:38,590
Now here you could instantiate text align but actually, this now works a bit differently,
23
00:01:38,710 --> 00:01:42,670
there you can just use text align without parentheses and then dot
24
00:01:42,670 --> 00:01:46,930
and here you have a couple of default values, for example center.
25
00:01:46,930 --> 00:01:49,700
So what's that text align center thing here?
26
00:01:49,830 --> 00:01:53,920
It's a so-called enum which is a list of predefined values
27
00:01:53,920 --> 00:01:59,800
you could say. The idea here simply is that there are different kind of values you can assign for alignment
28
00:01:59,800 --> 00:02:02,210
like center, left, right and so on
29
00:02:02,240 --> 00:02:08,320
and these are encoded as numbers and instead of using 0, 1 and 2 which are very meaningful to computer
30
00:02:08,320 --> 00:02:12,900
of course, they're not that meaningful to you as a developer, as a human
31
00:02:12,970 --> 00:02:19,870
and therefore we have this enum thing which is a different data type in Dart, which simply assigns labels
32
00:02:19,930 --> 00:02:22,730
to these different options, to these different numbers.
33
00:02:22,750 --> 00:02:27,490
So it's always a good solution if you have like different options and you want to use a human readable
34
00:02:27,850 --> 00:02:32,520
label and behind the scenes, you only basically need a number that identifies an option.
35
00:02:32,560 --> 00:02:35,350
We'll have a look at the source code behind that in a second.
36
00:02:35,350 --> 00:02:39,720
Now how do I know such things? Well from the official documentation
37
00:02:39,790 --> 00:02:45,040
and I'll dive into that together with you later in this module because of course, it is important that
38
00:02:45,040 --> 00:02:48,640
you are also able to find out such things on your own.
39
00:02:48,640 --> 00:02:52,810
I'll come back to that later, for now let's just use text align center here,
40
00:02:52,810 --> 00:03:01,590
if we now save that, you will see that the text is not just bigger but that it also should be centered.
41
00:03:01,740 --> 00:03:03,620
Doesn't really work, right?
42
00:03:03,740 --> 00:03:10,840
The reason for that is that by default the text widget only allocates as much space as the text needs.
43
00:03:10,980 --> 00:03:17,160
We can change this however by wrapping our text here in another widget and that would be container
44
00:03:17,160 --> 00:03:24,540
widget for example. Container can be wrapped around our text here, so open and close parentheses after
45
00:03:24,540 --> 00:03:29,020
text and container takes a child named argument which now is the text,
46
00:03:29,040 --> 00:03:36,720
so that is now the widget that's wrapped into container. Container by default is pretty invisible, doesn't
47
00:03:36,720 --> 00:03:42,780
have anything that you would see on the screen, if you save that, doesn't change right, it's the same as
48
00:03:42,780 --> 00:03:51,770
before. So what we can do with container however is there we can set a width and you can set this to
49
00:03:51,770 --> 00:03:58,850
double.infinity, .infinity gives you basically a width that ensures that the container takes as
50
00:03:58,850 --> 00:04:02,030
much size, as much width as it can get,
51
00:04:02,030 --> 00:04:05,060
so by default the full width of the device
52
00:04:05,060 --> 00:04:06,890
and now we see this is centered,
53
00:04:06,920 --> 00:04:07,710
why?
54
00:04:07,790 --> 00:04:14,330
Because the container takes the full width of the screen now and the text now automatically takes the
55
00:04:14,330 --> 00:04:16,490
full width of the container.
56
00:04:16,490 --> 00:04:22,310
So now the text does not use its text as a measurement for how big it should be but the surrounding
57
00:04:22,310 --> 00:04:27,740
widget and that's just the default behavior of Flutter and you'll get into such behaviors and get a feeling
58
00:04:27,740 --> 00:04:30,980
for them as you work with Flutter and as you build apps.
59
00:04:30,980 --> 00:04:36,200
So no worries if that is a lot of new information right now, that is something which will become familiar
60
00:04:36,350 --> 00:04:38,750
the more you work with Flutter.
61
00:04:38,780 --> 00:04:44,090
So now the text takes the full width of the container, which in turn takes the full width of the device and
62
00:04:44,090 --> 00:04:47,200
therefore now we see that centering effect.
63
00:04:47,270 --> 00:04:53,930
Now let's wrap up this first little styling excursion here by also adding another named argument to
64
00:04:53,930 --> 00:05:00,690
container and that would be margin. Margin is spacing around the container
65
00:05:01,120 --> 00:05:07,520
and for that, you have to understand that the container widget is a special widget which actually has
66
00:05:07,610 --> 00:05:12,850
a bunch of settings that allow you to space things and to align things.
67
00:05:13,130 --> 00:05:15,530
The core of the container always is the child,
68
00:05:15,590 --> 00:05:22,940
in our case that's the text widget. Around that however, we can set up some padding, that is internal spacing
69
00:05:22,940 --> 00:05:24,950
inside of the container.
70
00:05:24,950 --> 00:05:31,220
Now inside means that container also may have a border which marks the border of the container
71
00:05:31,310 --> 00:05:37,070
and that border can be drawn such that you can see it, you can give it a color and so on and outside
72
00:05:37,070 --> 00:05:39,590
of the border, you have that margin thing,
73
00:05:39,620 --> 00:05:45,000
so that's the spacing around the container, between the container and neighboring elements.
74
00:05:45,110 --> 00:05:50,230
So that overall makes up the container and it's just important that you keep this in mind,
75
00:05:50,300 --> 00:05:52,580
child in the middle, padding around it
76
00:05:52,580 --> 00:05:57,230
if you have one, border around that if you have one and margin around all of that
77
00:05:57,290 --> 00:06:02,360
if you have a margin. Now here on this container, I'm not setting a border, not setting a padding but I'm
78
00:06:02,360 --> 00:06:07,180
setting a margin and the margin is of type EgeInsetsGeometry,
79
00:06:07,340 --> 00:06:13,540
actually created by calling EdgeInsets and then dot one of these options.
80
00:06:13,590 --> 00:06:17,160
Now these are basically additional constructors,
81
00:06:17,190 --> 00:06:24,670
a class can have more than one default constructor and you can call them as methods on the object.
82
00:06:24,690 --> 00:06:26,650
It's not something we have learned about yet,
83
00:06:26,670 --> 00:06:29,160
I'll dive a bit deeper into this in the next lecture,
84
00:06:29,160 --> 00:06:31,110
for now let's just take it for granted,
85
00:06:31,320 --> 00:06:37,200
these basically allow us to create different variants of one and the same object, for example with
86
00:06:37,210 --> 00:06:44,670
EdgeInsets.all, we can assign a certain value as a margin in all directions around the container. With
87
00:06:44,790 --> 00:06:52,670
only, we could target one specific direction, for example only to the top or only to the bottom. So here,
88
00:06:52,970 --> 00:06:54,250
I'll actually use all
89
00:06:54,260 --> 00:06:59,320
and now to all, you simply pass a double value, so here we could add 10
90
00:06:59,390 --> 00:07:05,360
and now this will take 10 device pixels of margin around the container, which is why we now see
91
00:07:05,360 --> 00:07:12,730
a little bit more spacing around our title. So that were obviously a lot of new things,
92
00:07:12,770 --> 00:07:21,410
container, margin, new types of constructors, static fields like here text align center but these all show
93
00:07:21,410 --> 00:07:28,550
us that now we have that logic, this a little bit more complex widget in our own widget which hides that
94
00:07:28,550 --> 00:07:31,510
complexity when we use it here in the main.dart file and
95
00:07:31,560 --> 00:07:37,730
this is now a great proof of why you want to split your app into your custom widgets because it ensures
96
00:07:37,730 --> 00:07:43,400
that no file gets too big because in the main.dart file, we would have a way longer code if we had
97
00:07:43,460 --> 00:07:49,040
all that code in there and instead, we really have different custom widgets which can focus on certain
98
00:07:49,040 --> 00:07:50,010
tasks,
99
00:07:50,030 --> 00:07:55,550
for example here the task is to output a question in a nicely styled way.
100
00:07:55,620 --> 00:07:59,390
Now with that, let's dive into special constructors in the next lecture.
11208
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.