Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,780 --> 00:00:06,390
In the last section, we started the refactoring process to turn our stateless app widget into a stateful
2
00:00:06,390 --> 00:00:08,970
widget instead is a quick reminder.
3
00:00:08,970 --> 00:00:13,890
We're going through this entire process right now because our app widget is going to have a counter
4
00:00:13,890 --> 00:00:15,480
variable associated with it.
5
00:00:16,079 --> 00:00:19,110
That counter variable is going to change over time.
6
00:00:19,380 --> 00:00:24,650
And any time it changes, we want to update the representation of that widget on the screen of our device.
7
00:00:25,080 --> 00:00:29,730
So because we have data that's going to change over time and updating that data should be reflected
8
00:00:29,730 --> 00:00:33,300
on the screen of our device, we have to use a stateful widget.
9
00:00:34,240 --> 00:00:39,400
And we just accomplished step number one of that refactor, we're now going to move on to step number
10
00:00:39,400 --> 00:00:43,870
two, where we're going to add a create state method to the widget class.
11
00:00:45,170 --> 00:00:50,390
Whenever we call create state, it's going to return an instance of the Wichita State to class.
12
00:00:51,860 --> 00:00:56,450
So here is the widget class, this is the widget state class.
13
00:00:57,340 --> 00:01:00,610
On the widget class, I'm going to add a method called to create state.
14
00:01:02,380 --> 00:01:09,130
And from this thing, any time someone calls this, I will return an instance of our Wichita State or
15
00:01:09,130 --> 00:01:11,890
we are referred referring to it as the state class.
16
00:01:13,360 --> 00:01:15,550
All right, so that's it for step number two.
17
00:01:15,940 --> 00:01:20,740
Now, again, I know these steps are pretty confusing and they probably seem a little bit arbitrary.
18
00:01:20,980 --> 00:01:25,810
Once we get through all these steps, we will have a discussion on why we have to do all these crazy
19
00:01:25,810 --> 00:01:26,470
things in here.
20
00:01:26,620 --> 00:01:29,410
Let's just keep going for right now and get our code on the screen.
21
00:01:30,600 --> 00:01:36,480
So the next thing we have to do is add a build method to the Wichita State class, so this is the widget
22
00:01:36,500 --> 00:01:37,520
state class right here.
23
00:01:37,860 --> 00:01:43,470
And as you can see, we already have our build method on here are build method is returning all the
24
00:01:43,470 --> 00:01:46,830
widgets that our widget is going to show on the screen.
25
00:01:47,910 --> 00:01:53,040
So no work required for step number three right here, I just included it on this list to be complete.
26
00:01:54,720 --> 00:02:00,120
On to step number four, so now we're going to add some instance variables to the Wichita State class
27
00:02:00,480 --> 00:02:04,200
and these instant's variables are going to represent the data that we're going to have that's going
28
00:02:04,200 --> 00:02:05,340
to change over time.
29
00:02:06,560 --> 00:02:14,270
So on my Wichita State class right here, I'm going to add on an instance variable that I'll call counter.
30
00:02:15,250 --> 00:02:18,830
I'll make it an integer and it's going to start off as the number zero.
31
00:02:19,630 --> 00:02:22,690
So this is the instance variable that's going to change over time.
32
00:02:23,320 --> 00:02:28,150
Every time a user presses that blue button on the bottom right hand side, we're going to attempt to
33
00:02:28,150 --> 00:02:31,210
increment the value of this counter variable by one.
34
00:02:31,750 --> 00:02:38,500
And every time a user presses the button, we're also going to try to reflect the new counter value
35
00:02:38,500 --> 00:02:39,910
on the screen of our device.
36
00:02:41,710 --> 00:02:46,510
All right, so that's duck number four and finally on to step number five.
37
00:02:47,730 --> 00:02:53,330
So step number five right here is going to kind of have two steps associated with it on its own, so
38
00:02:53,360 --> 00:02:57,920
the first thing we need to do is make sure that we are changing that Wichita State's data.
39
00:02:58,080 --> 00:03:00,840
So that would be the instance variable that we just created.
40
00:03:01,260 --> 00:03:02,420
So let's take care of that first.
41
00:03:02,460 --> 00:03:06,000
Let's think about where we need to increment that counter variable.
42
00:03:07,280 --> 00:03:12,740
Well, we want to make sure any time someone presses the floating action button right here, we increment
43
00:03:12,770 --> 00:03:13,760
the counter variable.
44
00:03:14,540 --> 00:03:21,020
If you recall, when we created the floating action button, we created this on prest property and assigned
45
00:03:21,020 --> 00:03:21,830
a function to it.
46
00:03:23,300 --> 00:03:29,000
We then saw that any time user press this button, this function would be invoked, and so it seems
47
00:03:29,000 --> 00:03:34,550
to me like this would be the perfect place to take that counter variable in, increment it by one.
48
00:03:35,300 --> 00:03:41,990
So inside of the unpressed function I'll add on counter plus equals one like so.
49
00:03:43,410 --> 00:03:48,600
It's now every time we press the button, boom, counter incremented, so we now have some data that
50
00:03:48,600 --> 00:03:51,570
is changing over the life cycle of our application.
51
00:03:52,940 --> 00:03:57,910
Now on to step number two of this step, number five, I guess part two of step number five.
52
00:03:58,400 --> 00:04:00,240
So now we've changed our class data.
53
00:04:00,620 --> 00:04:03,380
We now need to call the set state method.
54
00:04:04,280 --> 00:04:10,900
So the set state method is provided to our state class when we extend the state based class.
55
00:04:11,720 --> 00:04:17,899
So there is a method inside of the state thing right here called Set State, and you and I are required
56
00:04:17,899 --> 00:04:20,480
to call it any time our data changes.
57
00:04:20,630 --> 00:04:23,960
And we want to get this component to update on the screen of our device.
58
00:04:25,660 --> 00:04:30,580
So right underneath the counter increment, I'm going to call set state.
59
00:04:31,950 --> 00:04:37,500
Like, so now you'll notice that I get the red squiggly right here when we call that state, we are
60
00:04:37,530 --> 00:04:40,320
actually required to pass in a function.
61
00:04:40,440 --> 00:04:41,670
That's what this means right here.
62
00:04:42,060 --> 00:04:44,970
So we have to pass a function into set state.
63
00:04:45,390 --> 00:04:50,850
And inside of that function is where you and I are supposed to do our data modification.
64
00:04:51,570 --> 00:04:55,590
So right now, we just put down some code to increment counter outside of set state.
65
00:04:55,980 --> 00:04:57,670
This is not 100 percent correct.
66
00:04:57,720 --> 00:05:03,230
We're supposed to modify our instance variable inside of the function that we pass to set state.
67
00:05:03,600 --> 00:05:05,250
So let's do that refactor right now.
68
00:05:05,760 --> 00:05:07,230
I'm going to add in a function right here.
69
00:05:08,890 --> 00:05:09,400
Come on.
70
00:05:09,460 --> 00:05:10,870
There we go, one up, one more.
71
00:05:10,930 --> 00:05:11,320
There we go.
72
00:05:12,700 --> 00:05:19,930
And I'm going to make sure that I mutate or change my instance variable inside of set state like so.
73
00:05:22,050 --> 00:05:23,580
OK, so that's it.
74
00:05:24,270 --> 00:05:32,010
That is the entire refactor, we now have a stateful widget called App that depends upon the widget
75
00:05:32,010 --> 00:05:33,480
state of App State.
76
00:05:34,850 --> 00:05:39,530
Any time a user presses on our floating action button right here, we're going to increment the value
77
00:05:39,530 --> 00:05:40,910
of the counter variable.
78
00:05:41,850 --> 00:05:47,790
Now, that's it for the refactored in terms of turning a state list widget into a stateful widget,
79
00:05:48,390 --> 00:05:52,290
but to actually get our application working the way we expect, there's one last little thing we have
80
00:05:52,290 --> 00:05:52,680
to do.
81
00:05:53,190 --> 00:05:58,860
We have to make sure that the counter actually gets displayed somewhere within our widget.
82
00:05:59,400 --> 00:06:04,380
So right now we are yep, we're incrementing the value of counter, but we never actually show it on
83
00:06:04,380 --> 00:06:05,490
the screen of our device.
84
00:06:06,480 --> 00:06:11,490
So to get it to show up on the screen, we're going to add one additional property to our scaffold,
85
00:06:11,490 --> 00:06:16,200
which is right here, and we're going to make sure that that additional property gets our counter to
86
00:06:16,200 --> 00:06:17,250
show up on the device.
87
00:06:18,190 --> 00:06:22,360
Let's take a quick glance at the scaffold documentation to see you get a good idea of where this is
88
00:06:22,360 --> 00:06:22,990
going to come from.
89
00:06:24,280 --> 00:06:25,870
So back at my.
90
00:06:27,390 --> 00:06:28,890
Widget documentation.
91
00:06:31,780 --> 00:06:32,950
Let me get this up here.
92
00:06:32,980 --> 00:06:36,580
I'm going to do a search on the top right hand side for scaffold again.
93
00:06:42,580 --> 00:06:47,590
And then down for the constructor of scaffolded, you'll notice that there's the body property right
94
00:06:47,590 --> 00:06:47,910
here.
95
00:06:48,400 --> 00:06:54,730
So if we pass a widget to the body property of Scaffold, the scaffold will show that on the body of
96
00:06:54,730 --> 00:06:55,150
the screen.
97
00:06:55,160 --> 00:06:59,500
So basically that counter variable will show up right here, which is exactly what we want.
98
00:07:01,540 --> 00:07:07,450
Back over inside of my scaffolded widget, I'll add on the body property and I'll have this be some
99
00:07:07,450 --> 00:07:11,920
text that's going to interpolate the counter variable in like.
100
00:07:11,920 --> 00:07:16,160
So the text widget right here always expects to be called with a string.
101
00:07:16,180 --> 00:07:20,860
So that's why we are taking the counter variable and shoving it into the string counter itself as an
102
00:07:20,860 --> 00:07:21,400
integer.
103
00:07:21,580 --> 00:07:25,020
And the text widget does not want to see an integer, it wants to see a string.
104
00:07:25,180 --> 00:07:29,230
So we're just doing a quick kind of conversion right here of that integer into a string.
105
00:07:30,350 --> 00:07:37,400
So I'll now save this, we'll flip back over to our terminal, I'll do a full reload with shifta.
106
00:07:38,980 --> 00:07:40,780
And it will test this out on our device.
107
00:07:44,970 --> 00:07:48,340
All right, so it's going to take I don't know, it's always takes me like there we go.
108
00:07:48,360 --> 00:07:48,900
Ten seconds.
109
00:07:48,900 --> 00:07:49,470
That's pretty quick.
110
00:07:49,650 --> 00:07:49,950
All right.
111
00:07:49,950 --> 00:07:55,140
So it's really, really small, but you might be able to see there is, in fact, a zero right there.
112
00:07:55,470 --> 00:07:57,150
So now I'm going to press my button.
113
00:07:57,150 --> 00:08:00,720
And when I do so, you'll notice at that counter is incrementing successfully.
114
00:08:01,060 --> 00:08:03,620
So it's going up to 10, 11, 12, 13, 14, 15.
115
00:08:03,840 --> 00:08:04,730
You get the idea.
116
00:08:05,370 --> 00:08:10,500
OK, so it looks like we definitely have the ability to create some data that's going to change over
117
00:08:10,500 --> 00:08:12,030
time inside of our application.
118
00:08:12,420 --> 00:08:16,020
But it absolutely involved a lot of very strange code.
119
00:08:16,650 --> 00:08:18,110
So let's take a quick pause right here.
120
00:08:18,480 --> 00:08:23,460
We're going to come back to the next section and we're going to talk about one little bit of weird syntax,
121
00:08:23,670 --> 00:08:28,860
specifically this state, less than greater than app thing right here.
122
00:08:29,430 --> 00:08:35,100
And we'll do a quick overview on why we had to write all this complicated code to make a stateful widget.
123
00:08:35,580 --> 00:08:37,460
So quick break and we'll see you in just a minute.
12300
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.