All language subtitles for 19. Connecting Functions & Buttons

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: 1 00:00:02,260 --> 00:00:09,610 So whilst we can certainly improve the styling, I first of all want to improve the behavior of the buttons. 2 00:00:09,610 --> 00:00:12,370 Right now, they're not doing anything when we press them and 3 00:00:12,370 --> 00:00:15,600 that happens because we haven't wired them up to anything. 4 00:00:15,820 --> 00:00:21,130 Now onPressed here takes a function, that's important, 5 00:00:21,130 --> 00:00:24,810 the value of onPressed is a function that returns void, 6 00:00:24,820 --> 00:00:26,230 this is what this here means. Now 7 00:00:26,290 --> 00:00:28,630 previously, for example on title here, 8 00:00:28,630 --> 00:00:30,950 we saw that title wanted a widget. 9 00:00:31,150 --> 00:00:33,890 If we hover over onPressed, we see this strange thing 10 00:00:34,020 --> 00:00:36,980 and that simply means a function that takes no arguments 11 00:00:37,060 --> 00:00:39,850 and doesn't return anything but a function, 12 00:00:39,850 --> 00:00:47,320 this here is basically the type definition for a function, arguments and return value. 13 00:00:47,490 --> 00:00:49,770 So onPressed takes a function, 14 00:00:49,770 --> 00:00:52,690 now we got a couple of different options here. 15 00:00:52,800 --> 00:00:57,540 We can add a new function outside of the class, 16 00:00:57,540 --> 00:00:59,300 that's a bad idea. 17 00:00:59,310 --> 00:01:07,080 Your object, so your classes, should always work standalone, so everything that belongs to a widget should 18 00:01:07,080 --> 00:01:13,380 go into the same class, all the data a widget uses and so on should belong into the same class so that 19 00:01:13,380 --> 00:01:19,710 the widget is a standalone unit. So we can add a function here into our class, 20 00:01:19,710 --> 00:01:25,920 you can do that because you can add functions to classes, they are then called methods but it's a normal 21 00:01:25,920 --> 00:01:27,210 function. 22 00:01:27,210 --> 00:01:33,580 So here we want a function that takes no arguments and does not return anything, 23 00:01:33,580 --> 00:01:43,240 so here in MyApp, we can add void and then any name you want, like answerQuestion, 24 00:01:43,240 --> 00:01:49,130 typically your function names are descriptions of what happened or what happens. 25 00:01:49,150 --> 00:01:52,110 So here, we got our answerQuestion function, 26 00:01:52,120 --> 00:01:55,090 it doesn't take any arguments, it returns nothing and in there, 27 00:01:55,090 --> 00:02:00,960 let's simply print answer chosen. 28 00:02:01,220 --> 00:02:03,800 Now that's the function defined here, 29 00:02:03,800 --> 00:02:04,660 how do we now 30 00:02:04,670 --> 00:02:06,130 wire this up to onPressed? 31 00:02:06,950 --> 00:02:07,890 Well it's simple right, 32 00:02:07,910 --> 00:02:12,300 we call answerQuestion here, like this, 33 00:02:12,300 --> 00:02:15,150 add parentheses and we're good. 34 00:02:15,150 --> 00:02:17,480 Well we already see that we get an error here, 35 00:02:17,610 --> 00:02:21,150 the expression here has a type of void and therefore cannot be used. 36 00:02:21,840 --> 00:02:23,390 Does that make sense? 37 00:02:23,470 --> 00:02:26,850 Shouldn't it return void, isn't that what I just said? 38 00:02:26,860 --> 00:02:32,200 The problem here is and that is now something which I know that it can be confusing but it's super important 39 00:02:32,200 --> 00:02:33,130 to understand, 40 00:02:33,130 --> 00:02:36,520 so I'll explain this in very clear and slow steps. 41 00:02:36,520 --> 00:02:43,330 The problem is that here, I'm executing answerQuestion, sounds all right, right? 42 00:02:43,330 --> 00:02:50,200 I want to execute it when onPressed is clicked. However, you have to remember that Dart parses your file 43 00:02:50,530 --> 00:02:56,920 from top to bottom, left and right when it in the end starts your application. Now there, it then runs 44 00:02:56,920 --> 00:03:02,080 the app, builds the MyApp widget and that means the build method gets called and in the build method, Dart 45 00:03:02,110 --> 00:03:06,700 now goes through the entire method to find out what to build and it sees 46 00:03:06,730 --> 00:03:10,840 okay I build a MaterialApp and there I have a scaffold, appBar, 47 00:03:10,840 --> 00:03:14,710 text column and so on, Dart goes through all of that with the help of Flutter, 48 00:03:14,710 --> 00:03:20,680 it's able to bring that onto the screen but then it encounters the RaisedButton. The RaisedButton is configured 49 00:03:20,710 --> 00:03:26,620 to have a child, which is this text and then for onPressed, what you're saying to Dart with this syntax 50 00:03:26,620 --> 00:03:34,270 is onPressed, the value for onPressed, should be what answerQuestion returns. 51 00:03:34,420 --> 00:03:35,940 Let's have a look at DartPad again. 52 00:03:36,010 --> 00:03:45,600 There earlier, what I did is I added addNumbers and then down here, I store the result of addNumbers in 53 00:03:45,600 --> 00:03:47,090 first result, right, 54 00:03:47,100 --> 00:03:50,320 what addNumbers returns and I do this by executing addNumbers, 55 00:03:50,340 --> 00:03:55,860 this then executes this code and returns that value and therefore I can store that returned value here 56 00:03:55,920 --> 00:04:02,610 in first result and here it's the same. By adding parentheses, this gets executed as soon as Dart 57 00:04:02,610 --> 00:04:09,090 encounters this, not when the user presses the button but when Dart goes over that code to set up the 58 00:04:09,150 --> 00:04:16,110 RaisedButton. So it executes answerQuestion and now it expects to get back the value which should be passed 59 00:04:16,110 --> 00:04:23,190 to the RaisedButton as a value for onPressed but onPressed wants a function, answerQuestion on the 60 00:04:23,190 --> 00:04:27,730 other hand doesn't return anything because this is a function that doesn't return anything 61 00:04:27,840 --> 00:04:34,920 and since we now pass the returned value to onPressed, we return nothing to onPressed but onPressed wants a 62 00:04:34,920 --> 00:04:41,940 function. So we're not passing this function here to onPressed but the return value of this function 63 00:04:42,150 --> 00:04:49,070 because by adding parentheses, this gets executed when Dart tries to build that button. 64 00:04:49,110 --> 00:04:50,930 Of course, this is not what we want. 65 00:04:51,120 --> 00:04:52,330 We want to execute 66 00:04:52,340 --> 00:04:55,290 answerQuestion when the user presses the button, 67 00:04:55,320 --> 00:05:00,850 that's why we want to pass a pointer at answerQuestion to onPressed. 68 00:05:00,960 --> 00:05:03,580 Now consider this real life example - 69 00:05:03,810 --> 00:05:11,070 if you have a TV and you can turn a TV on with your remote by pressing a button on your remote, then 70 00:05:11,070 --> 00:05:16,470 you could say turning the TV on, that that's what happens in a function, that function could be named 71 00:05:16,620 --> 00:05:22,260 turnOn and you execute that function by pressing the button on your remote. 72 00:05:22,260 --> 00:05:28,260 Now if I give you the remote for the TV, you can press that button whenever you want and that turns on 73 00:05:28,260 --> 00:05:29,350 the TV. 74 00:05:29,400 --> 00:05:36,930 It would be bad if I pass you the remote and simultaneously, I pressed the button for you because then, 75 00:05:37,350 --> 00:05:41,520 I would take the option of turning the TV on away from you and 76 00:05:41,520 --> 00:05:42,740 that's what we're doing here. 77 00:05:42,780 --> 00:05:48,770 We're essentially executing the function at the point of time we want to give it to Flutter. 78 00:05:48,900 --> 00:05:52,860 So the solution for all that is that we remove the parentheses here, 79 00:05:52,860 --> 00:06:01,200 by doing that, we're passing a pointer at that function, to Flutter, to onPressed. We're 80 00:06:01,200 --> 00:06:08,490 telling onPressed hey this is the name of the function which you should execute when the user presses 81 00:06:08,490 --> 00:06:09,450 the button, 82 00:06:09,450 --> 00:06:16,050 so we're passing the name of the function to onPressed into the RaisedButton and not the result of the 83 00:06:16,050 --> 00:06:17,510 function, name 84 00:06:17,550 --> 00:06:18,750 instead of result, 85 00:06:18,870 --> 00:06:26,680 super important. So here, we have to use answer passed question without parentheses and you always need 86 00:06:26,680 --> 00:06:34,000 to use it without parentheses when you just want to give the name of the function to some argument or 87 00:06:34,000 --> 00:06:35,690 whatever it is in your code and 88 00:06:35,710 --> 00:06:39,140 we'll see this occur over and over throughout the course. 89 00:06:39,190 --> 00:06:46,360 So here we can do the same for answerQuestion down there, on the second RaisedButton and of course also 90 00:06:46,360 --> 00:06:52,570 on the third RaisedButton, always without parentheses to just point at the function instead of executing 91 00:06:52,570 --> 00:06:56,010 it. With that if we go back, now the button is enabled, 92 00:06:56,010 --> 00:07:02,070 it's not dark greyish anymore and we can press it and I pressed each button now and therefore here 93 00:07:02,130 --> 00:07:07,800 in our debug console, we now see answer chosen, answer chosen, answer chosen and that's coming from that 94 00:07:07,800 --> 00:07:13,350 print statement here which simply is a system output which we don't see in the app but we can use 95 00:07:13,350 --> 00:07:18,600 it during development to see some log here in our debug console. Of course this is not a feature you would 96 00:07:18,600 --> 00:07:21,820 use in a real app because the user doesn't see it 97 00:07:21,870 --> 00:07:24,630 but for developing an app, it's super useful. 98 00:07:24,630 --> 00:07:28,320 So our buttons work and we execute this function when we press it. 10347

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