Conditional assets in Flutter - flutter

I have an app with multiple flavours. Assets are flavour-specific, and I'm looking for a way to filter out the assets that don't apply for the current flavour. The current structure looks like
assets
flavour1
image.jpg
flavour2
image.jpg
In other words, how do I make sure that when I compile flavour1 the assets folder gets truncated down to
assets
flavour1
image.jpg

Related

How to organize the images in the assets folder such in a way if we refactor developers dont need to change it every where

Guess I have a flutter project with assets in the following folder structure
assets ->
images ->
img1.png
img2.png ...
So assume I am using this img2.png in multiple files in my flutter project, and after some time I change the assets folder structure as follows since my assets folder gets overloaded with images. So the new asset folder structure would be like,
assets ->
images ->
login ->
img1.png
signin ->
img2.png
...
So in the above scenario I have changed the image folder structure based on a feature. If so, since I have used the img2.png image file in multiple files, I have to change it in all the files which is really a burden. Is there any other alternative or possibly an easier way to reflect the image path changes based on the folder structure we have redefined in the assets folder?
Thanks in advance.
You could create a static class for your app assets, so that you'll only have to change the file path in the class
For Example
class AppAssets{
static const loginImg1 = "assets/images/login/img1.png";
static const loginImg2 = "assets/images/login/img2.png";
}
// Then access it in your widgets like
Image.asset(AppAssets.loginImg1)

Is there a way to change how Flutter load assets by resolution (2.0x, 3.0x)?

In Flutter Docs https://flutter.dev/docs/development/ui/assets-and-images we need to past our Images to folders 2.0x|3.0x etc.
As a example, we have:
...icons/stackoverflow.png,
...icons/2.0x/stackoverflow.png.
In my project, I have a list icons of all countries (255 icons) and when I export from figma, it came with a suffix. It's gonna be hard rename all icons.
Is there a way to change how Flutter read assets to accept suffix in the assets? In 2.0x folder, load assets with #2x suffix and in 3.0x load assets with #3x suffix.

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 render all kinds of SVG files on iPhone?

For rendering svg file on iPhone I have downloaded a project from GitHub
SVGQuartzRenderer
but it shows the following errors:
UIKit/UIKit.h No such file or directory
(I am not able to add it using add existing frameworks)
Libxml/tree.h:No such file or directory
Expected specifier-qualifier list before xmlParserCtxtPtr
Does anyone know how I can make this project work?
I have also found another framework:
SVGKit.
It works well for some SVG files but not for all.
Is it possible to render any SVG file in iPhone without using UIWebView and are there any tutorials or sample code?
Starting Xcode 12, you can add the .svg file in your Assets Catalog and do this:
let image = UIImage(named: "SomeSVGImage")
ref: https://developer.apple.com/documentation/xcode-release-notes/xcode-12-beta-release-notes