Display local pictures in Flutter

Display local pictures in Flutter


1. Create a picture resource directory

First, create a picture resource directory to store pictures.

In the project root directory, create an assets directory, and below it, create a subdirectory called images to store image resources:

/assets/images/

2. Add local pictures to the resource directory

Add the pictures we need to the /assets/images/ directory.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-2FOBtSpi-1609240082246)(evernotecid://6FE75482-54A0-433A-9625-A01F7FEE92EC/appyinxiangcom/9896050/ENResource /p2982)]

3. Register in pubspec.yaml

Configure the picture directory to the pubspec.yaml file:

flutter:
  assets:
    - assets/images/

The entire picture catalog is registered above, and then we can use it.

Of course, if we only want to register a picture, we can do this:

flutter:
  assets:
    - assets/images/task_icon.jpg

4. Picture usage

Now, you can use the image directly in the code:

Image.asset('assets/images/task_icon.jpg')

Flutter Image is a Widget that displays pictures. Image.asset(String name): Get the image from AssetBundler.


**PS: For more exciting content, please check --> "Flutter Development"
**PS: For more exciting content, please check --> "Flutter Development"
**PS: For more exciting content, please check --> "Flutter Development"

Guess you like

Origin blog.csdn.net/u011578734/article/details/111935285