Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
Loading of Flutter picture
Everyone knows that adding images to flutter is cumbersome. After adding images to flutter, be sure to write the path in pubspec.yaml.
And then you still have to hard-code it, like this:
static final add = Image.asset('res/assets/add.png', fit: BoxFit.cover);
Copy the code
The principle of Flutter image loading is the same as above. Images can be loaded as long as there are no mistakes. However, we usually don’t just add a few images to a Flutter App. So pubspec.yaml file we have a good plug-in to use, here is a plug-in I have been using, one click import image path, and avoid the embarrassment of hard coding.
flutter-img-sync
This plugin can be found by searching for flutter-img-sync in the VSCode extension. The following is a detailed description of how to use flutter and what to pay attention to.
The installation
Find this plugin in the extension store, download it and install it.
Modify pubspec yaml
The assets path of the pubspec.yaml file is modified and the format remains as follows
Assets: # assets-generator-begin # res/assets/* Fill in your picture path # assets-generator-endCopy the code
Run the FlutterImgSync command
After the pubspec.yaml file has been modified, call out to the command panel, type FlutterImgSync, and press Enter.
Going back to our pubspec.yaml file, the place where we just changed the path should have added the path to all the images in our project.
At the same time, there should be a new file in our project, R. swift
R.swift has a class R that defines static variables, which are the paths to all images in the project. Similar to r.swift, we use scripts to define resource files for later use.
Method of use
The next step is to make it easy to use. Let’s just use r. Swift. For those of you who have used R.swift, there should be no hard coding errors.
Thank you for reading
Well, this article is a Tips, there will be some useful small tools to share, I hope it is useful to students.
If you like it, give it a thumbs up