Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,790 --> 00:00:06,910
Now we can see that in the final version of our app when we click on either of these icon cards, they
1
2
00:00:06,910 --> 00:00:14,680
actually change color to show that they've been interacted with and they've been selected. But currently
2
3
00:00:14,890 --> 00:00:21,490
our cards don't really do anything when we click on it. And we can see that our cards or what it's based on
3
4
00:00:21,520 --> 00:00:24,340
be at the iconContent or the reusableCard,
4
5
00:00:24,340 --> 00:00:29,980
none of them actually can take any user input. They don't have an onPressed or an onTap or anything
5
6
00:00:29,980 --> 00:00:30,940
of the sort.
6
7
00:00:30,940 --> 00:00:38,110
So let's fix this in this lesson and let's make our icon cards interactive. Now one way of doing this
7
8
00:00:38,200 --> 00:00:45,850
is we could wrap our reusableCard in a flat button. So we could hold down OPTION or ALT and hit ENTER.
8
9
00:00:45,940 --> 00:00:50,940
Then we could wrap with the new widget and the widget would be a flat button.
9
10
00:00:51,160 --> 00:00:58,210
And this of course will give us access to that crucial onPressed which allows us to tap into the moment
10
11
00:00:58,480 --> 00:01:04,870
when a user actually presses on either of these reusable cards, and we'll be able to change the color
11
12
00:01:05,170 --> 00:01:06,100
that way.
12
13
00:01:06,100 --> 00:01:12,820
Now the problem with flat button is if I hit save right now, you'll be at to see that it actually affects
13
14
00:01:12,850 --> 00:01:15,500
the styling of our card.
14
15
00:01:15,550 --> 00:01:20,110
It's trying to change the margin and various other styles of its children.
15
16
00:01:20,170 --> 00:01:22,620
So it's quite an opinionated widget.
16
17
00:01:22,870 --> 00:01:30,830
And the use case for a flat button is you want to be a to implement the style of a material flat button,
17
18
00:01:30,970 --> 00:01:35,360
and that includes various colors various themes and its appearance.
18
19
00:01:35,470 --> 00:01:43,180
But in our case all that we actually need is just the ability to detect a touch on our already pretty
19
20
00:01:43,180 --> 00:01:46,380
much well styled reusable card.
20
21
00:01:46,570 --> 00:01:55,660
So instead of a flat button, we can use something called a gesture detector. A gesture detector does pretty
21
22
00:01:55,660 --> 00:01:57,070
much what it says on the tin,
22
23
00:01:57,070 --> 00:01:57,340
right?
23
24
00:01:57,340 --> 00:02:03,610
It detects gestures and the gestures it can detect are things like onTap, which is the same as onPressed,
24
25
00:02:04,270 --> 00:02:06,860
but it can also detect a whole lot more.
25
26
00:02:06,940 --> 00:02:14,590
So for example it can detect a long press or drag or force press and a whole lot more.
26
27
00:02:15,100 --> 00:02:18,760
All that this widget is concerned with is detecting gestures.
27
28
00:02:18,760 --> 00:02:26,120
It doesn't try to force any sort of styling or appearance or animations onto the children widgets.
28
29
00:02:26,140 --> 00:02:31,940
So this is a very pure way of detecting when a user interacts with a widget of ours.
29
30
00:02:31,960 --> 00:02:39,370
So by having our reusable card as a child of our gesture detector, we can now start tapping into the
30
31
00:02:39,370 --> 00:02:41,440
gestures that it can detect.
31
32
00:02:41,440 --> 00:02:46,930
So for example, onTap is your classic single tap on the screen.
32
33
00:02:46,930 --> 00:02:53,440
So it's the same as the onPressed of a flat button. Or we can detect a vertical drag, horizontal drag,
33
34
00:02:53,440 --> 00:02:54,950
double tap, force press,
34
35
00:02:55,030 --> 00:02:55,900
whole lot more.
35
36
00:02:55,900 --> 00:03:00,290
But all we need right now is the onTap gesture detector.
36
37
00:03:00,400 --> 00:03:07,510
So I'm going to add our classic anonymous callback and inside here, I'm just going to print that the
37
38
00:03:07,780 --> 00:03:18,550
male card was pressed. And now if we hit save and go into our app making sure that our run tab is showing,
38
39
00:03:18,550 --> 00:03:24,160
and if I click on that male button, you can see that we get 'Male card was pressed' every single time I
39
40
00:03:24,160 --> 00:03:24,820
press on it.
40
41
00:03:24,820 --> 00:03:27,430
So it works just like a flat button
41
42
00:03:27,430 --> 00:03:34,300
and it's really easy to implement. Now that we've got our gesture detector, we're able to detect when
42
43
00:03:34,300 --> 00:03:38,890
a user actually clicks on our cards, be it male or female.
43
44
00:03:38,890 --> 00:03:45,370
And what we want to happen is when we click on the male card, we want it to change color to show that
44
45
00:03:45,430 --> 00:03:47,650
it has been selected.
45
46
00:03:47,650 --> 00:03:52,110
And in order to do this we have to, of course, use our set state.
46
47
00:03:52,190 --> 00:03:56,860
And we're going to update the color property of our reusable card.
47
48
00:03:56,890 --> 00:04:04,970
This is why we have a stateful widget here so that we can actually change the state of our cards. The
48
49
00:04:04,970 --> 00:04:07,820
way that we're going to do this is our cards
49
50
00:04:07,820 --> 00:04:11,990
will start out with a slightly darker background color.
50
51
00:04:11,990 --> 00:04:17,760
And when it's tapped, it will change to have the same color as the rest of the card.
51
52
00:04:17,780 --> 00:04:20,460
So the active color.
52
53
00:04:20,870 --> 00:04:27,410
So firstly up here in our constants, we're going to have a activeCardColor and we're also going to
53
54
00:04:27,410 --> 00:04:36,860
create a new constant for the inactiveCardColor. And the inactiveCardColor is also going to be a
54
55
00:04:36,890 --> 00:04:44,200
custom color and it's going to have a hex value of 0xFF111328 .
55
56
00:04:44,420 --> 00:04:50,750
And you can see over here in the gutter that it's just a little bit darker from the activeCardColor,
56
57
00:04:51,050 --> 00:04:55,550
but it's not exactly the same as the main background color.
57
58
00:04:55,550 --> 00:04:57,350
It's just a little bit lighter.
58
59
00:04:57,440 --> 00:05:05,810
And if I go ahead and simply change the color for the male card for example to the inactive card color
59
60
00:05:05,990 --> 00:05:13,610
and hit save, then you can see that it's just a little bit darker than the active color but it's not
60
61
00:05:13,610 --> 00:05:17,570
quite the same as the background color so it doesn't just blend in.
61
62
00:05:17,570 --> 00:05:24,320
So you can still see that there is a card here but it's obviously not selected and not activated, whereas
62
63
00:05:24,320 --> 00:05:26,830
this card looks the same as the other cards.
63
64
00:05:26,930 --> 00:05:30,170
So it looks like it's the activated or the selected card.
64
65
00:05:30,170 --> 00:05:32,810
So this is the look that we're going for.
65
66
00:05:32,810 --> 00:05:41,930
And both cards should start out having the inactive card color but when one of them is tapped on, then
66
67
00:05:41,990 --> 00:05:45,890
we should change that color to the active card color.
67
68
00:05:46,010 --> 00:05:53,480
So to do this we're going to go into our state widget which is mutable so its properties can change
68
69
00:05:53,630 --> 00:05:58,670
and mutate, so the properties for this class don't have to be final.
69
70
00:05:58,670 --> 00:06:00,730
They can be variables that can change.
70
71
00:06:00,740 --> 00:06:04,000
So the first one we're going to create is going to be of type color.
71
72
00:06:04,310 --> 00:06:12,860
And it's going to be called maleCardColor and it's going to be set to equal the inactiveCardColor.
72
73
00:06:12,890 --> 00:06:20,250
So this is going to be the starting value for both the male card color and also the femaleCardColor.
73
74
00:06:20,570 --> 00:06:27,950
And we're going to be using this variables, femaleCardColor and maleCardColor inside our reusable
74
75
00:06:27,950 --> 00:06:28,710
cards.
75
76
00:06:28,790 --> 00:06:38,020
So this way, we're able to selectively change the color for these cards depending on which one was selected.
76
77
00:06:38,260 --> 00:06:45,010
Now in order to make our cards change onTap, we're going to create a method called updateColor.
77
78
00:06:45,190 --> 00:06:51,610
And this is going to take an input which is going to somehow tell us which gender was clicked on.
78
79
00:06:52,000 --> 00:06:56,900
So it's going to be called gender and it's simply just going to be an integer right now.
79
80
00:06:56,950 --> 00:07:04,780
And we could say that maybe for example, if we pass in one then that equals male and if we pass and two
80
81
00:07:04,780 --> 00:07:12,370
then that equals female. Inside this method, we're going to use an IF statement to check to see if the
81
82
00:07:12,370 --> 00:07:14,920
gender is equal to 1.
82
83
00:07:14,920 --> 00:07:22,740
So if we pass in the value one, then this means that it was the male card that was pressed.
83
84
00:07:22,780 --> 00:07:32,470
Then in this case we're going to check to see if the maleCardColor is currently equal to an inactive
84
85
00:07:32,500 --> 00:07:33,640
CardColor.
85
86
00:07:33,640 --> 00:07:39,520
Well in that case, we're going to change it to the activeCardColor.
86
87
00:07:39,520 --> 00:07:43,060
So we're going to toggle the color of the card.
87
88
00:07:43,660 --> 00:07:50,680
But if it was already in the activeCardColor, then we're going to change it to the opposite which is
88
89
00:07:50,680 --> 00:07:54,320
going to be the inactiveCardColor.
89
90
00:07:54,340 --> 00:08:02,050
So now if we go into our onTap and instead of printing the 'Male card was pressed', we use a set state
90
91
00:08:02,260 --> 00:08:08,860
to update the state of our maleCardColor property which is used to determine the color.
91
92
00:08:08,860 --> 00:08:12,090
Then we can call that method that we created just now updateColor.
92
93
00:08:12,400 --> 00:08:16,920
And we said that the number 1 was going to represent the male gender.
93
94
00:08:16,930 --> 00:08:23,640
So now if we hit save and you can see that my card both start out being the inactive color.
94
95
00:08:23,890 --> 00:08:30,290
So they're not quite the same as this one but as soon as I click on it it changes to the active color.
95
96
00:08:30,550 --> 00:08:37,690
And that's because I'm passing in 1 into this method and while maleCardColor started out being inactive,
96
97
00:08:38,080 --> 00:08:42,390
as soon as it detects the touch, then it'll update it to the active color.
97
98
00:08:42,430 --> 00:08:43,990
But right now it's the active color
98
99
00:08:43,990 --> 00:08:47,980
so if I click on it again, it'll go back to the inactive color.
99
100
00:08:48,010 --> 00:08:54,700
So this way we can toggle the background color of the card to be able to represent when something is selected
100
101
00:08:54,820 --> 00:08:56,840
or not.
101
102
00:08:56,980 --> 00:09:05,020
So let's go ahead and do the same for the female card. All we need to do is to add another ELSE, if the
102
103
00:09:05,020 --> 00:09:10,360
gender is not equal to one or if you want to have more than one gender in your app, then you might simply
103
104
00:09:10,360 --> 00:09:14,440
just add an IF statement that checks if gender equals 2.
104
105
00:09:14,590 --> 00:09:19,400
So we'll say that 2 is equal to female card pressed.
105
106
00:09:19,690 --> 00:09:23,260
And in this case, we're pretty much going to do the opposite.
106
107
00:09:23,290 --> 00:09:29,270
So we're going to check to see if the femaleCardColor starts out being inactive.
107
108
00:09:29,320 --> 00:09:35,100
If so, we're going to change it to the active color but otherwise we're going to toggle it to inactive.
108
109
00:09:35,110 --> 00:09:44,080
So now inside our female card, we're going to also wrap our reusable card inside a new gesture detector.
109
110
00:09:44,080 --> 00:09:50,890
And this is also going to have a onTap which is an anonymous function that gets triggered when the
110
111
00:09:50,890 --> 00:09:52,870
tap gets detected.
111
112
00:09:52,870 --> 00:09:59,790
And when that happens, we're going to call set state to update the color and we're going to pass in the
112
113
00:09:59,790 --> 00:10:05,710
gender as number two, to say that the female card got pressed. So let's hit save
113
114
00:10:05,750 --> 00:10:07,190
and let's check out our app.
114
115
00:10:07,530 --> 00:10:13,930
So now when I click the male card, the male card lights up and I can deactivate it by clicking on it again.
115
116
00:10:14,070 --> 00:10:17,660
And if I click on the female card, the female card gets activated.
116
117
00:10:17,670 --> 00:10:23,430
So now we're able to select a gender in our BMI calculator.
117
118
00:10:23,580 --> 00:10:29,850
Now at the moment though when I click on the male card when the female card is selected, the female card
118
119
00:10:29,850 --> 00:10:30,860
stays selected
119
120
00:10:30,870 --> 00:10:31,190
right?
120
121
00:10:31,410 --> 00:10:35,700
But let's say that I made a mistake and I actually meant to select this one, then when I click on it
121
122
00:10:35,820 --> 00:10:41,100
it should turn off this one because they probably shouldn't both be selected at the same time.
122
123
00:10:42,210 --> 00:10:44,610
So how can we do this?
123
124
00:10:44,610 --> 00:10:49,770
Well it's pretty easy although it's going to add to our already super wordy method.
124
125
00:10:49,860 --> 00:10:53,180
Don't worry, we're gonna make it a lot easier on the eye a little bit later on.
125
126
00:10:53,310 --> 00:10:57,110
But we're first mapping out the functionality so we understand what's going on.
126
127
00:10:57,120 --> 00:11:04,290
So if updateColor is called and it was the male card that was pressed, so gender is one, then we're going
127
128
00:11:04,290 --> 00:11:11,020
to not only switch on the maleCardColor, but we're also going to switch off the femaleCardColor. So
128
129
00:11:11,070 --> 00:11:17,970
we're gonna change this to inactiveCardColor. And the opposite is also true when the female card gets
129
130
00:11:17,970 --> 00:11:18,480
pressed,
130
131
00:11:18,480 --> 00:11:21,950
then we're going to switch off the maleCardColor.
131
132
00:11:22,580 --> 00:11:24,770
So we're going to change that to inactive.
132
133
00:11:24,970 --> 00:11:32,910
And now let's hit save and you can see that if I click on the male card, the female card switches off.
133
134
00:11:32,910 --> 00:11:35,830
If I click on the female card, the male cuts which is off.
134
135
00:11:35,910 --> 00:11:41,200
And this behavior makes a lot more sense. Now in the coming lessons,
135
136
00:11:41,220 --> 00:11:48,180
we're going to simplify all of this logic to something that's a lot shorter and more succinct and easier
136
137
00:11:48,180 --> 00:11:48,650
on the eyes.
137
138
00:11:48,690 --> 00:11:53,320
Because at the moment, it's super wordy and very confusing what's actually going on.
138
139
00:11:53,460 --> 00:11:58,060
But in order to achieve this, we need to learn a little bit more about Dart.
139
140
00:11:58,080 --> 00:12:02,870
So in the next lesson we're going to talk about Dart enums. For all of that and more,
140
141
00:12:02,970 --> 00:12:03,480
I see you there.
16179
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.