All language subtitles for 10. The Flutter Slider Widget

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
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:07,500 Now in the last few lessons we've been refactoring our code and making it simpler, easier to read and 1 2 00:00:07,560 --> 00:00:13,500 more succinct. And in the process we've learned a lot of the fundamentals of Dart that enables us to do 2 3 00:00:13,500 --> 00:00:19,970 this such as enums or const, final parsing function and a whole lot of programming knowledge. 3 4 00:00:20,010 --> 00:00:25,320 But in this lesson we're going to get back to our design and we're going to build out the next card. 4 5 00:00:26,970 --> 00:00:33,510 Now the next card is going to be a slider and we're gonna be using a Flutter slider but we're going 5 6 00:00:33,510 --> 00:00:39,450 to customize it to our liking. And in the process we're going to learn a lot about customizing Flutter 6 7 00:00:39,440 --> 00:00:40,850 widgets. 7 8 00:00:41,040 --> 00:00:47,040 So first things first, let's head over to our next reusable card, which is right here. And at the moment 8 9 00:00:47,160 --> 00:00:49,680 it doesn't really do anything. It's completely empty. 9 10 00:00:49,740 --> 00:00:58,380 So let's give it a cardChild. The cardChild property is going to hold a number of things. Because we 10 11 00:00:58,380 --> 00:01:01,720 have quite a few things that are going to be stacked vertically, 11 12 00:01:01,770 --> 00:01:07,920 it makes sense to create a column widget. And it's going to have a bunch of children, the first of which 12 13 00:01:07,950 --> 00:01:14,190 is going to be a text widget. And our text is simply going to say the word height because this is going 13 14 00:01:14,190 --> 00:01:22,230 to be the height input. And I would really love for that text to have the same style as the male and 14 15 00:01:22,230 --> 00:01:23,220 female text here. 15 16 00:01:23,340 --> 00:01:31,470 So the same font size, the same color and the same font. And at the moment that style is coming from a 16 17 00:01:31,470 --> 00:01:38,940 constant that's defined in our icon_content.dart file. And it doesn't really make sense to reach into 17 18 00:01:38,940 --> 00:01:45,140 the icon content file to be able to define a label that's on my input page. 18 19 00:01:45,210 --> 00:01:47,290 It seems like that would get really messy. 19 20 00:01:48,090 --> 00:01:55,110 So this is a good time to talk about creating a dedicated constants file. As we create our design and 20 21 00:01:55,110 --> 00:02:02,010 our user interface, it's very frequent that you might want to share designs or share settings across 21 22 00:02:02,010 --> 00:02:03,270 different screens. 22 23 00:02:03,270 --> 00:02:09,690 So instead of having it live inside a particular file or a particular component, it makes sense to create 23 24 00:02:09,780 --> 00:02:15,350 a new file that is going to be a Dart file and it's going to be called constants. 24 25 00:02:15,870 --> 00:02:21,550 And this is going to hold all of the things that are constant across our app. 25 26 00:02:21,630 --> 00:02:27,900 So that is going to include this labelTextStyle for example. So I'm going to cut it out of here and paste 26 27 00:02:27,900 --> 00:02:29,670 it into here instead. 27 28 00:02:29,670 --> 00:02:36,900 And of course whenever we're using material components we have to import our material library. 28 29 00:02:36,930 --> 00:02:44,550 Now in addition to these text styles, I can also add in all of my color constants from over here. And we 29 30 00:02:44,550 --> 00:02:47,960 can paste that into this file instead. 30 31 00:02:48,120 --> 00:02:55,560 And now I have all of my constants together in one place and I can refer to it from any of my files 31 32 00:02:55,620 --> 00:02:58,620 simply by importing the constants. 32 33 00:02:58,620 --> 00:03:04,020 So at the moment you can see that we have some errors here because it doesn't know about labelTextStyle 33 34 00:03:04,020 --> 00:03:06,050 anymore since we moved it out. 34 35 00:03:06,210 --> 00:03:12,660 But as soon as I hit import and I import my constants.dart file, then that error goes away and it's 35 36 00:03:12,660 --> 00:03:17,920 able to see into here and see what text style it should apply to that label. 36 37 00:03:17,920 --> 00:03:22,710 Now similarly on my input page, I've got errors all over the place because it doesn't know about this 37 38 00:03:22,800 --> 00:03:25,320 activeCardcolor and inactiveCardColor. 38 39 00:03:25,620 --> 00:03:30,970 So let's go ahead and import our constants into this file as well. 39 40 00:03:31,050 --> 00:03:39,060 So now it's a good time to mention that the naming convention for these constants usually start out 40 41 00:03:39,060 --> 00:03:40,640 with a K. 41 42 00:03:40,860 --> 00:03:48,750 So we can refactor our code to meet that style. So we can right click on each of our constants and we 42 43 00:03:48,750 --> 00:03:55,770 can go to refactor and rename which is our friend that is going to search for all the places where we 43 44 00:03:55,770 --> 00:03:59,460 use this constant and rename it all in one go. 44 45 00:03:59,460 --> 00:04:05,100 So instead of calling it bottomContainerHeight, the convention is to start with a K, K for constant 45 46 00:04:05,550 --> 00:04:08,040 because C is used for a lot of other things. 46 47 00:04:08,490 --> 00:04:12,300 So K for constant and then we're going to start off with a capital B. 47 48 00:04:12,420 --> 00:04:18,180 So K constant, bottomContainerHeight we're going to refactor that and now you can see everywhere where 48 49 00:04:18,180 --> 00:04:22,330 we use that, it's now been refactored automatically. 49 50 00:04:22,320 --> 00:04:27,780 So I'm just gonna go ahead and do the same for all my other constant. And from now on when I'm creating 50 51 00:04:27,780 --> 00:04:33,460 new constants in my constants.dart, I'll be using that naming convention. 51 52 00:04:33,800 --> 00:04:34,050 All right. 52 53 00:04:34,080 --> 00:04:40,920 So here are all my constants renamed and they're going to be renamed everywhere where they're used. 53 54 00:04:40,920 --> 00:04:44,610 Now why did I go through all that trouble of renaming all my constants? 54 55 00:04:44,850 --> 00:04:52,470 Well whenever you're trying to pick out a color or a text style from your constants file, you start out 55 56 00:04:52,470 --> 00:04:59,600 by typing K. And now Android Studio is intelligent enough to show you all of the relevant constants 56 57 00:04:59,630 --> 00:05:05,730 that you might want to use at this point. So you can see that there's kBottomContainerColor, kBottom 57 58 00:05:05,730 --> 00:05:13,080 ContainerHeight and there's also constants that Flutter has defined such as the kMaterialEdges, k 58 59 00:05:13,080 --> 00:05:14,250 MaterialListPadding, 59 60 00:05:14,310 --> 00:05:18,810 so this way all the constants can be searched easily in one go. 60 61 00:05:19,350 --> 00:05:25,440 So let's change that back to kActiveCardColor. And every time you're looking for a constant you get 61 62 00:05:25,440 --> 00:05:31,680 that dropdown list to select from, which is much easier than trying to remember what the name of your 62 63 00:05:31,680 --> 00:05:37,230 constants at the top of the file is especially once this list gets a bit longer and your app gets more 63 64 00:05:37,230 --> 00:05:38,920 complex. 64 65 00:05:38,930 --> 00:05:39,180 All right. 65 66 00:05:39,210 --> 00:05:48,240 So back to our reusableCard which now has a text widget and I'm now able to give it a additional style 66 67 00:05:48,690 --> 00:05:52,090 which is going to be the kLabelTextStyle, 67 68 00:05:52,140 --> 00:05:53,810 so I can simply select that. 68 69 00:05:54,030 --> 00:06:01,950 And now you should be able to see in the app we've got the same styling for that height text. But our 69 70 00:06:01,950 --> 00:06:06,150 card has now resized to the size of its child. 70 71 00:06:06,240 --> 00:06:12,750 So remember that the way that containers work is that if they have no children, they'll try to size themselves 71 72 00:06:12,990 --> 00:06:15,560 to be as large as the parent allows, 72 73 00:06:15,900 --> 00:06:22,860 and in this case it was stretched to the full width of the column. But once it receives the child, it 73 74 00:06:22,860 --> 00:06:26,930 will try to resize itself to fit the size of the child. 74 75 00:06:26,940 --> 00:06:30,350 So that's why my card has now shrunken in width. 75 76 00:06:30,750 --> 00:06:32,820 But this is relatively easy to fix. 76 77 00:06:32,850 --> 00:06:41,040 We can simply go to our main column at the very top and we can change it's crossAxisAlignment to our 77 78 00:06:41,040 --> 00:06:49,020 friend which is crossAxisAlignment.stretch. And now it will stretch all of its children to take 78 79 00:06:49,020 --> 00:06:56,850 up the full width of the column no matter the size of the child. In addition to the height label, I also 79 80 00:06:56,850 --> 00:07:04,500 want to add a value for the selected height so that when the user toggles the slider it will change 80 81 00:07:04,650 --> 00:07:06,140 as they move it around. 81 82 00:07:06,150 --> 00:07:13,430 So I now need to add a number and a bit of text that says cm or centimeter. In order to do that, 82 83 00:07:13,440 --> 00:07:22,900 I'm going to add a row under my text widget right here. So my row widget is going to have just two children 83 84 00:07:23,500 --> 00:07:29,550 and both of them are going to be text widgets because one of them is going to display the height that's 84 85 00:07:29,560 --> 00:07:38,180 selected and the second one is just going to display the word cm. My text widget is going to contain 85 86 00:07:38,240 --> 00:07:39,570 some numbers. 86 87 00:07:39,590 --> 00:07:47,930 So let's say it starts out with maybe the string 180. And then it's going to have some styling. And I 87 88 00:07:47,930 --> 00:07:50,480 want this to be super large 88 89 00:07:50,480 --> 00:08:02,390 so my textile is going to have a font size of maybe 50 and it's also going to have a super super heavy 89 90 00:08:02,390 --> 00:08:09,110 font weight. So I'm going to set it as probably w900 which is the heaviest weight possible. 90 91 00:08:09,280 --> 00:08:12,430 The heavyweight champion of my font. 91 92 00:08:12,800 --> 00:08:19,550 So now if we take a look at our design, you can see that the number is pretty much as thick as my icons 92 93 00:08:19,640 --> 00:08:27,050 and I quite like that consistency. So I'm probably going to be using this number style across the other numbers 93 94 00:08:27,080 --> 00:08:28,370 that are on the screen, 94 95 00:08:28,370 --> 00:08:35,480 so for example when I create the weight card or the age card. So it makes sense to turn this into a constant. 95 96 00:08:35,830 --> 00:08:42,050 Pause the video and create a constant from this text style and replace all of this code here. 96 97 00:08:45,290 --> 00:08:45,600 All right. 97 98 00:08:45,630 --> 00:08:50,710 So of course we're going to create our constants in our brand new constants.dart file. 98 99 00:08:50,730 --> 00:08:52,760 We're going to start out with the keyword const 99 100 00:08:52,920 --> 00:08:55,980 and we're going to name it using the K convention. 100 101 00:08:55,980 --> 00:09:00,940 So this is going to be k and I'm going to call it numberTextStyle. 101 102 00:09:00,960 --> 00:09:03,450 You can call yours anything you wish. 102 103 00:09:03,540 --> 00:09:08,370 And I'm going to replace it with what I have here which is my text style. 103 104 00:09:08,430 --> 00:09:12,380 And over here I'm going to call it NumberTextStyle. 104 105 00:09:12,390 --> 00:09:15,430 And now that should refer to our file over here. 105 106 00:09:15,450 --> 00:09:19,940 And I'm going to paste in that text style I had from before. 106 107 00:09:20,010 --> 00:09:25,260 So now I can reuse this one and this one across a lot of my design components. 107 108 00:09:25,260 --> 00:09:33,110 And it keeps my code relatively simple and straightforward on my main page. Now that I've created my 108 109 00:09:33,110 --> 00:09:33,800 number, 109 110 00:09:33,980 --> 00:09:41,030 let's go ahead and add the second text widget in my row which is going to simply just say cm. 110 111 00:09:41,090 --> 00:09:47,630 Now if you're from the States and you want to have imperial as your unit then feel free to change the 111 112 00:09:48,080 --> 00:09:51,390 numbers or the units as you wish. 112 113 00:09:51,410 --> 00:09:58,020 Now, that cm is going to have the same text style as my male, female or height labels. 113 114 00:09:58,040 --> 00:10:05,030 Simple enough, I can simply tap into the style property, add my k for constant and select my label text style. 114 115 00:10:05,420 --> 00:10:09,230 So now I have 180 and I have my cm. 115 116 00:10:09,320 --> 00:10:16,970 Now you'll notice that everything is kind of all over the place at the moment. My height label is all 116 117 00:10:16,970 --> 00:10:18,830 squished right at the top 117 118 00:10:18,980 --> 00:10:22,020 and my number row is all the way to the left. 118 119 00:10:22,130 --> 00:10:23,380 And it doesn't look very good. 119 120 00:10:23,390 --> 00:10:31,520 So let's bring all of our components into the center of our column by changing the mainAxisAlignment 120 121 00:10:31,550 --> 00:10:33,820 of our column to center. 121 122 00:10:34,010 --> 00:10:38,000 And this should bring everything down into the middle of the column. 122 123 00:10:38,000 --> 00:10:42,210 Now let's also make our row centered along the main axis 123 124 00:10:42,230 --> 00:10:48,800 so it brings it right into the middle, by going over to our row and changing our mainAxisAlignment 124 125 00:10:48,830 --> 00:10:51,590 to center as well. 125 126 00:10:51,590 --> 00:10:53,930 And now everything's at least in the middle. 126 127 00:10:54,080 --> 00:11:00,320 But there's one other thing that I would really like to have, which is for the cm or the centimeter 127 128 00:11:00,410 --> 00:11:06,720 or whatever units you chose, to be on the same baseline as my number here. 128 129 00:11:06,740 --> 00:11:12,260 So that way it looks like it's actually a 180 cm rather than sort of having the unit floating around 129 130 00:11:12,260 --> 00:11:13,580 somewhere in the middle. 130 131 00:11:13,940 --> 00:11:19,610 And you can see in the final design that's exactly what I have and it looks a lot nicer when it's aligned 131 132 00:11:19,670 --> 00:11:21,970 all to the same baseline. 132 133 00:11:22,010 --> 00:11:27,560 Now we can do this quite easily by simply adding a crossAxisAlignment to our row. 133 134 00:11:27,680 --> 00:11:33,650 So remember that the main axis is along the length of the row and the cross axis is along the short 134 135 00:11:33,650 --> 00:11:33,940 side, 135 136 00:11:33,950 --> 00:11:35,420 so the width of the row. 136 137 00:11:36,110 --> 00:11:42,260 And we're going to set our crossAxisAlignment to one of the options which sounds very very much like 137 138 00:11:42,260 --> 00:11:45,400 what we want, which is .baseline. 138 139 00:11:45,500 --> 00:11:52,160 And in this case, it's going to place the children along the cross axis such that their base lines match. 139 140 00:11:52,160 --> 00:11:53,830 Let's go ahead and select that. 140 141 00:11:54,350 --> 00:12:00,980 Now at this point if we hit save and we tried to look at our design, you'll see that it will break. 141 142 00:12:01,040 --> 00:12:07,820 And the reason it tells us seems kind of cryptic. It's saying if we are going to change our cross axis 142 143 00:12:07,820 --> 00:12:14,180 alignment to baseline, then we actually have to have a text baseline property set to something and it 143 144 00:12:14,180 --> 00:12:15,890 can't be null. 144 145 00:12:15,920 --> 00:12:16,910 How do I know this? 145 146 00:12:16,910 --> 00:12:25,470 Well you can simply take this error message from our run panel and pass it through our friend which 146 147 00:12:25,470 --> 00:12:31,730 has Google of course. And we can search to see if this has already occurred for somebody else. 147 148 00:12:32,330 --> 00:12:36,920 So very often when you get error codes with Flutter, you can search for it in Google and it'll come 148 149 00:12:36,920 --> 00:12:44,090 up in Stack Overflow or in the actual Flutter GitHub repository issue area. And here somebody actually 149 150 00:12:44,090 --> 00:12:51,110 from the Flutter team has provided the solution here saying that you have to specify the textBaseline 150 151 00:12:51,140 --> 00:12:58,820 argument if you want the baseline to be aligned, so that they know which baseline to line against and 151 152 00:12:58,940 --> 00:13:02,320 maybe in the future, they're going to plan on making that message better. 152 153 00:13:02,330 --> 00:13:07,820 And this is really one of the great things about the Flutter community. It's in development but your 153 154 00:13:07,820 --> 00:13:13,720 input and also all the other developers experiences are contributing to the final product. 154 155 00:13:13,820 --> 00:13:19,160 And day by day fixes will be made and changes will happen and it's just going to get better. 155 156 00:13:19,190 --> 00:13:20,810 So let's try and implement that. 156 157 00:13:20,810 --> 00:13:25,120 So they're talking about something called a textBaseline argument. 157 158 00:13:25,160 --> 00:13:32,990 So where we have our crossAxisAlignment for our row, if we go ahead we can see that if we click on 158 159 00:13:33,080 --> 00:13:39,800 CONTROL + J or CONTROL + Q on Windows, you can see that one of its properties is exactly that text 159 160 00:13:39,850 --> 00:13:40,500 base line. 160 161 00:13:40,850 --> 00:13:45,270 So let's try and set it so that our crossAxisAlignment.baseline will work. 161 162 00:13:45,290 --> 00:13:52,040 So let's add textBaseline and then we can start typing TextBaseline with a capital T. And we can 162 163 00:13:52,040 --> 00:14:00,200 now see that we have two options either alphabetic which is for alphabetic characters or ideographic 163 164 00:14:00,290 --> 00:14:03,440 which is for basically graphical characters. 164 165 00:14:03,440 --> 00:14:10,970 So we want alphabetic and now if we hit save, you can see that our error goes away and our row is now 165 166 00:14:10,970 --> 00:14:15,480 beautifully aligned across the baseline. Pretty neat. 166 167 00:14:15,560 --> 00:14:22,280 Now if you're working on any sort of development be it web or mobile or Flutter, you'll inevitably encounter 167 168 00:14:22,280 --> 00:14:27,560 errors and warnings and problems. But just know that there's a strong community behind you who will be 168 169 00:14:27,560 --> 00:14:30,430 able to help you out and you are not alone. 169 170 00:14:30,560 --> 00:14:35,370 And in most cases the things that you've experienced, other people have experienced too and they've probably 170 171 00:14:35,370 --> 00:14:38,540 come across a solution already. All right. 171 172 00:14:38,570 --> 00:14:48,440 So we've solved our labels and our numbers, and it's now time to add a slider. Now Flutter has a widget 172 173 00:14:48,470 --> 00:14:56,920 called a slider and it will implement a material design slider. And if we search for it, you can see that 173 174 00:14:56,920 --> 00:15:04,300 this is roughly what it looks like. It's got a small round thumb which you can move around and it's got 174 175 00:15:04,300 --> 00:15:06,830 one active side and one inactive side. 175 176 00:15:06,880 --> 00:15:12,820 And when you click on it, it's got this sort of outline that pops up. And that looks very similar to what 176 177 00:15:12,820 --> 00:15:15,020 we want to implement in our design. 177 178 00:15:15,520 --> 00:15:21,760 So let's go ahead and implement it so that we can create this design. Now right below where I have my 178 179 00:15:21,760 --> 00:15:23,230 row ending here, 179 180 00:15:23,410 --> 00:15:32,370 I'm going to add a slider widget. And the slider widget has a number of properties such as a onChanged 180 181 00:15:32,470 --> 00:15:33,060 callback. 181 182 00:15:33,070 --> 00:15:38,860 So it's expecting a method that takes a double as an input and outputs void. 182 183 00:15:38,890 --> 00:15:44,710 So whenever you see an arrow as one of the properties, you always know that this is expecting a function. 183 184 00:15:45,940 --> 00:15:50,530 And it can also detect when a change starts or when the change ends. 184 185 00:15:50,530 --> 00:15:56,830 So that means when the user starts dragging and ends dragging, you can also set the minimum position 185 186 00:15:57,130 --> 00:15:58,730 and the maximum position. 186 187 00:15:58,750 --> 00:16:05,590 So maybe if you're setting a temperature, you'll go from 0 to 30, but maybe for other users your scale 187 188 00:16:05,590 --> 00:16:07,680 will be different so you can determine that. 188 189 00:16:07,900 --> 00:16:13,150 And then there's the number of divisions you might want, a label for the slider and then there's the 189 190 00:16:13,240 --> 00:16:15,430 active color and the inactive color. 190 191 00:16:16,360 --> 00:16:22,000 So the active color, the documentation says, is the color to use for the proportion of the slider track 191 192 00:16:22,060 --> 00:16:24,050 that's active. 192 193 00:16:24,310 --> 00:16:27,690 And if you click on it, it gives you a little bit more detail. 193 194 00:16:27,850 --> 00:16:31,740 And it shows that the active side is actually the side between the thumb, 194 195 00:16:31,750 --> 00:16:34,680 so the circle in the middle, and the minimum value. 195 196 00:16:34,750 --> 00:16:37,500 So basically that's going to be the color on the left here. 196 197 00:16:37,900 --> 00:16:40,920 And then the inactive color is the one on the right. 197 198 00:16:40,930 --> 00:16:48,470 So let's go ahead and implement some of these properties so that we can bring our slider to life. So 198 199 00:16:48,560 --> 00:16:52,260 first things first, we're going to provide a value. Now, 199 200 00:16:52,280 --> 00:16:57,360 the value of our slider is going to be the height that the user selected. 200 201 00:16:57,590 --> 00:17:02,690 And we also want it to show up inside here, in this number. 201 202 00:17:02,690 --> 00:17:09,590 So let's go ahead and create another property for our class that's going to be a integer and it's 202 203 00:17:09,590 --> 00:17:15,040 going to be called height. And we'll start it off with a value of 180. 203 204 00:17:15,050 --> 00:17:20,570 Now this can't be final or constant because it's going to change all the time and every time the user 204 205 00:17:20,570 --> 00:17:27,260 slides on that slider. Instead of hardcoding 180 in here, we're going to be putting the value of the 205 206 00:17:27,260 --> 00:17:28,380 height in here. 206 207 00:17:28,580 --> 00:17:36,320 But because the text widget needs a string as the data, we can't simply just put a number in here. And 207 208 00:17:36,320 --> 00:17:40,280 it tells us that type it can't be assigned to type string. 208 209 00:17:40,310 --> 00:17:44,480 So let's go ahead and convert it by writing height.toString. 209 210 00:17:44,480 --> 00:17:49,670 So now we have a string that's converted from our number and you can always convert numbers to strings 210 211 00:17:50,000 --> 00:17:55,340 or ints to doubles. But if you want to do the opposite way then it's a little bit more tricky because 211 212 00:17:55,400 --> 00:17:58,990 A B and C don't map directly to a number right? 212 213 00:17:59,270 --> 00:18:00,760 But this is easy to do. 213 214 00:18:00,860 --> 00:18:08,480 So now for our slider, the value is going to be our height. And we're going to get another incompatible 214 215 00:18:08,480 --> 00:18:09,360 issue here. 215 216 00:18:09,410 --> 00:18:15,080 It's going to tell us that the argument type int can't be assigned to the parameter type double, and 216 217 00:18:15,080 --> 00:18:19,940 that's because the value is expecting a double to go in there as the value. 217 218 00:18:20,300 --> 00:18:27,040 So what we can do is we can use again our trusty .notation to change it to a double. 218 219 00:18:27,050 --> 00:18:30,400 So again super easy conversion and easily doable. 219 220 00:18:30,410 --> 00:18:34,510 So now our height is going to be displayed as the value of the slider. 220 221 00:18:34,550 --> 00:18:38,410 So that's the starting value basically. Now, 221 222 00:18:38,410 --> 00:18:45,040 next we're going to define the scale for our height and most people are not zero centimetres tall hopefully. 222 223 00:18:45,640 --> 00:18:49,690 I guess unless you're like a single celled organism or something, but they're probably not going to be 223 224 00:18:49,690 --> 00:18:50,870 using our app. 224 225 00:18:50,890 --> 00:19:01,430 So let's start off with a minimum value of maybe let's say 120, and maybe a max value of say, I don't know, 220 225 226 00:19:01,480 --> 00:19:01,760 right? 226 227 00:19:02,470 --> 00:19:07,930 So because we're in centimeters, we're talking about 1 meter 20 and 2 meter 20. You can obviously change 227 228 00:19:07,930 --> 00:19:10,440 your scale if this doesn't make sense at all. 228 229 00:19:10,450 --> 00:19:17,140 If all the people that you live around are like three meters tall, then feel free to change the scale. 229 230 00:19:17,140 --> 00:19:21,240 Again these are things that work really well as constants. 230 231 00:19:21,430 --> 00:19:25,540 So it might make sense to add it into our constants over here. 231 232 00:19:25,780 --> 00:19:30,460 But for now I'm just going to leave it in here to make it easy to see how our slider is being built 232 233 00:19:30,460 --> 00:19:30,640 out. 233 234 00:19:30,880 --> 00:19:35,860 But feel free to create a constant and you'll be able to easily hit up one file and update those constants 234 235 00:19:35,890 --> 00:19:37,330 if needed. 235 236 00:19:37,330 --> 00:19:40,720 So you can see that our slider is still being highlighted as yellow, 236 237 00:19:40,840 --> 00:19:45,600 and whenever that happens you also get a line in the right telling you what the warning is about. 237 238 00:19:45,620 --> 00:19:50,660 You also get a little yellow box telling you there's problems or there's warnings found. 238 239 00:19:50,800 --> 00:19:57,260 So in this case if we hover over it, it tells us that the parameter or the property onChanged is required. 239 240 00:19:57,460 --> 00:20:01,930 So at the moment, we're still missing one more property which is the onChanged. 240 241 00:20:02,170 --> 00:20:09,550 And you can see that just as I was typing over here, that it's telling us that it expect a function something 241 242 00:20:09,550 --> 00:20:13,780 that takes a double as an input and returns void. 242 243 00:20:13,810 --> 00:20:23,090 So we have to pass in a function into the slider just as we did for our reusableCard. In our onChanged, 243 244 00:20:23,180 --> 00:20:29,960 instead of just having an anonymous function with no inputs, it actually expects a double value in there. 244 245 00:20:30,470 --> 00:20:35,310 And that value is going to be the new value of the slider. 245 246 00:20:35,600 --> 00:20:41,450 So you can see that in the docs it tells us that the onChanged is called during a drag when the user is 246 247 00:20:41,450 --> 00:20:44,120 selecting a new value by dragging. 247 248 00:20:44,120 --> 00:20:50,960 And if we click on it to see more information, you can see that the slider will pass the new value to 248 249 00:20:50,960 --> 00:20:58,020 the callback and we will be able to tap into that new value inside the anonymous function. 249 250 00:20:58,040 --> 00:21:02,240 So this is how they're recommending us to use the onChanged property. 250 251 00:21:02,570 --> 00:21:09,620 So let's go ahead and do that. In our onChanged property, it's going to take a double. 251 252 00:21:09,940 --> 00:21:11,650 And we're going to call it newValue. 252 253 00:21:12,310 --> 00:21:19,690 And this is going to be the value that will be passed to us from the slider when the slider gets changed 253 254 00:21:19,690 --> 00:21:20,660 by the user. 254 255 00:21:20,890 --> 00:21:27,790 And then inside the curly braces here, we'll be able to tap into that newValue and we'll be able to use 255 256 00:21:27,790 --> 00:21:31,900 it for our purposes of changing the height. 256 257 00:21:31,900 --> 00:21:38,830 Now if we go ahead and hit save and when we run it, we can see it vaguely on screen but it's so dark 257 258 00:21:38,830 --> 00:21:41,620 that we actually can't differentiate it from the background. 258 259 00:21:41,980 --> 00:21:44,290 So let's go ahead and add some color to it. 259 260 00:21:44,530 --> 00:21:50,530 Let's add an activeColor which is going to be a custom color as usual. 260 261 00:21:50,530 --> 00:22:00,940 And in this case it's going to be 0xFFEB1555, and this will turn the left side pink and let's 261 262 00:22:00,940 --> 00:22:04,260 go ahead and add a color for the right side as well. 262 263 00:22:04,270 --> 00:22:15,130 So the inactiveColor and this is going to be 0xFF8D8E98. 263 264 00:22:16,040 --> 00:22:20,070 So now we have a gray on the right and a pink on the left. 264 265 00:22:20,090 --> 00:22:27,050 Now at this stage something very curious happens. When I click onto the handle of my slider and I drag 265 266 00:22:27,050 --> 00:22:33,560 it around, because we're going to print the new value that the slider is at, we're going to see it inside 266 267 00:22:33,560 --> 00:22:34,530 our console. 267 268 00:22:34,610 --> 00:22:41,060 So you'll see the value change and I can go all the way down to the very bottom which is 120 and go 268 269 00:22:41,060 --> 00:22:43,880 back up to 220. 269 270 00:22:44,000 --> 00:22:50,840 But the slider itself is not really doing anything on screen right? It's not animating, it's not moving 270 271 00:22:50,840 --> 00:22:56,450 at all. It looks like it's completely stuck. If it wasn't for all of these readings that I'm getting by 271 272 00:22:56,450 --> 00:23:01,370 printing the new value, I would have thought that my slider is completely not working. 272 273 00:23:01,370 --> 00:23:02,360 Why is this? 273 274 00:23:03,260 --> 00:23:09,590 Well if we head into the documentation and we look at the onChanged property, you can see that it says 274 275 00:23:09,590 --> 00:23:13,910 that the slider passes the new value to the callback. 275 276 00:23:13,940 --> 00:23:22,490 So when I move my toggle around, it will pass the new value into my callback through this input here. 276 277 00:23:22,490 --> 00:23:29,480 So I get access to that new value which is being printed inside my console. But it does not actually 277 278 00:23:29,480 --> 00:23:37,090 change the state of my slider until the parent widget rebuilds the slider with the new value. 278 279 00:23:37,100 --> 00:23:40,370 So how do we rebuild the parents slider with the new value? 279 280 00:23:40,370 --> 00:23:41,970 Well the callback, 280 281 00:23:42,080 --> 00:23:50,380 so everything in between these curly braces, should update the status of the parent using set state. 281 282 00:23:50,660 --> 00:23:54,440 So the parent gets rebuilt in the way that they've shown here. 282 283 00:23:54,440 --> 00:24:01,910 So for example here in their onChanged, they have a new value being passed into this callback here and 283 284 00:24:01,910 --> 00:24:09,820 then that new value is used to update the value of the slider which is right here. 284 285 00:24:09,830 --> 00:24:13,660 So this is how we're able to update the appearance of the slider. 285 286 00:24:13,670 --> 00:24:22,160 Let's give it a go in our code as well and actually get our slider moving and animating. Instead of printing 286 287 00:24:22,160 --> 00:24:23,240 the new value, 287 288 00:24:23,300 --> 00:24:31,930 we're going to set the height property which is a mutable or a variable property that we have in here. 288 289 00:24:32,210 --> 00:24:35,580 And it's also an integer which starts out at 180. 289 290 00:24:35,810 --> 00:24:42,650 Once the slider is changed, we're going to set the height to equal the new value. 290 291 00:24:42,980 --> 00:24:45,610 But the problem is that new value is a double 291 292 00:24:45,650 --> 00:24:47,900 but height is an integer. 292 293 00:24:47,910 --> 00:24:55,740 So let's go ahead and round it up to an integer so that can be turned into the nearest whole number. 293 294 00:24:56,160 --> 00:25:04,890 And then all we have to do is call set state and make sure that the state of our slider gets updated 294 295 00:25:04,950 --> 00:25:06,750 based on this height. 295 296 00:25:06,750 --> 00:25:13,800 Now if we hit save and we check out our handiwork, then you can see our slider's moving and our text which 296 297 00:25:13,800 --> 00:25:17,640 displays the height also, is also updating. 297 298 00:25:17,640 --> 00:25:18,360 Perfect. 298 299 00:25:18,360 --> 00:25:23,100 We've got all of our functionality of our slider card now enabled. 34697

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.