Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,440 --> 00:00:00,760
All right.
1
2
00:00:00,780 --> 00:00:03,430
So how did you get on with that challenge?
2
3
00:00:03,450 --> 00:00:08,160
This should be quite simple because we've already done it once for the left dice. But it's important
3
4
00:00:08,160 --> 00:00:13,230
that you understand how it all links together, which is why I wanted you to complete the challenge.
4
5
00:00:13,230 --> 00:00:19,320
The first thing that we need to do is we've got a variable that's tracking the value for our leftDice
5
6
00:00:19,320 --> 00:00:26,910
Number. And it's updating it when the button is pressed. So that it can display a new image in that image
6
7
00:00:26,910 --> 00:00:27,770
widget.
7
8
00:00:27,780 --> 00:00:33,300
Now in order to do the same for the right dice, we're going to need to create a new variable. And we'll
8
9
00:00:33,300 --> 00:00:34,990
just call this rightDice
9
10
00:00:35,010 --> 00:00:38,730
Number, because not feeling too imaginative today.
10
11
00:00:38,730 --> 00:00:45,810
Now once we've got our variable, then we can go about changing it to a random number, when the flap button
11
12
00:00:45,930 --> 00:00:49,140
is pressed. Inside our onPressed,
12
13
00:00:49,140 --> 00:00:54,750
the first thing we're gonna do is we're going to say that our rightDiceNumber is going to equal to
13
14
00:00:54,870 --> 00:00:59,700
a number that's generated from a random number generator.
14
15
00:00:59,940 --> 00:01:04,180
And the specific type of number we want to generate is an integer,
15
16
00:01:04,260 --> 00:01:09,610
so a whole number. And we're going to set the max to six.
16
17
00:01:09,630 --> 00:01:16,050
So we generate random numbers between 0 and 5. And then we'll just add 1 to any of those numbers to bring
17
18
00:01:16,050 --> 00:01:22,470
it up to 1 - 6, which matches with the dice numbers that we have in our images.
18
19
00:01:22,470 --> 00:01:28,470
The next thing we're going to do is we're going to update the place where we use that rightDiceNumber.
19
20
00:01:28,920 --> 00:01:35,820
Instead of pulling up the images/dice1.png, which is pretty much hardcoded, it's always going
20
21
00:01:35,820 --> 00:01:41,610
to be the first image shown. We're going to delete that one, and instead we're going to use a string interpolation
21
22
00:01:41,940 --> 00:01:49,740
to insert the rightDiceNumber inside there. And it's going to be inserted using that little dollar
22
23
00:01:49,740 --> 00:01:52,210
sign there that we saw previously.
23
24
00:01:52,230 --> 00:02:02,130
Now all we have to do is to say that when the rightDiceNumber changes, we should update all the places
24
25
00:02:02,430 --> 00:02:03,720
where it's used.
25
26
00:02:04,170 --> 00:02:09,070
So in order to do that, we have to call that method called set state.
26
27
00:02:09,070 --> 00:02:17,700
And this simply says that this change in the rightDiceNumber's value is going to need to trigger a
27
28
00:02:17,700 --> 00:02:24,620
rebuild of our stateful widget, so that the part where it's used can update on the screen.
28
29
00:02:24,720 --> 00:02:30,390
Let's hit save and for those changes to go through to our app, and let's test it by... we already know the
29
30
00:02:30,390 --> 00:02:31,800
left side works.
30
31
00:02:31,860 --> 00:02:38,670
And now if we click on the right side, it also works. And it gives us all the numbers that we could possibly
31
32
00:02:38,670 --> 00:02:38,930
want.
32
33
00:02:39,060 --> 00:02:43,790
And we now have two separate dice that we can roll on our dice app.
33
34
00:02:43,860 --> 00:02:51,880
Now what if we wanted both dice to change when one of the buttons is pressed?
34
35
00:02:51,930 --> 00:02:57,840
So what if I wanted both of these buttons to update when I click on any one of those buttons?
35
36
00:02:57,870 --> 00:02:59,180
Here is your challenge.
36
37
00:02:59,220 --> 00:03:06,420
Try and get this behavior in your app. So pause the video and try to complete this challenge.
37
38
00:03:06,420 --> 00:03:09,180
And I'll share with you the solution in the next lesson.
4315
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.