Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,400 --> 00:00:07,970
Now this way of hooking up a button to a function is one of two possible ways.
2
00:00:08,070 --> 00:00:11,760
We have our function here, our named function inside of stateless widget
3
00:00:12,360 --> 00:00:15,490
and then we connect here to onPressed.
4
00:00:15,720 --> 00:00:20,040
Now another way of solving this would be to use an anonymous function,
5
00:00:20,040 --> 00:00:22,830
this here is a named function because it has a name.
6
00:00:22,860 --> 00:00:27,750
Now if you have a function which you really only need here in this one place and you're never calling
7
00:00:27,750 --> 00:00:32,760
it from anywhere else in the application which is already not the case here because we have three buttons
8
00:00:32,760 --> 00:00:33,720
that use the function
9
00:00:33,720 --> 00:00:39,810
but let's say you only had one button, then instead of using a named function which you can always do
10
00:00:39,850 --> 00:00:46,770
but instead of doing that, you can add a function right here. Now without a name but you simply add parentheses
11
00:00:47,010 --> 00:00:50,450
to get any possible arguments, in this case you don't get any though,
12
00:00:50,460 --> 00:00:56,870
so it's an empty pair of parentheses and then either that one line expressions syntax with an arrow function
13
00:00:57,300 --> 00:00:58,890
and then the code you want to execute,
14
00:00:58,890 --> 00:01:03,780
so here answer 2 chosen for example or if I do the same on answer 3,
15
00:01:03,840 --> 00:01:09,930
you can also add curly braces for a possibly longer function body.
16
00:01:09,930 --> 00:01:18,130
So here, you could now do some stuff and then in the end, print answer 3 chosen, like this
17
00:01:21,940 --> 00:01:24,330
and here I had one parentheses too much.
18
00:01:24,330 --> 00:01:32,110
Now if I save that, you will see that if I click answer 1, 2, 3 here, we see answer chosen,
19
00:01:32,130 --> 00:01:35,600
that's from the answer 1 button which executed our name,
20
00:01:35,610 --> 00:01:42,240
answerQuestion function, then we had answer 2 chosen which is coming from my anonymous function here
21
00:01:42,620 --> 00:01:45,740
and answer 3 chosen which is coming from the function here
22
00:01:45,870 --> 00:01:48,700
and it's called anonymous because it has no name here,
23
00:01:48,720 --> 00:01:52,400
it just has the argument list and then the function body.
24
00:01:52,560 --> 00:01:57,030
Now an anonymous function always is a good idea if you never need to call it from anywhere else in the
25
00:01:57,030 --> 00:02:00,530
code because you can't call it from anywhere else, it has no name
26
00:02:00,780 --> 00:02:03,660
but you don't need to define it up here
27
00:02:03,660 --> 00:02:06,020
if you only need it in one place.
28
00:02:06,060 --> 00:02:10,710
So these are two function syntaxes which you will also see throughout the course, please note that this
29
00:02:10,710 --> 00:02:13,050
also is not executed immediately,
30
00:02:13,050 --> 00:02:15,050
this is just the function definition,
31
00:02:15,090 --> 00:02:19,320
I didn't add parentheses after that which would execute it immediately,
32
00:02:19,320 --> 00:02:25,740
instead I just defined my function here with the arguments in the function body and I pass that definition
33
00:02:25,980 --> 00:02:31,770
of the function to onPressed, which therefore ensures that the RaisedButton can execute the function when
34
00:02:31,770 --> 00:02:39,110
the user really pressed the button. So that's a lot of fun with buttons and functions and it's a crucial
35
00:02:39,110 --> 00:02:45,110
building block of Flutter applications because in your typical application of course, you do interact
36
00:02:45,110 --> 00:02:49,940
with the user and you want to give the user some chance of interacting with the application
37
00:02:49,970 --> 00:02:54,140
and that requires exactly what we learned here.
38
00:02:54,140 --> 00:02:57,790
Nonetheless right now, we're really just doing some behind the scenes stuff right,
39
00:02:57,800 --> 00:03:03,560
we're really just printing something here and that's nice for debugging so that we see that the buttons
40
00:03:03,560 --> 00:03:09,410
really are pressed but it would be nicer to change something on the screen before we then also later
41
00:03:09,410 --> 00:03:14,510
work on the general layout and the look of this and that is exactly what we'll do next.
42
00:03:14,510 --> 00:03:18,260
Let's make sure that something changes on the screen when we press one of these buttons.
4768
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.