All language subtitles for 024 Creating a New, Custom Widget_en

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,330 --> 00:00:06,580 A few lectures ago, I mentioned the strange input data. 2 00:00:06,630 --> 00:00:09,270 Now what do I mean by input data? 3 00:00:09,290 --> 00:00:10,530 Let me explain this 4 00:00:10,610 --> 00:00:15,990 and at the same time let's also improve our look of this app, at least a little bit 5 00:00:16,219 --> 00:00:23,390 and for that, we'll actually create a new widget, a widget which should wrap this question text here. 6 00:00:23,600 --> 00:00:28,280 Now to create such a new widget, we can simply add a new class. 7 00:00:28,280 --> 00:00:35,330 Now we can do this in the same file we previously worked in but a good convention rule is to only have 8 00:00:35,330 --> 00:00:37,460 one widget per file. 9 00:00:37,460 --> 00:00:43,560 There are rare exceptions to that rule, for example if you have two widgets that really only work together 10 00:00:43,640 --> 00:00:48,920 and you don't really plan on reusing a widget in other files but generally, you want to have one widget 11 00:00:48,920 --> 00:00:53,780 per file and therefore here, I'll add a new file next to the main.dart file and I'll name it 12 00:00:53,810 --> 00:00:56,690 question.dart 13 00:00:56,920 --> 00:00:58,860 and this should hold my question widget. 14 00:00:59,000 --> 00:01:04,970 Now you should actually, thanks to the Flutter extension which you installed, have a little helper here 15 00:01:04,970 --> 00:01:12,440 if you type st, you should normally get some suggestions to automatically create some widget, so a little 16 00:01:12,440 --> 00:01:18,590 shortcut, a little code snippet which was added by the Flutter extension in Visual Studio Code and in 17 00:01:18,590 --> 00:01:19,750 Android Studio. 18 00:01:19,940 --> 00:01:24,890 If you get that, you can select a Flutter stateless widget option here and hit enter and you automatically 19 00:01:24,890 --> 00:01:27,590 get a new stateless widget created for you, 20 00:01:27,620 --> 00:01:33,790 all you have to do here is add a name for that widget, which in my case would be question here. 21 00:01:33,840 --> 00:01:39,330 Now this is in the end a class extending stateless widget and you already get that build method with 22 00:01:39,330 --> 00:01:45,030 the override annotation and it's the exact same widget setup we manually created at the beginning of 23 00:01:45,030 --> 00:01:46,590 this module. 24 00:01:46,590 --> 00:01:54,570 Now here, we're using stateless widget and that is provided by the material file, from the Flutter package, 25 00:01:54,750 --> 00:02:00,300 just like widget and build context and this container thing which we haven't used before and therefore, 26 00:02:00,300 --> 00:02:06,330 we have to add an import at the top of the file which points at a package and there at the Flutter package 27 00:02:06,330 --> 00:02:07,780 with /material.dart, 28 00:02:07,800 --> 00:02:14,760 so at that material.dart file. Now this is a valid widget and there, let's get rid of container and let's 29 00:02:14,850 --> 00:02:19,740 add text here and here, I want to output the question text. 30 00:02:19,740 --> 00:02:21,510 The problem just is 31 00:02:21,510 --> 00:02:24,850 I'm managing my questions here in main.dart. 32 00:02:25,110 --> 00:02:30,870 Now of course, we could move that questions list or array as you could also call it, array and list, these 33 00:02:30,870 --> 00:02:32,730 are interchangeable terms, 34 00:02:32,730 --> 00:02:36,060 you could move that list into the question widget. 35 00:02:36,060 --> 00:02:38,990 However then, you would have your list of strings in there 36 00:02:39,150 --> 00:02:44,520 but the logic for changing the index resides in the main.dart file. 37 00:02:44,540 --> 00:02:50,940 Now you could move that into the question widget as well but we'll later also need it for our answer buttons 38 00:02:50,940 --> 00:02:54,640 and actually already the answer buttons are in the main.dart file. 39 00:02:54,750 --> 00:03:00,000 We could move them into the question.dart file as well but then we effectively would have moved basically 40 00:03:00,030 --> 00:03:05,520 everything from the main.dart file into the question.dart file and that's not the idea behind splitting 41 00:03:05,520 --> 00:03:07,250 our app into widgets. 42 00:03:07,380 --> 00:03:13,800 So instead what I want to do is, I want to come back to that idea of getting some input data in a widget 43 00:03:14,520 --> 00:03:22,650 and if you recall earlier lectures of this module, then you know that when you create a class, you can 44 00:03:22,650 --> 00:03:30,330 have a constructor which can take some data to initialize data inside of that class 45 00:03:30,570 --> 00:03:36,570 and here the idea could be that we simply have our question text or whatever you want to name it 46 00:03:36,750 --> 00:03:42,100 and since I don't initialize it with a default value here, I'll set this to type string to be clear 47 00:03:42,100 --> 00:03:43,820 about which type of data it will hold 48 00:03:44,310 --> 00:03:49,770 and that question text should be passed into that widget, kind of from outside, 49 00:03:49,770 --> 00:03:57,030 so from inside the place where we later use that question widget. For that, we can add a question constructor 50 00:03:57,270 --> 00:04:03,150 using the same syntax I showed you earlier, where you repeat the name of the class and then add parentheses 51 00:04:03,660 --> 00:04:09,300 and then you can either use the long form with the curly braces where you accept your arguments and 52 00:04:09,300 --> 00:04:16,560 then you store your arguments in properties of this class or you use the short form where you add a 53 00:04:16,560 --> 00:04:24,930 semicolon after the constructor and then you use this question text and now the first argument which 54 00:04:24,930 --> 00:04:31,650 is passed to the question constructor will be stored in the question text property. Please note that 55 00:04:31,650 --> 00:04:38,080 I'm saying the first argument because since I didn't wrap this here in curly braces in my argument list, 56 00:04:38,220 --> 00:04:41,600 this is not a named argument but a positional argument, 57 00:04:41,610 --> 00:04:46,950 only if I would wrap this in curly braces it would be turned into a named argument. 58 00:04:46,950 --> 00:04:52,290 However here since I only need one argument, I'll go with a positional argument because identifying it 59 00:04:52,590 --> 00:04:53,530 shouldn't be too hard, 60 00:04:53,560 --> 00:04:54,940 it's the only one, right. 61 00:04:55,080 --> 00:05:01,800 So this is my positional argument which is required which will be stored in the question text variable 62 00:05:02,100 --> 00:05:10,920 or property here and now down there in the text widget which expects a text, I can simply refer to question 63 00:05:10,920 --> 00:05:11,250 text, 64 00:05:11,250 --> 00:05:14,790 so to this property, I can refer to it down there. 65 00:05:14,790 --> 00:05:20,040 Now how do we use this widget then? Let's go back to the main.dart file, where we at the moment have our 66 00:05:20,040 --> 00:05:26,430 text widget here and now I want to forward my selected question which I select in the same way I did 67 00:05:26,430 --> 00:05:34,510 before, not to the text widget but to my question widget instead. To do that, we first of all need to import 68 00:05:34,600 --> 00:05:40,240 our question widget from the question file, so we add a new import statement at the top and the convention 69 00:05:40,240 --> 00:05:46,480 here is to first of all have a block of all the imports that point at packages and then a block of imports 70 00:05:46,480 --> 00:05:48,540 that point at your own files. 71 00:05:48,640 --> 00:05:52,450 Here you add quotes, single quotes or double quotes, doesn't matter, 72 00:05:52,450 --> 00:05:53,890 just be consistent 73 00:05:53,890 --> 00:06:01,450 once you made your choice and there, you now add a relative import path, which means you add dot slash, 74 00:06:01,480 --> 00:06:07,060 which means look in the same folder as the main.dart file is and then the name of the file from which 75 00:06:07,060 --> 00:06:07,700 you want to import, 76 00:06:07,720 --> 00:06:09,710 which in this case is question.dart. 77 00:06:09,850 --> 00:06:15,970 So now we're importing from question.dart and that means everything that's in this file is now available 78 00:06:15,970 --> 00:06:21,310 in the main.dart file as well, unless you name it with an underscore in front of the name. 79 00:06:21,310 --> 00:06:25,720 However question doesn't have an underscore and it shouldn't because we want to be able to use the 80 00:06:25,720 --> 00:06:28,730 question widget in the main.dart file. 81 00:06:28,840 --> 00:06:37,360 So here, we now can use question instead of text and since question, its constructor, expects to get a 82 00:06:37,360 --> 00:06:42,730 value for the question text, we still need to pass a value to this question constructor, 83 00:06:42,760 --> 00:06:46,750 so this is already correct here in the main.dart file. 84 00:06:46,820 --> 00:06:49,920 Now of course you might wonder, what's the benefit of this? 85 00:06:49,960 --> 00:06:51,930 Previously, we used text, 86 00:06:52,030 --> 00:06:56,230 now we use our own widget but in there, we also just use text. 87 00:06:56,230 --> 00:07:01,110 Of course for the simple restructuring, it's not useful, nonetheless 88 00:07:01,210 --> 00:07:09,790 it is a good idea to split your widgets or your app into smaller custom widgets that can help with performance 89 00:07:10,000 --> 00:07:13,350 because it can make your rebuilds more efficient 90 00:07:13,480 --> 00:07:16,830 and it also makes it easier for you to manage your code. 91 00:07:16,900 --> 00:07:21,340 Of course, not that much if all we had here was a single text widget 92 00:07:21,340 --> 00:07:28,690 but the more complex of a widget structure you had here, the leaner your main.dart file gets as soon 93 00:07:28,690 --> 00:07:30,790 as you move that into a separate widget. 94 00:07:30,790 --> 00:07:36,880 Again, not that useful for the single text widget but we'll see the benefits more and more as we progress 95 00:07:36,880 --> 00:07:38,550 throughout the course. 96 00:07:38,680 --> 00:07:41,470 Now that here is the stateless widget, that's important 97 00:07:41,470 --> 00:07:47,950 and this now receives some input data because it gets some data in its constructor. 98 00:07:47,950 --> 00:07:54,040 Now if we save that and we have a look at our app, it looks the same way as before and conveniently if 99 00:07:54,040 --> 00:07:59,920 we press Answer 1, it also behaves the same way as before and the text changes once we press that 100 00:07:59,920 --> 00:08:00,860 button. 101 00:08:01,060 --> 00:08:03,050 Now this has one important implication, 102 00:08:03,070 --> 00:08:07,540 it means that what I mentioned on this slide is true. On the left side, 103 00:08:07,750 --> 00:08:12,060 on the stateless widget, it gets re-rendered when the input data changes. 104 00:08:12,160 --> 00:08:14,150 Now that's exactly what's happening here. 105 00:08:14,260 --> 00:08:21,460 Here in main.dart, when we change the question index with set state, the build method gets called here 106 00:08:21,460 --> 00:08:27,400 and therefore Flutter and Dart go through our widget tree and in the end, they find out that for question 107 00:08:27,400 --> 00:08:34,539 here, we are changing the text right because questions, question index changed because the index changed, 108 00:08:34,750 --> 00:08:38,260 so we select the second question in our list of questions now. 109 00:08:38,470 --> 00:08:45,130 So the input data to question changes and therefore Flutter automatically calls build in the question 110 00:08:45,280 --> 00:08:46,180 widget here. 111 00:08:46,390 --> 00:08:50,890 Now technically, it calls build for every widget here but then the widget basically checks 112 00:08:50,890 --> 00:08:57,160 did I change and most widgets didn't change but for the question widget, it now did change because it 113 00:08:57,160 --> 00:09:03,580 received a new text and since it received a new text and was rebuilt with that new text, it also updates 114 00:09:03,700 --> 00:09:05,190 on the screen. 115 00:09:05,320 --> 00:09:07,600 So that is what Flutter does for us, 116 00:09:07,600 --> 00:09:13,120 we rebuild the stateless widget not because it changed internally, 117 00:09:13,180 --> 00:09:13,850 it can't, 118 00:09:13,870 --> 00:09:17,350 it's stateless but because the data from outside 119 00:09:17,350 --> 00:09:24,020 changed when the build method in main ran again. So that's what's happening here. 120 00:09:24,330 --> 00:09:31,260 Now one syntax improvement which we should do here because I'm already getting a warning here, 121 00:09:31,500 --> 00:09:40,190 this here is a property of the question widget, which like this could be changed internally. 122 00:09:40,200 --> 00:09:45,420 Now of course, that wouldn't be reflected on the user interface as you learned because it's stateless 123 00:09:45,780 --> 00:09:50,880 but technically inside of the class, you could change this because there's a difference between the Dart 124 00:09:50,910 --> 00:09:57,170 features and there in any class, you can change data and how Flutter uses that class 125 00:09:57,420 --> 00:09:58,920 and that's what that warning tells us. 126 00:09:58,920 --> 00:10:01,740 This class is marked as immutable, 127 00:10:01,740 --> 00:10:05,160 that happens inside of the stateless widget in the end 128 00:10:05,160 --> 00:10:11,460 and yet we have an instance field which in the end is just a property which is not final, which means it 129 00:10:11,460 --> 00:10:12,450 could be changed, 130 00:10:12,450 --> 00:10:18,300 you could change that string from inside that class and therefore, it's a good convention and recommended 131 00:10:18,300 --> 00:10:23,430 that you add final in front of this property definition here. 132 00:10:23,460 --> 00:10:31,860 This tells Dart that this value will never change after its initialization here in the constructor. 133 00:10:31,860 --> 00:10:33,190 So this change doesn't count, 134 00:10:33,210 --> 00:10:34,340 that is OK, 135 00:10:34,560 --> 00:10:39,840 so when data is received here in the constructor that will be stored in question text because in the 136 00:10:39,840 --> 00:10:45,370 end, that all happens when this object gets created based on the class, the constructor 137 00:10:45,420 --> 00:10:51,720 remember gets called when the object is created. At this point of time, the data we receive in the constructor 138 00:10:51,720 --> 00:10:53,640 is stored in that property, 139 00:10:53,670 --> 00:10:59,730 so that is OK but thereafter, once this object and therefore this widget is live 140 00:10:59,730 --> 00:11:05,300 so to say, we can't reassign this, we can't assign a new value to question text. 141 00:11:05,430 --> 00:11:11,490 We didn't do that but now we would get an error if we tried to and that only makes sense because since 142 00:11:11,490 --> 00:11:16,380 it's a stateless widget, changing this wouldn't have any effect and now we're really writing code that 143 00:11:16,380 --> 00:11:18,630 makes this super clear. 144 00:11:18,630 --> 00:11:25,110 So adding final here is a common thing you will see throughout this course and something you should add here. 145 00:11:25,140 --> 00:11:28,160 Now with that, we have the same behavior as before 146 00:11:28,200 --> 00:11:34,020 but in our own widget and our custom widgets will become more and more important the more complex our 147 00:11:34,020 --> 00:11:35,730 widget logic gets. 148 00:11:35,940 --> 00:11:40,920 We can already make this a bit more complex by adding our first little styling to this text. 149 00:11:40,950 --> 00:11:43,250 Thus far we haven't really styled anything, 150 00:11:43,260 --> 00:11:47,550 well now we can do at least a little bit of styling here for our question text. 16716

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