Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,390 --> 00:00:03,420
So currently, this is what our app looks like.
1
2
00:00:03,420 --> 00:00:11,510
We've got seven keys, one in each color, and they all manage to play different sounds. But at the moment,
2
3
00:00:11,690 --> 00:00:13,100
the layout isn't great, right?
3
4
00:00:13,100 --> 00:00:18,950
It's only taking up a small proportion of the screen. And it doesn't really look like a Xylophone app
4
5
00:00:18,980 --> 00:00:19,970
yet.
5
6
00:00:20,030 --> 00:00:26,930
So how can we get it to look like this instead? Where we have a black background so that any part that's
6
7
00:00:26,930 --> 00:00:29,360
not a key is blank.
7
8
00:00:29,360 --> 00:00:37,010
And we also have a full width keys that take up all of the available space in the horizontal axis and
8
9
00:00:37,250 --> 00:00:41,610
all the vertical space is evenly divided by these seven keys.
9
10
00:00:41,750 --> 00:00:46,670
Now there shouldn't be anything new here because we've covered all of this when we explored layouts
10
11
00:00:47,120 --> 00:00:49,130
in the Mi card app.
11
12
00:00:49,130 --> 00:00:55,010
So I want to set this to you as a challenge to see if you can make the user interface look like this.
12
13
00:00:56,350 --> 00:01:01,090
Pause the video and try to complete the challenge.
13
14
00:01:01,300 --> 00:01:01,650
All right.
14
15
00:01:01,710 --> 00:01:03,270
So here's the solution.
15
16
00:01:03,270 --> 00:01:08,100
First let's change the background color of our scaffold to black.
16
17
00:01:08,100 --> 00:01:10,810
So we're going to go into our scaffold widget.
17
18
00:01:10,890 --> 00:01:17,630
We're gonna add a property that's changing the background color to colors.black.
18
19
00:01:17,640 --> 00:01:18,300
There we go.
19
20
00:01:18,300 --> 00:01:22,420
And let's just confirmd to make sure that it worked.
20
21
00:01:22,770 --> 00:01:23,010
All right.
21
22
00:01:23,040 --> 00:01:28,710
So here's what we've got so far. The white background has turned into a black background, and it's blended
22
23
00:01:28,710 --> 00:01:31,130
seamlessly with the edges.
23
24
00:01:31,140 --> 00:01:36,600
So now let's go ahead and try to expand our buttons.
24
25
00:01:36,600 --> 00:01:41,340
So the keys will distribute themselves along the main axis of our column.
25
26
00:01:41,340 --> 00:01:48,510
And to do that, if you remember, we had to use the expanded widget. So we would wrap whatever we want to
26
27
00:01:48,510 --> 00:01:55,440
expand inside an expanded widget, and we're going to use the OPTION + ENTER to bring up that light bulb
27
28
00:01:55,440 --> 00:02:01,670
menu, and we're going to wrap with a new widget that's going to be an expanded widget.
28
29
00:02:01,770 --> 00:02:08,250
Now at this point if you only have a single expanded widget that will try to take up all of the space
29
30
00:02:08,310 --> 00:02:10,770
that's available to it.
30
31
00:02:10,770 --> 00:02:14,420
So it looks something like this, which is not what we want.
31
32
00:02:14,430 --> 00:02:21,060
We want all of these keys to divide the amount of height that's available by themselves so that they each
32
33
00:02:21,060 --> 00:02:23,240
take up the same amount of space.
33
34
00:02:23,370 --> 00:02:29,320
So to do that, we have to add all of these other buttons inside the expanded widget.
34
35
00:02:29,340 --> 00:02:36,060
Now this is a good point to mention that when you notice any of these widgets enclosed in a red box,
35
36
00:02:36,510 --> 00:02:42,780
at this point, if you go to another widget say the next flat button that you want to embed in a expanded
36
37
00:02:42,780 --> 00:02:50,070
widget and you use that same trick of doing ALT + ENTER or OPTION + ENTER, then it doesn't really work because
37
38
00:02:50,070 --> 00:02:52,770
it's still held on to this widget.
38
39
00:02:52,770 --> 00:02:59,280
So this is a quirk of Android Studio and you can simply get rid of the red box by just hitting the TAB
39
40
00:02:59,280 --> 00:03:02,320
key anywhere where there's some empty space.
40
41
00:03:02,670 --> 00:03:09,770
And that gets rid of the selection and we can now again use our OPTION + ENTER or ALT + ENTER.
41
42
00:03:09,810 --> 00:03:15,900
So let's go through our file and let's add expanded to all of our flat buttons.
42
43
00:03:19,420 --> 00:03:24,910
So now that we've wrapped all of our flat buttons inside an expanded widget,
43
44
00:03:24,910 --> 00:03:31,330
now all of our buttons should now expand to the amount of height they can possibly have.
44
45
00:03:31,330 --> 00:03:39,460
So now all our buttons should have the same height and it should be evenly distributed in our column.
45
46
00:03:39,700 --> 00:03:47,100
And you can see, our flat buttons are now evenly distributed in terms of height, at least, in our column.
46
47
00:03:47,410 --> 00:03:53,860
But the only thing is the width that each button is taking up is still the default amount that they're
47
48
00:03:53,860 --> 00:03:55,320
told to take up.
48
49
00:03:55,420 --> 00:04:01,820
So in order for it to go all the way to the edge, we have to change the cross axis alignment of our column.
49
50
00:04:01,930 --> 00:04:07,510
So we find our column widget and we're going to add a cross axis alignment and we're going to make it
50
51
00:04:07,630 --> 00:04:17,040
stretch so that it takes up the entire width of the screen, or as much width as possible basically.
51
52
00:04:17,050 --> 00:04:22,750
So this is the solution to our mini challenge, and I hope that was a good review of some of the concepts
52
53
00:04:22,750 --> 00:04:24,940
that you learnt in the Mi card module
53
54
00:04:25,090 --> 00:04:29,670
where we did mostly things such as layouts and axis alignments et cetera.
54
55
00:04:29,860 --> 00:04:36,350
So now we've basically completed our user interface. It looks pretty much the way that we want it to.
55
56
00:04:36,430 --> 00:04:41,770
But if we take a look at our code, you can see that it's incredibly repetitive.
56
57
00:04:41,830 --> 00:04:44,520
There's so many lines of repeated code.
57
58
00:04:44,650 --> 00:04:51,460
And if you look at these little sections of expanded widgets, they're extremely similar to each other.
58
59
00:04:51,460 --> 00:04:53,300
They're all expanded widgets.
59
60
00:04:53,320 --> 00:04:57,950
They each have a child of a flat button. The flat buttons each have a color.
60
61
00:04:58,000 --> 00:05:02,800
And when they get pressed, they all call the play sound method up here.
61
62
00:05:03,190 --> 00:05:06,660
So, it's not great to have all of this code
62
63
00:05:06,670 --> 00:05:12,770
that's the same, because let's say that we wanted to, I don't know, change something about our buttons,
63
64
00:05:12,790 --> 00:05:20,050
for example if we wanted to give it, you know, some piece of text that says 'This is a button',
64
65
00:05:20,830 --> 00:05:27,350
then we would have to do this to every single one of our buttons for this to be carried out everywhere.
65
66
00:05:27,400 --> 00:05:33,070
And as you saw even previously when we were just adding the expanded widget around all the flat buttons,
66
67
00:05:33,370 --> 00:05:34,480
it was painful enough.
67
68
00:05:34,510 --> 00:05:40,510
So how can we reduce all of this code redundancy and try to get our code just a bit tighter?
68
69
00:05:40,510 --> 00:05:45,660
Well we can probably extract a single one of our widgets,
69
70
00:05:45,700 --> 00:05:46,330
right?
70
71
00:05:46,330 --> 00:05:50,960
What if instead of creating this thing many many times over,
71
72
00:05:51,040 --> 00:05:54,990
what if we tried to use a function to create this instead?
72
73
00:05:55,060 --> 00:06:00,570
And we call that function seven times, instead of creating it from scratch every single time.
73
74
00:06:00,580 --> 00:06:03,130
Isn't that what functions are for, right?
74
75
00:06:03,190 --> 00:06:09,310
So in order to do that we're going to remove a single expanded widget, and I'm going to use COMMAND + X
75
76
00:06:09,310 --> 00:06:14,950
or CONTROL + X to cut it out of my column. And up here just below playSound,
76
77
00:06:14,950 --> 00:06:19,350
I'm going to create a new function called buildKey.
77
78
00:06:19,720 --> 00:06:27,070
And this function is meant to help me build this expanded widget. And I want to be able to, instead of
78
79
00:06:27,070 --> 00:06:34,750
having to have all of this repeated code, I'm going to delete everything right up until the final expanded.
79
80
00:06:34,750 --> 00:06:39,190
So just before it says widgets, and I'm gonna delete all of that.
80
81
00:06:39,250 --> 00:06:49,190
Instead I want to be able to call my method buildKey seven times, and this should build my widget each
81
82
00:06:49,190 --> 00:06:50,170
time.
82
83
00:06:50,270 --> 00:06:52,010
So let's just check to make sure I've got enough
83
84
00:06:52,010 --> 00:06:55,440
1, 2, 3, 4, 5, 6 & 7 so perfect.
84
85
00:06:55,730 --> 00:07:02,660
So once this starts working, every single widget will be built using our new function called buildKey.
85
86
00:07:03,020 --> 00:07:09,140
And it will build an expanded widget with a flat button inside with a color and also a sound that gets
86
87
00:07:09,140 --> 00:07:10,040
played.
87
88
00:07:10,040 --> 00:07:14,250
Now there's just a couple of fixes that we have to make here.
88
89
00:07:14,300 --> 00:07:21,920
Firstly, at the end of each line of code in a function, we know it has to end with a semicolon. And that's
89
90
00:07:21,920 --> 00:07:25,780
just the grammar of functions. As in English language
90
91
00:07:25,790 --> 00:07:31,970
we have the full stop, in programming when we have a function and we get it to do something or execute
91
92
00:07:31,970 --> 00:07:36,120
something, we have to tell it where it's the end of that line of instruction.
92
93
00:07:36,410 --> 00:07:43,350
But our code still have some errors. And all the errors are down here, and they all say the same thing.
93
94
00:07:43,430 --> 00:07:48,860
The expression here has a type of void, and therefore cannot be used.
94
95
00:07:48,890 --> 00:07:51,500
What is this void that they're referring to?
95
96
00:07:51,500 --> 00:07:57,950
Well it's because each of our build key functions have that void keyword here that we've so far tried
96
97
00:07:57,950 --> 00:07:59,710
to avoid talking about.
97
98
00:07:59,810 --> 00:08:06,350
And the reason is because this will involve learning about the third type of functions, which is function
98
99
00:08:06,380 --> 00:08:12,380
that not only have a body, as in some instructions that it needs to carry out when it gets triggered, but
99
100
00:08:12,440 --> 00:08:19,480
also it can have a input, as we've seen, by putting in a argument inside the parentheses.
100
101
00:08:19,550 --> 00:08:25,040
And finally the third type of functions that we want to talk about are the type where it can actually have
101
102
00:08:25,130 --> 00:08:31,670
an output. So something can come out of this function once it is called.
102
103
00:08:31,860 --> 00:08:37,800
And right now, this function, our buildKey function, doesn't actually have an output.
103
104
00:08:37,800 --> 00:08:39,960
It's basically a type 1 function right?
104
105
00:08:39,960 --> 00:08:42,200
It doesn't even take any input.
105
106
00:08:42,210 --> 00:08:47,910
So if you are somebody who already have experience programming in other languages, you can solve this
106
107
00:08:47,910 --> 00:08:54,390
problem and you can get rid of this error and get it to produce seven buttons on screen that all play
107
108
00:08:54,390 --> 00:09:01,860
the same sound that all have the color red, then you can skip the next lesson. But if you can't, then I
108
109
00:09:01,860 --> 00:09:06,810
recommend heading over to the next lesson where we're going to talk about the third type of functions
109
110
00:09:06,810 --> 00:09:13,070
in Dart, functions that can have an output. Whichever path you choose,
110
111
00:09:13,070 --> 00:09:14,990
I will see you on the next lesson.
12703
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.