All language subtitles for 16. Adding the BMI Calculator Functionality

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,810 --> 00:00:06,450 Now that all of our design is complete, we're ready to actually write the functionality. 1 2 00:00:06,480 --> 00:00:12,210 And as I mentioned in the beginning, the design is really the hardest part of this app and the functionality 2 3 00:00:12,510 --> 00:00:16,260 of actually calculating the BMI is in fact the easiest. 3 4 00:00:16,290 --> 00:00:22,890 Let's get started by creating a new Dart file which is going to contain the functionality of calculating 4 5 00:00:22,890 --> 00:00:23,490 the BMI. 5 6 00:00:23,610 --> 00:00:32,320 So let's call it maybe the calculator_brain. And in this file we don't need any material components, but 6 7 00:00:32,350 --> 00:00:34,960 what we are going to create is a new class. 7 8 00:00:34,980 --> 00:00:43,840 And it's going to be called the CalculatorBrain. And this class is going to have two properties. 8 9 00:00:43,890 --> 00:00:47,880 We're going to pass it a height which is going to be an integer 9 10 00:00:48,180 --> 00:00:56,650 and we're also going to pass it a weight. Now the way that we're gonna pass over these properties is 10 11 00:00:56,650 --> 00:00:59,140 of course when we construct the calculator brain. 11 12 00:00:59,290 --> 00:01:06,980 So let's create a constructor with the height and the weight. 12 13 00:01:07,310 --> 00:01:12,440 So now that we have a constructor, we'll be able to supply the values for the height and weight when 13 14 00:01:12,440 --> 00:01:19,310 we create a new object from the calculator brain. And we can now start writing some of the functionality. 14 15 00:01:19,400 --> 00:01:24,710 The first thing we'll need is to be able to calculate the actual BMI. 15 16 00:01:25,020 --> 00:01:32,010 Now the BMI stands for the body mass index. And it's a way of normalising somebody's weight against their 16 17 00:01:32,010 --> 00:01:33,000 height. 17 18 00:01:33,030 --> 00:01:41,700 So the formula for calculating the BMI is the mass or weight in kilograms divided by the height in meters 18 19 00:01:41,820 --> 00:01:42,450 squared. 19 20 00:01:42,960 --> 00:01:47,640 But because our height is currently in centimeters, so we might have to do a little bit of adjustment here. 20 21 00:01:49,160 --> 00:01:55,690 Let's go ahead and create our function which is going to return the BMI as a string and we're going 21 22 00:01:55,690 --> 00:02:05,130 to call it maybe calculateBMI. And this is going to take no inputs because we'll be able to really have 22 23 00:02:05,130 --> 00:02:09,530 access to everything we need height and weight when the calculator brain is initialized. 23 24 00:02:10,080 --> 00:02:15,960 But it is going to do some work and the work that it's going to do is it's going to calculate the BMI 24 25 00:02:16,020 --> 00:02:24,210 which is going to be a double. And it's going to record the BMI and it's going to be equal to the weight 25 26 00:02:24,450 --> 00:02:29,760 which is already in kilograms divided by the height squared. 26 27 00:02:29,760 --> 00:02:37,370 So as we saw earlier, the easiest way of squaring a number is by using the Dart math library. 27 28 00:02:37,410 --> 00:02:44,850 So let's import that so that we can use the power function which takes a number and an exponent. So we're 28 29 00:02:44,850 --> 00:02:51,880 able to provide the height and then we're able to provide the power, which in this case, is to to be up 29 30 00:02:51,890 --> 00:02:53,810 to square the height. 30 31 00:02:53,820 --> 00:02:57,470 Now remember I mentioned earlier that our height is in centimeters. 31 32 00:02:57,960 --> 00:03:03,210 So in order to convert it to meters, we have to divide it by 100. 32 33 00:03:03,210 --> 00:03:07,990 So now we have our BMI calculated from this super simple formula, 33 34 00:03:08,190 --> 00:03:11,070 we can now use it and convert it into a string. 34 35 00:03:11,700 --> 00:03:18,720 So at this point in time, the BMI is going to be a super long value because we said it's a double. Now 35 36 00:03:19,010 --> 00:03:23,740 all that we want in our app is actually just a single decimal place. 36 37 00:03:23,760 --> 00:03:29,430 So 18.something but not like a million lines long because it's so large it's gonna go off 37 38 00:03:29,430 --> 00:03:36,810 the screen and nobody needs that degree of accuracy. In order to convert our double into a single decimal 38 39 00:03:36,810 --> 00:03:40,140 place and also convert it into a string, 39 40 00:03:40,150 --> 00:03:52,030 there's a really convenient method. So we can write BMI.toStringAsFixed and this returns a decimal 40 41 00:03:52,030 --> 00:03:58,870 point string and we can specify how many decimal places we want as the input. 41 42 00:03:58,870 --> 00:04:07,150 So let's use that method and say that we only want one decimal place and we can output this value using 42 43 00:04:07,180 --> 00:04:08,780 the return keyword. 43 44 00:04:08,800 --> 00:04:15,430 So now our calculateBMI is able to calculate the BMI based on the weight and the height and it's also 44 45 00:04:15,430 --> 00:04:21,400 able to convert it to a single decimal point value and return it as a string. 45 46 00:04:21,460 --> 00:04:26,500 The next thing that we want is to be able to provide a result based off that BMI. 46 47 00:04:26,500 --> 00:04:31,660 So we're going to create another method that is going to return a string which is going to be the result 47 48 00:04:31,940 --> 00:04:34,670 and we're gonna call the method getResult. 48 49 00:04:34,690 --> 00:04:45,040 So again it takes no inputs but it's going to check inside this method if the BMI is greater or equal 49 50 00:04:45,190 --> 00:04:51,880 to 25 in which case it will maybe return the words Overweight. 50 51 00:04:54,660 --> 00:04:58,620 And then it's going to check else if it's not greater than 25, 51 52 00:04:58,650 --> 00:05:03,450 well then is it maybe greater than 18.5. 52 53 00:05:03,450 --> 00:05:09,440 Well in that case then the results should be normal. 53 54 00:05:09,670 --> 00:05:15,730 And finally for all the results that are lower than 18.5, we're simply going to return 54 55 00:05:15,940 --> 00:05:19,790 Underweight. 55 56 00:05:20,030 --> 00:05:26,540 Now these interpretations are based off most of the common BMI charts which you can take a look at on 56 57 00:05:26,540 --> 00:05:28,890 Wikipedia or elsewhere if you wish. 57 58 00:05:28,940 --> 00:05:34,140 So at the moment, we don't have access to BMI though which is why I'm getting this error. 58 59 00:05:34,160 --> 00:05:41,260 It doesn't know about BMI because the BMI that's calculated in here is limited to these curly braces. 59 60 00:05:41,270 --> 00:05:43,730 It's only visible locally inside here. 60 61 00:05:44,210 --> 00:05:49,310 But if we wanted to get access to it, we need to make it visible to this method. 61 62 00:05:49,310 --> 00:05:52,910 So let's go ahead and create a private property up here. 62 63 00:05:52,940 --> 00:05:58,880 So we're going to create it as a double and it's going to be a private variable. So we're going to add 63 64 00:05:58,880 --> 00:06:04,610 the underscore beforehand and it's going to be equal to nothing to begin with. 64 65 00:06:04,790 --> 00:06:11,750 And then when we calculate our BMI, we're going to assign it to that value and then we can return it 65 66 00:06:11,840 --> 00:06:13,320 as the string as well. 66 67 00:06:13,460 --> 00:06:20,120 And we're now able to use it in all the places where we need that BMI. But remember because this is a 67 68 00:06:20,120 --> 00:06:26,420 private value, we won't be able to access it from any other class other than the calculator brain because 68 69 00:06:26,690 --> 00:06:32,180 frankly it's nobody's business trying to get access to the BMI or trying to change it. If they need it 69 70 00:06:32,210 --> 00:06:37,940 they can get it through this method which we know only returns it in the way that we want it to, which 70 71 00:06:37,940 --> 00:06:44,570 is a single decimal point. But inside our CalculatorBrain we now have free access to it and we can use 71 72 00:06:44,570 --> 00:06:49,980 it to calculate the result but we can also use it to give an interpretation. 72 73 00:06:50,000 --> 00:06:58,090 So let's create another method called getInterpretation and this is going to perform the same three 73 74 00:06:58,090 --> 00:06:58,510 checks. 74 75 00:06:58,510 --> 00:07:00,680 So I'm just going to copy and paste it in here. 75 76 00:07:00,970 --> 00:07:06,280 But instead of just returning overweight, normal, underweight, we want to be able to give the user a little 76 77 00:07:06,280 --> 00:07:12,490 bit of feedback about what their weight means. And maybe a little bit of advice if you really wanted 77 78 00:07:12,490 --> 00:07:19,500 to. To save you from watching me type for an hour, I'm simply going to copy and paste some interpretations 78 79 00:07:19,530 --> 00:07:21,000 that I've written earlier. 79 80 00:07:21,150 --> 00:07:26,690 You can either use these ones or you can make up your own. It's your app so do it your way. 80 81 00:07:26,820 --> 00:07:33,180 But now we have three methods inside our calculator brain, one that will give us the BMI, one that will 81 82 00:07:33,180 --> 00:07:39,230 give us the results of the BMI and one that will give us an interpretation of the BMI. 82 83 00:07:39,630 --> 00:07:45,460 So we're now ready to go and we're ready to use that class CalculatorBrain. 83 84 00:07:45,480 --> 00:07:53,460 So let's go ahead and import the calculator brain into our current file and we can collapse all of those 84 85 00:07:53,550 --> 00:08:00,720 imports after we're done. And the time when we need it is the moment when the user clicks on the calculate 85 86 00:08:00,720 --> 00:08:01,680 button. 86 87 00:08:01,680 --> 00:08:09,450 So inside the onTap is where we're going to initialize a new calculator brain object. And I'm just gonna 87 88 00:08:09,480 --> 00:08:16,080 call it calc to keep it short. And it's going to be initialized using the constructor that we built earlier 88 89 00:08:16,080 --> 00:08:16,630 on. 89 90 00:08:16,650 --> 00:08:22,830 So it's going to expect a height, which will be the height that we got from our slider here, 90 91 00:08:22,830 --> 00:08:26,930 and also the weight that we've got from our input page as well. 91 92 00:08:27,240 --> 00:08:32,680 So let's set weight equal to the local weight. 92 93 00:08:35,530 --> 00:08:42,730 So now that we've created our calculator brain and we can now get the calculation for the BMI, get the 93 94 00:08:42,730 --> 00:08:49,000 interpretation, get the result, we want to be able to pass that over to the next page which is the result 94 95 00:08:49,000 --> 00:08:49,870 page. 95 96 00:08:49,870 --> 00:08:57,210 So if we go over to the result page, at the moment it has no properties that we can access from the outside. 96 97 00:08:57,370 --> 00:09:03,490 But in order for us to pass some data over when we navigate over here, we have to create some of those 97 98 00:09:03,490 --> 00:09:04,170 properties. 98 99 00:09:04,210 --> 00:09:12,400 So let's create a final property that is going to be called bmiResult and this is going to be a string. 99 100 00:09:13,420 --> 00:09:18,440 And then let's create another one that's going to be called the resultText. 100 101 00:09:19,750 --> 00:09:27,840 And finally we'll have one that's going to be called the interpretation. And again we have our final 101 102 00:09:27,840 --> 00:09:30,450 properties which will need initialization. 102 103 00:09:30,480 --> 00:09:38,720 So let's go ahead and create our results page constructor and I'm gonna mark all of them as required 103 104 00:09:38,730 --> 00:09:43,640 because frankly in order to load up the results page and make it not look weird, 104 105 00:09:43,680 --> 00:09:47,030 we have to make sure it has both the BMI result, 105 106 00:09:47,040 --> 00:09:50,350 the result text as well as the interpretation. 106 107 00:09:50,430 --> 00:09:53,710 So let's mark all three as required. 107 108 00:09:53,910 --> 00:09:56,670 We've got this.interpretation, 108 109 00:09:59,250 --> 00:10:07,000 this.bmiResult and this.resultText. 109 110 00:10:07,340 --> 00:10:15,380 So now when we create our results page in the moment when we want to navigate over, we can now pass over 110 111 00:10:15,620 --> 00:10:18,680 the values that we get from our calculator brain. 111 112 00:10:18,680 --> 00:10:21,590 So we've got three properties that we can initialize. 112 113 00:10:21,590 --> 00:10:24,330 One is the BMI result, 113 114 00:10:24,350 --> 00:10:26,870 another is the result text 114 115 00:10:26,870 --> 00:10:29,120 and finally we've got our interpretation. 115 116 00:10:29,780 --> 00:10:37,610 So the BMI result is going to be equal to calc using the calculator brain to calculate the BMI. 116 117 00:10:37,790 --> 00:10:44,750 And remember this will return a string which is going to go straight into that property BMI result over 117 118 00:10:44,750 --> 00:10:49,050 here and we're going to be able to use it inside our build method. 118 119 00:10:49,100 --> 00:10:56,690 Now the next one is going to be calculated using the getResult method and the final one is going to 119 120 00:10:56,690 --> 00:11:00,140 rely on the getInterpretation method. 120 121 00:11:00,140 --> 00:11:06,260 So now that we've got all those strings from our calculator brain, we can go ahead and use it in our 121 122 00:11:06,260 --> 00:11:07,460 build method. 122 123 00:11:07,460 --> 00:11:16,260 So instead of having these hardcoded piece of text, we can go ahead and actually add the result text, 123 124 00:11:16,700 --> 00:11:22,430 the BMI results and also the interpretation. 124 125 00:11:25,130 --> 00:11:31,670 Now the final thing I'm going to do just to keep our style consistent with the one here, is I'm going 125 126 00:11:31,670 --> 00:11:39,050 to capitalize all the words for the result text here. And as always there's always a simple way of doing 126 127 00:11:39,050 --> 00:11:39,570 this. 127 128 00:11:39,680 --> 00:11:46,760 And the one in this case is called toupperCase and this simply turns all the characters in the string 128 129 00:11:46,820 --> 00:11:50,960 to uppercase which is what we need. Now, 129 130 00:11:50,980 --> 00:11:59,590 I'm going to stop my app and I'm going to run it from scratch so it starts from the beginning. 130 131 00:11:59,630 --> 00:12:02,510 So now let's test out our BMI calculator. 131 132 00:12:02,690 --> 00:12:08,870 We know from coding it up and also from Wikipedia that the gender doesn't really matter. But we might 132 133 00:12:08,870 --> 00:12:16,520 be using it in another part of our app, so we can select our agenda and it will activate our cards. And 133 134 00:12:16,580 --> 00:12:25,460 we can also select our height so my height is 180 and let's adjust our weight and let's adjust our age 134 135 00:12:26,270 --> 00:12:28,730 and go ahead and click calculate. 135 136 00:12:28,730 --> 00:12:35,930 So it now calculates my BMI, gives me a result telling me that it's normal, and it tells me an interpretation 136 137 00:12:36,080 --> 00:12:43,500 of my BMI. My BMI is rounded to one decimal place and it's displayed as this giant number. 137 138 00:12:43,520 --> 00:12:49,040 Now if we wanted to try it again with maybe a little bit lower height, a little bit more weight, let's 138 139 00:12:49,040 --> 00:12:54,140 click calculate and you can see we get a different result and a different interpretation. 139 140 00:12:54,140 --> 00:13:00,950 In this module, we've learnt a lot of things and it might be worth reviewing some of those key concepts 140 141 00:13:01,160 --> 00:13:07,790 especially if you're new to Dart programming. But I hope you had fun building this beautiful BMI app with 141 142 00:13:07,790 --> 00:13:14,960 me and I hope you're inspired also to take your Flutter to designs to the next level using all the various 142 143 00:13:14,960 --> 00:13:19,130 ways of customizing widgets for your UI designs. 143 144 00:13:19,890 --> 00:13:21,660 So that's all from me in this module. 144 145 00:13:21,740 --> 00:13:22,700 I see on the next one. 17018

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