Images
We are going to know about how to add images in flutter application. A flutter app when built has both assets and code. Assets mean resources that are available and deployed during runtime. The asset is a file that can include static data, configuration files, icons, and images offline and also online. The Flutter app supports many image formats such as JPEG, WebP, PNG, GIF, animated WebP/GIF, and many more.
We can add images in multiple ways here we are discussing two ways that are mainly used in the flutter.
Image.assets
When you have to set the images statically and display them in the app, you can use Image.assets() function.
1. Add an Image to the new folder
The new folder is should be at the root of your flutter project. You can name it as per your preference, but assets are preferred.
If you want to add other assets to your app, like fonts then it is preferred to make another subfolder named fonts, images.
2. Set image path
Now you can copy your image to the images sub-folder into the assets folder. The path should look like assets/images/yourImage. Before adding images also check the above-mentioned supported image formats.
Register the assets folder and image path in pubspec.yaml file and update it.
3. Add image in code
Image.network
When you have to set the images from a URL at that time you can use Image.network() function.
When you are using the Image.network at that time you do not need to create a folder add the image in pubspec.yaml file because we are giving a path for that image.
Nice walkthrough on adding images to the application. Simple and easy to follow.
ReplyDelete