Display an image from the asset in the documentation - flutter

I'm writing documentation about a variable and I would like to include an image that is inside the project. For example:
assets/
|- icons/
| |- my-image.svg
lib/
|- my_file.dart
I know it is possible to display an image from an URL, but what about one from a file?
Those were my unsuccessful tries:
// lib/my_file.dart
/// The image attempt:
/// ![](assets/icons/my-image.svg)
/// ![](../assets/icons/my-image.svg)
const myVariable = 0;
But it doesn't work:
Is there a way to do it?

There is already an issue on GitHub: #2390
and also a related thread: Using local image assets in dart documentation comments
Effectively, web urls are working, but relative paths aren't, because VSCode doesn't support that. It tries to open the files relative to its application directory. But I can't confirm that for Windows either, because for me, not even absolute paths are working...
So if your project lives in a public repository anyways, you could just use a web url.

1:open pubspec.yaml and click "Pub get"
2:
Image.asset("assets/icons/my-image.svg",width: 100,height: 100,)

So i think you want to let the variable hold the asset image path so do this
Go to pubspecs.yaml file and add this
flutter:
assets:
- assets/icons/my-image.svg
Then run flutter pub get
then you can use the image this way
//let the variable hold the image path
const myVariable = "assets/icons/my-image.svg";
//to display on widget
Image.asset(myVariable);

Related

Is there a way to specify the file name in advance for the picture taken with the flutter camera plugin?

Hi I am using the flutter camera plugin and it works fine, the main issue I am having is that I can't find a way to tell the plugin the image file name when taking the picturee, in android using Kotlin this would be something like:
photoUri = FileProvider.getUriForFile(requireActivity(), "io.awesomedomain", photoFile) // build uri on the app storage space and specific file name -> photoFile.
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri) // define the pic name before picture is actually taken by the camera.
startActivityForResult(captureImageIntent, REQUEST_PHOTO) // start the camera
I am currently just renaming the picture file name after the picture XFile returns from the flutter camera plugin but this doesn't feel optimal so I am wondering if I am missing something. Is it possible to specify the file name in advance to the flutter camera plugin?
I am currently using the latest available version at the moment: camera: ^0.7.0+2
OK, it looks like I had a misunderstanding of the Camera plugin, I noticed that the returned XFile places the picture in the cache directory and actually provides a method to save the picture to a more definitive storage xfile.saveTo so I did:
var appDir = appDocDirectory.parent.path; // get app directory
file.saveTo('$appDir${Platform.pathSeparator}files${Platform.pathSeparator}${weightPicture.pictureFileName}');
So the picture is properly saved under $myAppDir/files/picName.jpg where the files is a directory I configured to have permission to write to and the picName is defined by the application as I wanted.

Flutter Unable to load asset

I could find this issue as quite common, but any of the suggestions didn't help me.
I want to simply show an image:
body: new Image.asset('rh.png',width:300,height:100),
You can see it is in the correct folder
It has correct place in pubspec.yaml
But I still get an error:
Unable to load asset: rh.png
I tried a lot of suggestions from github or stackoverflow, but none of them helped me; played with spaces and tabs in .yaml file (doing flutter clean after each attempt), tried to rename .png file and put it in the subfolder - no success. Where is the catch?
You didn't add assets with the filename:
Correct way is: new Image.asset('assets/rh.png', width:300, height:100)
Complete image path should be passed for fetching any asset file
in your case using assets/rh.png would resolve the error
The problem is with your path to your .png file.
It should address properly to the file location.
Correct form would be something like below:
new Image(image: AssetImage('assets/rh.png')),

FlutterWeb - Display all images in a directory

I'm creating a Flutter Web app and i'm trying to figure out a way to display all images in a directory. Here's an example tree.
images
- projects
- p1
- image1.jpg
- image2.JPEG
- image3.png
- p2
I can display every image individually with Image.Asset(images/projects/p1/image1.jpg) but some image files have different extensions or are in different subfolders. Since this is flutter web I don't have access to dart:io and cant use Directory() which includes packages like 'path_provider'. Possible solution would be to create a json manifest file with all folders/filenames but some folders can have up to 20-30 files and would require me to update the manifest everytime I add/replace/remove images. Any other ideas?
you can do something like this
assets:
- images/
- fonts/
this way all of the "Direct" child's will be added to the assets but sub-child's won't so if you can make a constant structure for your assets and add only the directories then you don't have to worry about the pubspec.yaml or running flutter pub get when files are added or removed .

How to read all assets from assets folder

Is there a way to programatically access the assets folder and loop over all its content?
I want to display all my assets for something like an emoji picker.
I tried searching the web for a solution but all the answers was for a single file from assets.
It was a restriction in the early stage of flutter i guess. But now you can add/read all assets.
In the pubspec.yaml file include the whole folder with /
flutter:
assets:
- assets/
official docs
Programatically you can acces, you should pass/use the asset's name dynamically in that case. There are lots of packages are available for emoji, I think that will be better to use one of them.
For accessing single file we can do by file include pubspec.yaml folder with /:
flutter:
assets:
- assets/res/my_file.txt
And For access all assets from asset folder :
flutter:
assets:
- assets/
Try this, Hope you get the answer!
the flutter compiler lists all files from these folders in the AssetManifest.json file. All we have to do is read this file:
final manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final images = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/images'));

How to deal with different icon sizes

When developing for iphone, in Xcode/Swift there is an asset file where you add all the icons your application will use with 1, 2 and 3x sizes. There is probably a similar thing with Android that I don't know yet.
When developing for Flutter:
How do you deal with the different screen resolutions to use the right icon sizes?
If similar to Xcode/Swift, where do you put those files and how do you get them? What about Android?
If you know a tutorial or web page, just let me know! The only ones that I found are only dedicated for app icon and launch screen.
Thanks
Just check the official documentation about resolution-aware-image.
This is based on the device pixel ratio.
Here you have an example about the structure of your files.
.../pubspec.yaml
.../icons/heart.png
.../icons/1.5x/heart.png
.../icons/2.0x/heart.png
And this is how you have to declare the assets files into your pubspec.file
flutter:
assets:
- icons/heart.png
This is the link: https://flutter.io/docs/development/ui/assets-and-images#declaring-resolution-aware-image-assets
To add to #diegoveloper answer.
Some plugins like google-maps do not honour this standard and require you to call BitmapDescriptor.fromAsset('icons/heart.png') which is not converted to the correct image. In this case you can get the correct image from AssetImage by first createLocalImageConfiguration using the BuildContext as defined here. Then use this configuration to resolve the correct image as follows:
ImageConfiguration config = createLocalImageConfiguration(context);
AssetImage('icons/heart.png')
.obtainKey(config)
.then((resolvedImage) {
print('Name: ' + resolvedImage.onValue.name);
});
In summary
Create an asset folder with the naming convention:
pathtoimages/image.png
pathtoimages/Mx/image.png
pathtoimages/Nx/image.png
pathtoimages/etc.
Where M and N are resolutions (2.0x) or themes (dark).
Then add the image or all the images to the pubspec.file as either
flutter:
assets:
- pathtoimages/image.png
or
flutter:
assets:
- pathtoimages/
Load the images in using either AssetImage('pathtoimages/image.png') or resolving to the actual file name as mentioned above, depending on the library being used.