Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,500 --> 00:00:00,840
All right.
1
2
00:00:00,870 --> 00:00:06,900
So now that we've completed the design for our app, we're finally ready to actually give our app some
2
3
00:00:06,900 --> 00:00:08,190
functionality.
3
4
00:00:08,400 --> 00:00:15,720
And the first thing we have to do, is we have to detect when a user actually clicks on one of the images.
4
5
00:00:15,960 --> 00:00:19,660
And the way that we're going to do that is by using a button.
5
6
00:00:19,860 --> 00:00:25,980
If you head over to the widget catalog, and go into the material components, you'll see that there's a
6
7
00:00:25,980 --> 00:00:31,890
whole bunch of buttons that we can choose from. Such as a raised button, which has a little bit of a shadow
7
8
00:00:31,890 --> 00:00:32,650
around it.
8
9
00:00:32,820 --> 00:00:39,840
The floating action button, which we've already seen before, and a flat button which reacts to touches
9
10
00:00:39,900 --> 00:00:41,850
by filling with color.
10
11
00:00:42,300 --> 00:00:47,910
And there's icon button, so a button that just has an icon in it, dropdown, popup menu, a whole bunch
11
12
00:00:47,910 --> 00:00:52,890
of other things that you could use. The one that we're going to pick is a flat button.
12
13
00:00:53,220 --> 00:00:59,760
And I'm going to put that flat button around our first dice image, and change the tree to look like this.
13
14
00:01:00,720 --> 00:01:05,580
The way that we're going to do this is we're going to select the widget that we want to wrap our flat
14
15
00:01:05,700 --> 00:01:08,340
button around, which is going to be our image,
15
16
00:01:08,340 --> 00:01:14,720
the first one here of two. And then I'm going to hold down OPTION or ALT and hit ENTER.
16
17
00:01:14,850 --> 00:01:21,090
And now, I get to wrap it with a new widget. And it's going to be a flat button
17
18
00:01:21,090 --> 00:01:22,350
in this case.
18
19
00:01:22,350 --> 00:01:29,580
Now remember that we always want to get the DART formatter to help us, and we need to add a comma after
19
20
00:01:29,580 --> 00:01:34,710
every widget so that when we hit save, it will reformat it correctly for us.
20
21
00:01:35,070 --> 00:01:41,310
And this way our code looks neat and tidy and we can see easily at a glance, that the image is a child
21
22
00:01:41,430 --> 00:01:43,800
of the flat button.
22
23
00:01:43,800 --> 00:01:50,100
Now the thing that you notice here straight away, is that the left image has suddenly become smaller.
23
24
00:01:50,480 --> 00:01:55,380
Or why is that? Or probably it's gotten a little bit more padding added to it
24
25
00:01:55,380 --> 00:01:58,110
that came along with the flat button.
25
26
00:01:58,110 --> 00:01:59,910
Now how can we find out for sure?
26
27
00:02:00,390 --> 00:02:06,450
Well when your app is running, and you click on the refresh button, then our flat button shows up.
27
28
00:02:06,450 --> 00:02:12,600
And if we click on the flat button, then down here will popup all of the properties that are currently
28
29
00:02:12,600 --> 00:02:19,260
set for that button. And you can see that it's a really long list and include things such as the text
29
30
00:02:19,260 --> 00:02:21,820
color or the color of the button.
30
31
00:02:21,900 --> 00:02:27,000
But at the moment, all of these are null because it's actually just a see through component which you
31
32
00:02:27,000 --> 00:02:27,880
can see here.
32
33
00:02:27,920 --> 00:02:32,370
but the user can't see, because it doesn't have any color. It doesn't have any background. It doesn't have
33
34
00:02:32,370 --> 00:02:37,360
any text. It only has a single child which is our image here.
34
35
00:02:37,590 --> 00:02:44,970
If we click inside anywhere inside this area with the properties and you start typing for padding, and
35
36
00:02:44,970 --> 00:02:51,300
if you hit the down button, you'll go through all the places where there's padding applied to this widget.
36
37
00:02:51,990 --> 00:03:00,750
Then you can see that the button actually by default, comes with 16 pixels of padding. So there's padding
37
38
00:03:00,750 --> 00:03:08,460
that's applied to the left, and the right, which is why the image that's inside our flat button is actually
38
39
00:03:08,460 --> 00:03:15,630
even smaller than the one that isn't inside a flat button. To fix this and get it back to the appearance
39
40
00:03:15,630 --> 00:03:16,910
that we had before,
40
41
00:03:16,980 --> 00:03:24,030
all we need to do, is just remove the padding that we applied and use the one that came from the button.
41
42
00:03:24,030 --> 00:03:28,500
So let's hit save, and you can see all we have now is the padding
42
43
00:03:28,500 --> 00:03:36,690
that's from our button. And if I refresh that, then my padding disappears. All I'm left with is my button.
43
44
00:03:36,840 --> 00:03:39,940
Let's click somewhere around here and start typing padding.
44
45
00:03:40,050 --> 00:03:47,520
And if I go down a bit, you can see all we have now is the padding that came with the button, which is
45
46
00:03:47,610 --> 00:03:50,220
16 from the left and 16 from the right.
46
47
00:03:50,220 --> 00:03:57,580
And if I just enable the debug paint, then you can see that my button is this box right here.
47
48
00:03:57,630 --> 00:04:02,400
And it's got 16 pixels from the left and 16 pixels from the right.
48
49
00:04:02,490 --> 00:04:10,410
And this particular notation has four values going from left, to top, to right, to bottom. Kind of in a circle
49
50
00:04:10,440 --> 00:04:11,490
if you will.
50
51
00:04:11,490 --> 00:04:16,410
And again this is very similar to what happens on the Web. In Chrome,
51
52
00:04:16,410 --> 00:04:21,900
we have something called Chrome developer tools, which allows us to right click on pretty much anything
52
53
00:04:21,900 --> 00:04:30,120
we see on screen, click 'inspect' and we can see all of its properties right here. Such as the color of
53
54
00:04:30,150 --> 00:04:37,680
the element, or the font family, or things like what size it is, how much padding, how much margin, how much
54
55
00:04:37,680 --> 00:04:38,960
border it has.
55
56
00:04:38,970 --> 00:04:42,000
So this is very very similar.
56
57
00:04:42,930 --> 00:04:49,260
So sometimes when you're confused why something looks a certain way, it might help to dig into these
57
58
00:04:49,260 --> 00:04:55,200
properties inside the Flutter inspector. But right now, I'm gonna pop it away and I'm going to go ahead
58
59
00:04:55,320 --> 00:05:05,900
and remove the padding from my second button and also embed this image inside a flat button.
59
60
00:05:05,970 --> 00:05:10,300
Now you can play around with which button you decide to choose.
60
61
00:05:10,320 --> 00:05:16,590
So the other popular one is the raised button. And if you just hit save, you'll notice that it comes with
61
62
00:05:16,590 --> 00:05:17,120
a color.
62
63
00:05:17,130 --> 00:05:22,120
It comes with some shadow, but it's actually not really what we want it to look like.
63
64
00:05:22,170 --> 00:05:26,460
All we want is just the image to appear as the button itself.
64
65
00:05:26,460 --> 00:05:29,750
So in this case, a flat button works perfectly.
65
66
00:05:29,760 --> 00:05:36,750
Now that we've seen how the flat button looks, it's time to actually make it do something.
66
67
00:05:36,750 --> 00:05:44,220
And if you notice right here in the margin on the right, we have these little yellow lines. And Android
67
68
00:05:44,220 --> 00:05:50,070
Studio does this whenever it tries to warn you that something might be wrong with your code.
68
69
00:05:50,070 --> 00:05:53,620
So we'll see this inside Dart analysis as well.
69
70
00:05:53,700 --> 00:05:59,370
And remember that when this icon is yellow, it's a warning which means that the code will actually run
70
71
00:05:59,430 --> 00:06:05,620
and it'll try to build it. But when it's red, then it's a lot more angry about what you did and it'll try
71
72
00:06:05,620 --> 00:06:09,490
to prevent you from crashing your app, because it's detected something.
72
73
00:06:09,550 --> 00:06:10,950
It will definitely fail.
73
74
00:06:12,010 --> 00:06:19,020
In this case, what it's telling us is something about a parameter called 'onPressed' is required, both on
74
75
00:06:19,020 --> 00:06:22,440
line 25 and on line 30.
75
76
00:06:22,450 --> 00:06:25,110
Now if you have a whole bunch of warnings and errors in here,
76
77
00:06:25,150 --> 00:06:30,580
double clicking on it to get your cursor to move to it, is a really quick and easy way of figuring out
77
78
00:06:30,610 --> 00:06:32,420
what problems you have.
78
79
00:06:32,440 --> 00:06:40,420
So what it's telling us, is that flat button has a property called onPressed, and this hasn't been defined
79
80
00:06:40,420 --> 00:06:40,640
yet.
80
81
00:06:40,660 --> 00:06:46,600
Because if you're going to use a button, you probably should tell the button what it's going to do when
81
82
00:06:46,600 --> 00:06:47,710
it gets pressed, right?
82
83
00:06:48,250 --> 00:06:51,200
And by us not telling it what it should do,
83
84
00:06:51,280 --> 00:06:56,740
it's a bit like hiring a new worker and just getting them to sit in the corner and not really know what
84
85
00:06:56,740 --> 00:06:58,710
work they have to do.
85
86
00:06:58,750 --> 00:07:01,460
It's very confusing for the button.
86
87
00:07:01,570 --> 00:07:07,780
So if we have a look at our documentation for flat button, it says here that it has a property called
87
88
00:07:07,900 --> 00:07:12,460
onPressed and this is a required property.
88
89
00:07:12,460 --> 00:07:19,750
This is the reason why we're getting those errors in Dart analysis at the moment. Because we haven't specified
89
90
00:07:19,750 --> 00:07:22,510
something that is required by the button.
90
91
00:07:22,570 --> 00:07:26,770
So it means that whenever you're going to use a flat bottom widget, you better tell it what it should
91
92
00:07:26,770 --> 00:07:29,370
do when it gets pressed.
92
93
00:07:29,370 --> 00:07:35,970
Now if you scroll down the list of properties, you'll find our onPressed property. And it says here in
93
94
00:07:35,970 --> 00:07:44,490
the documentation, that this expects something called a void callback when the button is tapped or otherwise
94
95
00:07:44,510 --> 00:07:45,200
activates it.
95
96
00:07:45,210 --> 00:07:51,720
So basically when you press the button, what it should do. And if we click on this void callback, it tells
96
97
00:07:51,720 --> 00:07:59,560
you that this is something that have no arguments and return no data.
97
98
00:07:59,580 --> 00:08:07,770
So the way that we would write this is simply a set of parentheses which are empty, and it's empty because
98
99
00:08:07,770 --> 00:08:13,460
there are no arguments or no inputs going into this void callback.
99
100
00:08:13,710 --> 00:08:21,540
And then we have a set of parentheses which is going to contain what should happen when this flat button
100
101
00:08:21,540 --> 00:08:22,190
is pressed.
101
102
00:08:22,590 --> 00:08:26,260
So this, together, is a void callback.
102
103
00:08:26,490 --> 00:08:32,610
And if you come from other programming languages, you might know this as an anonymous function.
103
104
00:08:32,610 --> 00:08:36,540
Now in the next lesson, we're going to dig into this in a lot more detail.
104
105
00:08:36,630 --> 00:08:41,740
If you're unfamiliar with functions and arguments and inputs and outputs.
105
106
00:08:41,880 --> 00:08:50,220
But for now, all you need to understand, is that inside this set of curly braces, is what will happen when
106
107
00:08:50,220 --> 00:08:52,650
the flap button gets pressed.
107
108
00:08:52,650 --> 00:08:54,940
So here we can write some code.
108
109
00:08:54,960 --> 00:08:58,210
So for example let me write a simple print statement.
109
110
00:08:58,620 --> 00:09:05,190
So this is a function that will print whatever I put inside the parentheses, and inside the parentheses
110
111
00:09:05,340 --> 00:09:10,650
are the input I'm going to give to this function to tell it what it should print.
111
112
00:09:10,650 --> 00:09:19,620
So in this case, I'm going to say 'left button got pressed' and then we'll close off the function with
112
113
00:09:19,740 --> 00:09:20,590
a semicolon.
113
114
00:09:21,300 --> 00:09:30,180
And now if I hit save, and when I press on this button right here, what we expect to happen is the flat
114
115
00:09:30,180 --> 00:09:38,460
button that I'm pressing on, will detect a touch event and it will trigger this part onPressed.
115
116
00:09:38,520 --> 00:09:44,350
And once that happens, it's going to look inside these curly braces to see what it should do.
116
117
00:09:44,370 --> 00:09:49,690
And in this case we're telling it to print this piece of text into the console.
117
118
00:09:49,830 --> 00:09:56,370
So let's open up the console and let's see what happens when I click on this button. And you can see
118
119
00:09:56,370 --> 00:10:00,510
we get a message from Flutter, which is our app over here.
119
120
00:10:00,510 --> 00:10:06,080
And it says that left button got pressed, exactly what we told it to print.
120
121
00:10:06,210 --> 00:10:13,680
So print statements are commonly used across different programming languages. And it helps you to diagnose
121
122
00:10:13,740 --> 00:10:15,060
when something goes wrong.
122
123
00:10:15,060 --> 00:10:23,130
So for example, if we didn't link this up properly and it wasn't actually triggering our onPressed, well
123
124
00:10:23,130 --> 00:10:28,440
then our print statement wouldn't be shown down here as we know there's something wrong with our flat
124
125
00:10:28,500 --> 00:10:30,120
button.
125
126
00:10:30,130 --> 00:10:32,100
Now it's your turn.
126
127
00:10:32,170 --> 00:10:39,790
Try and do the same so that when I click on the right button here, that I can get a message printed down
127
128
00:10:39,790 --> 00:10:45,940
here, saying that the right button got pressed. So pause the video and try and complete that challenge.
128
129
00:10:47,840 --> 00:10:48,460
OK.
129
130
00:10:48,520 --> 00:10:50,050
Well this should be pretty simple.
130
131
00:10:50,050 --> 00:10:56,390
We've got our flat button already added to our image, so there already is a button there.
131
132
00:10:56,530 --> 00:11:00,340
But it's missing the onPressed parameter.
132
133
00:11:00,340 --> 00:11:07,360
So let's add that. Let's add our onPressed and let's add our void callback which, if you remember, consists
133
134
00:11:07,360 --> 00:11:15,790
of an empty set of parentheses and a set of curly braces. And it's inside the curly braces where we specify
134
135
00:11:15,880 --> 00:11:18,640
what should happen when that button got pressed.
135
136
00:11:18,790 --> 00:11:28,310
And in this case we simply want to print the words 'right button got pressed' and we're going to end the
136
137
00:11:28,310 --> 00:11:31,580
function call with a semicolon.
137
138
00:11:31,580 --> 00:11:39,680
And now if I hit save and hot reload my app, now if I click on the right side, I get 'right button got
138
139
00:11:39,680 --> 00:11:41,030
pressed' printed.
139
140
00:11:41,030 --> 00:11:44,870
If I click on the left side I get 'left button got pressed'.
140
141
00:11:44,870 --> 00:11:51,980
So in this lesson, we've created a flat button widget and we've specified what should happen when it
141
142
00:11:51,980 --> 00:11:53,180
gets pressed.
142
143
00:11:53,300 --> 00:12:00,320
And we've seen that we can use print statement to print text that we write into the console, so that
143
144
00:12:00,320 --> 00:12:08,180
we as the developer, know what's going on. But the user doesn't, right? All the user sees, is what's on screen
144
145
00:12:08,180 --> 00:12:11,420
here and they're seeing that when I click on it,
145
146
00:12:11,730 --> 00:12:17,780
there's this sort of animation that makes it look like something's happening, but actually nothing's
146
147
00:12:17,780 --> 00:12:18,290
happening.
147
148
00:12:18,290 --> 00:12:20,750
So the app must be broken.
148
149
00:12:20,810 --> 00:12:27,230
Well we know that it's actually working perfectly fine, but we just need to be able to update the images
149
150
00:12:27,470 --> 00:12:34,220
when the user clicks on the buttons. So that the appearance of the app updates when the user interacts
150
151
00:12:34,220 --> 00:12:35,350
with it.
151
152
00:12:35,360 --> 00:12:38,380
Now we're going to cover that, and more in the coming lessons.
152
153
00:12:38,390 --> 00:12:40,910
So once you're ready, I'll see you on the next lesson.
17509
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.