Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,640 --> 00:00:08,790
Now what have we added this ternary expression, we covered a lot of the core fundamentals of both Dart
2
00:00:08,880 --> 00:00:09,770
and Flutter.
3
00:00:09,810 --> 00:00:11,420
Now obviously there's way more to learn,
4
00:00:11,550 --> 00:00:14,790
otherwise there wouldn't be more course content left
5
00:00:14,790 --> 00:00:18,060
but it's an important first step.
6
00:00:18,090 --> 00:00:23,730
This ternary expression down there however could be kind of difficult to read. If you will have a look
7
00:00:23,730 --> 00:00:28,240
at this, it might not be obvious on first side what's going on here,
8
00:00:28,350 --> 00:00:34,320
especially because we have this quite complex column in between and to be honest, it isn't even that
9
00:00:34,320 --> 00:00:35,180
complex.
10
00:00:35,250 --> 00:00:41,490
You can and you will throughout this course have way more complex widget structures than this column here
11
00:00:41,760 --> 00:00:50,780
and what could we do about that to make that more readable? If you want to make your widget tree more
12
00:00:50,840 --> 00:00:52,950
readable and more manageable,
13
00:00:52,970 --> 00:00:59,360
one thing that should always come to your mind is splitting that. You can of course split your widgets
14
00:00:59,360 --> 00:01:00,380
into sub-widgets,
15
00:01:00,380 --> 00:01:03,640
the question always is, should you split it?
16
00:01:03,680 --> 00:01:11,030
Well, let's assume you have a bigger widget with a lot of different nested widgets which you pass in
17
00:01:11,030 --> 00:01:17,160
to each other and so on. Then of course you can always split this into a separate sub-widgets which take
18
00:01:17,160 --> 00:01:20,720
these individual tasks and the question is, should you do that?
19
00:01:20,870 --> 00:01:22,020
Now in general
20
00:01:22,020 --> 00:01:25,250
and that's really something you can memorize, in Flutter,
21
00:01:25,250 --> 00:01:30,530
it's always encouraged to create more than less widgets.
22
00:01:30,530 --> 00:01:36,410
Of course you should not wrap every single built-in widget into your own custom widget and you will
23
00:01:36,410 --> 00:01:40,730
get a feeling for when the right point to split your widgets is there
24
00:01:41,060 --> 00:01:46,870
but overall, it's better to have smaller widgets than large widgets.
25
00:01:46,880 --> 00:01:54,950
It's better to have a readable code than unreadable, really entangled code It's better because it makes
26
00:01:54,950 --> 00:01:58,370
your code easier to manage for you and your fellow developers
27
00:01:58,370 --> 00:02:00,900
but it's also better for performance,
28
00:02:01,010 --> 00:02:03,660
that is something I'll dive in later though.
29
00:02:03,740 --> 00:02:11,210
So in our application which we have here, it might be worth considering putting the column here but also
30
00:02:11,240 --> 00:02:14,170
the center widget into a separate widget.
31
00:02:14,170 --> 00:02:19,120
Now of course, the center widget is not really complex at all and it would be fine to leave it here
32
00:02:19,280 --> 00:02:22,740
but I do plan on adding more content to it.
33
00:02:22,940 --> 00:02:33,190
So therefore, I will add two new widgets here - quiz.dart and result.dart. Now in my
34
00:02:33,220 --> 00:02:43,020
quiz.dart file, I'll create a stateless widget, quiz, where I import the Flutter material.dart file as always
35
00:02:43,710 --> 00:02:50,310
and then we can take this column which we have in the main.dart file, grab it and add it here instead
36
00:02:50,310 --> 00:02:54,210
of that container as a return value for the build method.
37
00:02:54,210 --> 00:02:59,190
Now of course in here, we're referring to the question and to the answer widgets,
38
00:02:59,220 --> 00:03:06,810
so what we should do here is we should import both, we should import question.dart and we should import
39
00:03:06,990 --> 00:03:14,490
./answer.dart to unlock these widgets here in this file and I'm referring to the questions list
40
00:03:14,730 --> 00:03:17,690
and also to the answer question function.
41
00:03:17,700 --> 00:03:22,890
Now of course you could now think that you want to turn quiz into a stateful widget now because
42
00:03:22,890 --> 00:03:26,080
now we have both the question and the answers in here,
43
00:03:26,280 --> 00:03:29,220
so we could manage everything for that in this widget
44
00:03:29,460 --> 00:03:35,250
but please keep in mind that in order to show the result which we want to do in the main.dart file and
45
00:03:35,250 --> 00:03:40,910
not in the quiz.dart file, otherwise we would basically transfer the ternary expression also into the
46
00:03:40,920 --> 00:03:45,900
quiz.dart file, because we need to manage that condition in the main.dart file,
47
00:03:45,990 --> 00:03:48,810
we want to keep our state in here.
48
00:03:48,900 --> 00:03:55,500
So instead what I want to do is here in the main.dart file, instead of importing the question, I import
49
00:03:55,530 --> 00:04:01,950
the quiz.dart file and that allows me to use the quiz widget here if this condition is met,
50
00:04:01,950 --> 00:04:09,030
so often the question mark here in the body and to the quiz widget, we should pass both our questions
51
00:04:09,030 --> 00:04:17,880
list and our answer question function and that of course means that we forward this answer question
52
00:04:17,880 --> 00:04:24,660
function through two levels of widget. We pass it from the MyApp widget in the main.dart file to the
53
00:04:24,660 --> 00:04:30,840
quiz widget and in the quiz widget, we forward it to the answer widget. But it is what it is, it still
54
00:04:30,840 --> 00:04:36,630
is a leaner setup where we manage the state on the highest level that makes sense, where we then also
55
00:04:36,630 --> 00:04:42,770
can still control whether to show the quiz or our result which we'll soon show here.
56
00:04:42,780 --> 00:04:51,040
So that means I want to forward a pointer at answer question to quiz and I also want to forward questions
57
00:04:51,640 --> 00:04:57,970
and as a side note because I forgot this earlier, since questions is now a property of the entire class,
58
00:04:58,360 --> 00:05:05,020
it should of course be _questions to be in line with for example the question index here because
59
00:05:05,020 --> 00:05:08,870
it is a private property that's only available in MyAppState.
60
00:05:08,980 --> 00:05:16,360
So change questions to _questions, change it in all the places where you use it, like here in
61
00:05:16,360 --> 00:05:23,410
the condition and here where you pass it down and now in the quiz widget here, we want to accept
62
00:05:23,500 --> 00:05:31,620
both that function, that answer question function and our questions list here. For that let's add two
63
00:05:31,630 --> 00:05:38,830
final properties here as you learned it, runtime constant values which are initialized when our app starts
64
00:05:38,860 --> 00:05:50,710
but thereafter they don't change and here, we have our list of maps, where each map also has different
65
00:05:50,710 --> 00:05:58,030
values, it holds string keys and object values, so we have these nested generic assignments here. So we
66
00:05:58,030 --> 00:06:04,830
have our list of questions and we also have a function which I'll name
67
00:06:04,840 --> 00:06:09,890
answerQuestion, without an underscore here because this widget is not private,
68
00:06:09,910 --> 00:06:13,240
it should be used from inside other files.
69
00:06:13,330 --> 00:06:19,270
So here, I then also add a constructor and to mix it up, not because it's required but to mix it up,
70
00:06:19,280 --> 00:06:24,890
I'll use named arguments here, so I'll have my questions list and I have my answer
71
00:06:24,920 --> 00:06:26,030
question
72
00:06:26,090 --> 00:06:27,740
function here.
73
00:06:27,740 --> 00:06:31,280
Now the problem is I also need to know which question we're looking at,
74
00:06:31,590 --> 00:06:36,310
so let's also accept our question index here
75
00:06:36,320 --> 00:06:42,080
which should be an integer and also add this as a named argument here to the quiz widget
76
00:06:42,200 --> 00:06:48,760
and then here I use question index, without the underscore and here, answer question without the underscore
77
00:06:48,770 --> 00:06:50,530
and therefore this should all work
78
00:06:50,620 --> 00:06:57,770
and now in main.dart where we use this quiz widget, we now have to pass these values as named arguments.
79
00:06:57,770 --> 00:07:05,150
I also want to add, before I pass it, I want to add required, @required in front of all these arguments,
80
00:07:05,600 --> 00:07:13,490
@required is a decorator provided by material.dart and it basically tells Flutter that all these
81
00:07:13,490 --> 00:07:17,840
values are required, that we must not omit a single one of them.
82
00:07:17,840 --> 00:07:25,970
So if you tried to create this quiz widget with only one or two or none of these arguments here, we would
83
00:07:25,970 --> 00:07:26,660
get an error.
84
00:07:27,380 --> 00:07:29,870
So now of course we have to create that quiz widget,
85
00:07:29,870 --> 00:07:33,020
we do that in MyAppState class in the main.dart file.
86
00:07:33,320 --> 00:07:34,740
Here's the quiz widget
87
00:07:34,910 --> 00:07:38,060
and now let's assign values to our named arguments.
88
00:07:38,060 --> 00:07:44,930
If we place the cursor between the parentheses and we hit control space, we get some IDE support and
89
00:07:44,930 --> 00:07:53,130
for answer question which is my function pointer here, I want to point at _answerQuestion,
90
00:07:53,210 --> 00:08:00,560
so my function I have here in the MyAppState class. At question index, there I want to pass
91
00:08:00,560 --> 00:08:09,140
_questionIndex which is the property managed here in my MyAppState class and at questions, I will
92
00:08:09,140 --> 00:08:11,980
pass _questions.
93
00:08:11,990 --> 00:08:17,600
So now we're creating that quiz widget with that data forwarded to it, might look a bit redundant but
94
00:08:17,600 --> 00:08:22,260
still, I'd say that is now cleaner here than having the column in here.
95
00:08:22,370 --> 00:08:24,830
Let's now also outsource of course our result,
96
00:08:24,890 --> 00:08:32,040
so that center widget. Here in result.dart, I again create a stateless widget which I'll name result and
97
00:08:32,120 --> 00:08:43,020
in there, I import package:flutter/material.dart like before and instead of returning a container, i return
98
00:08:43,140 --> 00:08:45,420
center.
99
00:08:45,450 --> 00:08:47,980
Now we just need to use that result widget here,
100
00:08:48,000 --> 00:08:56,890
so after the colon, we can use result, like that. For that to be available of course, we need to import it
101
00:08:56,890 --> 00:08:59,490
though, you always need to import what you use,
102
00:08:59,490 --> 00:09:05,370
so let's import result.dart. With all of that out of the way, let's restart
103
00:09:05,390 --> 00:09:05,710
the app
104
00:09:05,720 --> 00:09:12,050
by pressing the green refresh button here and let's see whether that works as it should. We see the
105
00:09:12,050 --> 00:09:18,350
questions and answers and I can tap them and if I tap my buttons three times, I see you did it.
106
00:09:18,350 --> 00:09:24,860
Of course that was a lot of work for the same result as before but Flutter is also all about writing
107
00:09:24,860 --> 00:09:27,370
clean code and clean widget trees
108
00:09:27,470 --> 00:09:34,400
and I would argue that this is now easier to read than having that column here in our main.dart file,
109
00:09:34,580 --> 00:09:40,670
especially of course if you add more and more logic in here and splitting your app into widgets is a
110
00:09:40,670 --> 00:09:41,960
good practice.
111
00:09:41,960 --> 00:09:47,690
As I mentioned, don't wrap every single text into your own custom widget but as soon as it gets a bit
112
00:09:47,690 --> 00:09:48,850
more complex,
113
00:09:48,890 --> 00:09:54,710
couple of lines, more logic, then you should strongly consider creating a new widget for that.
12689
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.