Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,450 --> 00:00:06,810
Now in previous lessons, we learned how we can wrap our images inside a flat button, so that when we click
1
2
00:00:06,810 --> 00:00:11,850
on it we get something happening namely printing out the words 'Left
2
3
00:00:11,850 --> 00:00:14,910
button got pressed' or 'Right button got pressed'.
3
4
00:00:14,940 --> 00:00:17,040
Now that's not very interesting right?
4
5
00:00:17,040 --> 00:00:22,500
It's not a great app as it is because the user can't see that. We need to be able to update our images
5
6
00:00:22,620 --> 00:00:26,310
depending on when the user presses the button.
6
7
00:00:26,310 --> 00:00:33,640
We know that our images come from our assets which include all of the images inside this images folder.
7
8
00:00:33,660 --> 00:00:39,490
So that means I can tap into any of the dice images that I see here by simply changing that number.
8
9
00:00:39,510 --> 00:00:47,480
So I could change it to a dice four and I hit save. Hot reload happens, and my left die becomes the fourth
9
10
00:00:47,530 --> 00:00:48,990
die.
10
11
00:00:49,160 --> 00:00:56,440
Now I frequently mess up the plural and singular of dice and die, so just bear with me here.
11
12
00:00:56,600 --> 00:01:04,880
So again, we're not really interested in changing the code and then hot reloading and then changing the
12
13
00:01:04,880 --> 00:01:07,670
image. The user can't do this either,
13
14
00:01:07,670 --> 00:01:08,250
right?
14
15
00:01:08,270 --> 00:01:17,660
We need to somehow link the changing of this one number to the user pressing a button. That way it actually
15
16
00:01:17,660 --> 00:01:20,640
becomes an app with functionality.
16
17
00:01:20,750 --> 00:01:26,990
What we're going to do is we're going to create a variable up here, inside our stateless widget here.
17
18
00:01:27,530 --> 00:01:32,810
And we're going to create the variable by using the keyword var, and we're going to give our variable a
18
19
00:01:32,810 --> 00:01:38,110
name, namely leftDiceNumber.
19
20
00:01:38,450 --> 00:01:44,100
And this is going to refer to the number of the die that's going to be displayed on the left.
20
21
00:01:44,120 --> 00:01:47,760
So we're going to change that to equal one.
21
22
00:01:48,050 --> 00:01:55,070
And now, instead of having this being hardcoded, we're going to delete it and we're going to insert this
22
23
00:01:55,160 --> 00:02:00,160
variable. And to do that in Dart, we're gonna use the dollar sign.
23
24
00:02:00,470 --> 00:02:04,120
So we get to add one variable after the dollar sign.
24
25
00:02:04,250 --> 00:02:07,470
And this is going to be the leftDiceNumber.
25
26
00:02:08,630 --> 00:02:13,820
So now, when I run my app again, you'll notice there's just one problem.
26
27
00:02:14,000 --> 00:02:25,070
If I change this variable now to say five, and I go ahead and I click on hot reload or I hit save, the change
27
28
00:02:25,070 --> 00:02:31,820
doesn't actually happen here. Because remember that I said previously, when we hot reload, the thing that
28
29
00:02:31,820 --> 00:02:35,390
gets rerun is our build method here.
29
30
00:02:35,420 --> 00:02:43,670
So the block of code that's between that curly brace and this curly brace. So everything in between will
30
31
00:02:43,670 --> 00:02:48,260
get triggered again and any changes in there will be reflected here.
31
32
00:02:48,260 --> 00:02:56,390
But if our dice number is outside of our build method, then the change won't be reflected in our app.
32
33
00:02:56,420 --> 00:03:04,430
So I have to cut this and paste it into here. And then if I hit save, then this build method gets rerun.
33
34
00:03:04,550 --> 00:03:14,480
Our left dice number gets set to five, and wherever it's used gets updated. So where we create our variables
34
35
00:03:14,570 --> 00:03:22,190
and where we use them matters a huge deal. But now instead of having a hardcoded number in here that
35
36
00:03:22,190 --> 00:03:27,890
is one or two or five, we now have a variable that's determining what it is.
36
37
00:03:28,430 --> 00:03:36,800
And so if this variables equal to five, then this line becomes images/dice5.png It gets
37
38
00:03:36,980 --> 00:03:43,820
inserted in here. And this is all because we're able to use variables in Dart to achieve this.
38
39
00:03:44,300 --> 00:03:49,610
So in the next lesson, I want to talk a little bit more about variables in Dart. And especially for those
39
40
00:03:49,610 --> 00:03:54,650
you guys who are seeing variables for the first time, it'll be really important to understand how they
40
41
00:03:54,650 --> 00:03:57,380
work and especially how they work in Dart.
41
42
00:03:57,380 --> 00:04:00,470
So for all of that and more, I see on the next lesson.
4986
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.