Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,450 --> 00:00:05,010
Now in the last lesson, we've already created the main structure of our app.
2
00:00:05,100 --> 00:00:11,610
But in this lesson, I want to show you how you can work with image assets that you load into your app,
3
00:00:11,940 --> 00:00:16,500
so you don't have to fetch it from the Internet and you'll be able to use it anywhere inside your app
4
00:00:16,620 --> 00:00:19,770
without needing an internet connection.
5
00:00:19,770 --> 00:00:25,470
So first things first, we're going to click on the project folder so you can see our project here is
6
00:00:25,470 --> 00:00:31,740
called 'I Am Rich' and it's the root of all of our other folders. And we're going to right click on that,
7
00:00:31,830 --> 00:00:35,300
and we're going to create a new directory.
8
00:00:35,460 --> 00:00:41,460
We're going to call this directory, images. Inside this folder is where we're gonna put all of the images
9
00:00:41,460 --> 00:00:44,410
that we're going to use to work with our app.
10
00:00:44,430 --> 00:00:50,880
And if you look inside this current lesson you should be able to see a downloadable file called diamond
11
00:00:50,940 --> 00:00:52,720
.png
12
00:00:52,890 --> 00:01:01,140
And once you've downloaded that image, I want you to click and drag that image into that images folder
13
00:01:01,140 --> 00:01:06,090
that we created just now. And it'll have a little red line around the folder so that you know that it's
14
00:01:06,090 --> 00:01:15,280
going into the right one. And then we're going to go ahead and click OK to move that file into there.
15
00:01:15,330 --> 00:01:22,350
So now that we've incorporated that image into our project, it's time to tell Flutter that the image
16
00:01:22,440 --> 00:01:28,670
exists. And to do that, we're going to go into the configuration file, which is our pubspec.yaml
17
00:01:28,680 --> 00:01:35,880
file. And if you scroll down, you'll see that it even tells you in a comment, that this is how you add
18
00:01:36,030 --> 00:01:38,420
assets to your application.
19
00:01:38,480 --> 00:01:41,690
We have to create an asset section like this.
20
00:01:41,790 --> 00:01:47,280
So let's go ahead and uncomment this by holding down COMMAND + /.
21
00:01:48,300 --> 00:01:53,800
And we now have a assets section. Now inside our assets section,
22
00:01:53,910 --> 00:01:59,040
we're going to add our image. Instead of a.burr.jpeg
23
00:01:59,070 --> 00:02:01,890
we're going to add images/
24
00:02:01,890 --> 00:02:09,340
So that refers to our images folder, and then /diamond. png
25
00:02:09,360 --> 00:02:14,430
And this tells our Flutter project about this particular asset.
26
00:02:14,550 --> 00:02:20,160
And once it knows about it, we'll be have to use it inside our main.dart or any other code files that's
27
00:02:20,160 --> 00:02:22,440
in our Flutter project.
28
00:02:22,440 --> 00:02:26,330
Now you'll notice that inside this pubspec.yaml
29
00:02:26,430 --> 00:02:30,440
the comments don't start with two forward slashes.
30
00:02:30,570 --> 00:02:31,990
And in fact you tried that,
31
00:02:32,070 --> 00:02:33,280
it doesn't actually work.
32
00:02:33,300 --> 00:02:36,770
That's not treated as a comment.
33
00:02:37,080 --> 00:02:40,900
And this is because this is a special type of file.
34
00:02:40,920 --> 00:02:46,750
It's a YAML file, and YAML stands for YAML Ain't Markup Language.
35
00:02:47,280 --> 00:02:55,920
It's meant to be this very human readable type of language that machines can also understand, to interpret
36
00:02:55,980 --> 00:03:00,960
how you want to configure certain things, such as our Flutter project.
37
00:03:00,960 --> 00:03:05,310
Now that's all very well and good but there's just one problem.
38
00:03:05,310 --> 00:03:08,910
It relies really heavily on indentations.
39
00:03:08,910 --> 00:03:18,060
So for example every section inside this configuration file is right next to an up to the margin or
40
00:03:18,060 --> 00:03:19,650
the gutter right in the left.
41
00:03:19,650 --> 00:03:24,460
You can see that the name of our project or the description for our project,
42
00:03:24,480 --> 00:03:25,840
they don't have any spaces
43
00:03:25,860 --> 00:03:26,920
next to the left.
44
00:03:27,150 --> 00:03:33,840
And if they did, they wouldn't be treated as a new section. They would be treated as a child embedded
45
00:03:33,870 --> 00:03:36,450
in another section. Like this.
46
00:03:36,450 --> 00:03:46,770
So if you have two spaces next to something, then this is treated as a child inside this thing.
47
00:03:46,770 --> 00:03:53,520
So that means we have to be really careful when we're writing code in our pubspec.yaml because
48
00:03:53,670 --> 00:04:00,690
as it is, this doesn't work. So well we can do to make our lives just a little bit easier is to actually
49
00:04:00,690 --> 00:04:08,280
delete all of these comments that came in the configuration file. And a lot of these comments are useful
50
00:04:08,310 --> 00:04:14,430
because the Flutter team is trying to tell us what we can do with the configurations here, but it also
51
00:04:14,430 --> 00:04:19,760
clutters up the page and makes it really hard to see at a glance, what's actually going on.
52
00:04:19,860 --> 00:04:26,640
And so if we take a look now, you can see that we've got several top level properties such as the name
53
00:04:26,670 --> 00:04:33,570
of the project, description or version. And then we've got other things which are children of these top
54
00:04:33,570 --> 00:04:38,610
level items. Over here, we've got a couple of settings for our Flutter app.
55
00:04:38,610 --> 00:04:44,420
One is that it uses material design and the other are the assets that it wants to use.
56
00:04:44,430 --> 00:04:50,940
Now you can see that this asset is kind of out of sync with the indentation. It's currently indented by
57
00:04:51,180 --> 00:04:59,280
one, two, three spaces, and that doesn't actually mean anything in YAML because each indentation level,
58
00:04:59,460 --> 00:05:01,060
is two spaces.
59
00:05:01,060 --> 00:05:08,020
So if we hit backspace and move it back just by one, so that it's now indented by two spaces, from the
60
00:05:08,020 --> 00:05:13,630
top level Flutter key, then this is now pretty much valid YAML code.
61
00:05:14,220 --> 00:05:19,990
And this tells our configuration file that our assets are children of our Flutter project.
62
00:05:20,380 --> 00:05:25,780
So here's a top tip. When you're working with the pubspec.yaml file, your configuration file for
63
00:05:25,780 --> 00:05:27,190
your Flutter project,
64
00:05:27,310 --> 00:05:35,070
be really careful about the indentation. And remember that every indent is two spaces.
65
00:05:35,200 --> 00:05:42,680
So when your coding along with me, make sure that you have the same indentation levels as you see on
66
00:05:42,680 --> 00:05:43,270
screen.
67
00:05:43,660 --> 00:05:47,520
Otherwise you might get into trouble that you don't really need.
68
00:05:47,560 --> 00:05:54,190
Now that we've set up our configuration file to tell our project that we want to use assets, and our
69
00:05:54,190 --> 00:06:01,330
assets live in a folder called images, and the file is called diamond.png then we're ready to incorporate
70
00:06:01,330 --> 00:06:04,720
these files into our project, ready for use.
71
00:06:04,720 --> 00:06:10,540
So we're going to hit save with COMMAND + S or CONTROL + S. Aand we're going to click on this link here
72
00:06:10,540 --> 00:06:17,410
where it says 'Packages get' and that will incorporate the images that's inside our project ready for
73
00:06:17,410 --> 00:06:22,330
use within our main.dart or any of our other project files.
74
00:06:22,360 --> 00:06:30,550
So now, we can head back to our main.dart file, and instead of using a network image, we're going to
75
00:06:30,550 --> 00:06:33,000
use a different type of image.
76
00:06:33,100 --> 00:06:40,810
We're going to use an asset image. And as you can imagine, an asset image comes from our assets, which
77
00:06:40,810 --> 00:06:42,910
are defined in our configuration file.
78
00:06:43,420 --> 00:06:51,280
So we're now able to provide the name of the asset which is going to be exactly what we specified in
79
00:06:51,280 --> 00:06:52,470
here.
80
00:06:52,480 --> 00:06:58,480
So now let's head back into our main.dart file and we can specify the name of our image. And it's
81
00:06:58,480 --> 00:07:03,490
going to be images/diamond.png
82
00:07:03,730 --> 00:07:09,820
And that is all we need to do to be to use the image that we've incorporated under the images folder
83
00:07:10,180 --> 00:07:17,380
with the name of diamond.png and it will know what images/diamond.png is, because we added
84
00:07:17,380 --> 00:07:20,730
it to the configuration pubspec.yaml file.
85
00:07:20,890 --> 00:07:24,900
So now all we have to do is run our app and test it.
86
00:07:24,910 --> 00:07:33,400
So let's click the play button, and you can see our app now has our image asset that comes from our images
87
00:07:33,400 --> 00:07:42,280
folder namely the diamond.png image incorporated into our image placeholder in the center of the body
88
00:07:42,430 --> 00:07:43,510
of our app.
89
00:07:43,540 --> 00:07:47,800
Now for the cute minds amongst you, you might realize a slight problem.
90
00:07:47,800 --> 00:07:53,240
What if you had a 20 or 30 or 40 images inside your images folder?
91
00:07:53,260 --> 00:07:58,570
Are you really going to write them out line by line making sure that you've indented the correct number
92
00:07:58,570 --> 00:07:59,620
of spaces?
93
00:07:59,650 --> 00:08:01,030
That's quite painful right.
94
00:08:01,450 --> 00:08:09,820
What you can also do, is instead of specifying the precise file name, you can also just specify the folder
95
00:08:09,880 --> 00:08:15,970
where all of your image files are located, and then add a '/' to say that I want to incorporate
96
00:08:16,210 --> 00:08:20,740
everything that's inside that folder into my project.
97
00:08:20,740 --> 00:08:27,880
And if you go ahead and run 'Packages get' again, and you run your app then absolutely nothing should have
98
00:08:27,880 --> 00:08:28,430
changed.
99
00:08:28,480 --> 00:08:34,240
Everything should have been exactly the same because we're saying now we are adding everything inside
100
00:08:34,240 --> 00:08:42,660
the images folder as an asset that we're going to use inside our app. So as long as your images go into
101
00:08:42,660 --> 00:08:48,030
the folder that's called images, and not inside say a sub folder or you know, a folder of a folder.
102
00:08:48,180 --> 00:08:54,640
As long as they're inside the images folder, then you will be able to use it like so.
103
00:08:54,640 --> 00:08:59,330
And that also means you won't have to touch these indents after the first time you make them.
104
00:08:59,400 --> 00:09:02,330
So makes you life a little bit easier.
105
00:09:02,520 --> 00:09:10,140
Now this just one last thing that we need to fix which is when we take a look at our app icon. It still
106
00:09:10,140 --> 00:09:16,770
has the Flutter default app icon. And this will get placed for every single app that you create with
107
00:09:16,770 --> 00:09:19,960
Flutter, unless we update the app icon.
108
00:09:20,010 --> 00:09:23,700
So to learn how to do that and more, I will see you on the next lesson.
11492
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.