Flutter SVG as ImageIcon - flutter

My designer has switched from PNG to SVG files, 'cause our icons are to pixelated.
How I can use a SVG as an ImageIcon?.
Previous I have used this for PNG files:
ImageIcon(AssetImage('assets/images/ic_home.png')
I'm using flutter_svg (SvgPicture.asset(item.icon, height: 24, width: 24))
Thank you in advance for your help!

You actually can't do it as mentioned here by Flutter SVG creator that PictureProvider and ImageProvider are incompatible and ImageIcon needs an ImageProvider.
However, you don't actually need to do it, and you probably won't as well. You can refactor your code to use:
SvgPicture.asset(item.icon, height: 24, width: 24)
And nothing else. That should be enough, no need for more complexity.

Related

I want a image but nither assest image nor network image is loading on screen

image is not loading in output in my screen .soo plz anyone help me in solving this
Container(
decoration: const BoxDecoration(
image:DecorationImage(image: NetworkImage(''))
),
)
Check if you added your image into the pubspec.yaml file like this:
assets:
- images/image1.jpeg
- images/icon.png
I suggest use the child of the Container and not the decoration.
Also, you could use Image.Asset instead, specifing height or/and height:
Image.asset('images/icon.png', height: 20)
Documentation here and here
Generally, Network Images on flutter take time to appear.
But if it still continues then
Check Internet Permission setting in andoidmanifes.xml file
Using Asset image is not a good solution because it makes the app size larger..
The solution is that you need to use:
Container(
child: Image.network('') //your image source
)
And if that is not the case.. the image you are using is invalid but not exactly invalid. Meaning it does not end with an image format in the end.. like (.jpg,.png etc.)

How can add a specified image asset to pubspec and then load the image to AssetImage for the app to present?

I can't find anywhere teaches how to add a specified image within asset. Actually I don't know which pictures are included in the asset, or if I want to use an image from the Internet or my own photo gallery, I don't know how I can link the picture with the asset and apply it in my code.
Similarly, I also don't know what are included in packages, like font_awesome flutter. How can I check what are included? Thank you!
I think I have solved the above questions with the help of commenters, but I have more questions coming up: Why I was unable to load the asset?
Please see the pictures below.
enter image description here
enter image description here
you can place your assets in a directory in your project folder.
For example assets\drawables\ and assets\icons\
Then in your pubspec.yaml you should declare that folder. In this example:
# To add assets to your application, add an assets section, like this:
assets:
- assets/drawables/
- assets/icons/
Than in your code you can use them as follow:
Image.asset(
'assets/drawables/image.png',
width: 200,
),
For your second question, I think you only have two options:
either look for the library documentation / website, such as www.fontawesome.com (the free icons are the one in the library). Or look in the code/package yourself
Place your picture in a folder named assets like this:
Add this line to your pubspec.yaml:
Then put this somewhere in your code:
Image.asset(
'assets/my_image.png',
width: 200,
height: 200,
fit: BoxFit.contain,
);

flutter_svg not loading some svg files

I am unable to open certain svg files with flutter_svg package
example of file is this
I wanted to make sure there is nothing I am missing before I may be open an issue
I am getting no error back just an empty space
the code for loading the image
SvgPicture.asset(
"assets/svg/agaseke_logo.svg",
color: Color(0xFF0bd50b),
height: 45,
width: 25,
),

How to use SVG in Flutter

I'm currently in the process of integrating a map into my app, which should highlight individual components in color. I use this map (https://simplemaps.com/resources/svg-de), which is already available in a well-designed SVG file. I am now trying to color the individual SVG paths using Flutter Code. I am building on the svg_path_parser package (https://pub.dev/packages/svg_path_parser) and have used the corresponding example. But I am unable to replace the paths from the example with those from my SVG file. This may be due to the fact that my SVG paths are significantly longer than the ones given in the example.
I would be very grateful if you would give me a little help or a suggestion for a better approach.
step 1.add dependency in your pubspec.yaml
dependencies:
flutter_svg: any
step 2.import the flutter svg package in your app
import 'package:flutter_svg/flutter_svg.dart';
step 3. just use like below
SvgPicture.asset(
'assets/images/bm-icon1.svg',
width: 18.0,
height: 18.0,
),

SVG in Flutter Exception caught by SVG

I am using the latest library update
dependencies:
flutter_svg: ^0.17.4
Container( height: 150, //color: Colors.blue, width: screenWidth * 0.80, child: SvgPicture.asset( "assets/logo-violeta-NVIAME-login.svg", color: Color(0xFF6327f8), ), ),
------------------------------------------------------------------------
My code worked but I did Hot Restart .. and well I get the following exception
The <style> element is not implemented in this library.
Style elements are not supported by this library and the requested SVG may not render as intended.
If possible, ensure the SVG uses inline styles and/or attributes (which are supported), or use a preprocessing utility such as svgcleaner to inline the styles for you.
Picture key: AssetBundlePictureKey(bundle: PlatformAssetBundle#60537(), name: "assets/logo-violeta-NVIAME-login.svg", colorFilter: ColorFilter.mode(Color(0xff6327f8), BlendMode.srcIn))
════════════════════════════════════════════════════════════════════════════════
I/flutter (26058): unhandled element metadata; Picture key: AssetBundlePictureKey(bundle: PlatformAssetBundle#60537(), name: "assets/logo-violeta-NVIAME-login.svg", colorFilter: ColorFilter.mode(Color(0xff6327f8), BlendMode.srcIn))
It is actually the SVG image that you've used.
As mentioned in the error:
The <style> element is not implemented in this library.
Style elements are not supported by this library and the requested SVG may not render as intended.
If possible, ensure the SVG uses inline styles and/or attributes (which are supported), or use a preprocessing utility such as svgcleaner to inline the styles for you.
flutter_svg package does not support svg with internal css like:
<style>
.....
</style>
Here is an open GitHub issue that indicates it is not yet supported.
Some of the options that you can actually try:
Remove the internal css and write inline css
Use different svg picture
Use a preprocessing utility such as svgcleaner to inline the styles for you. Which is exactly the one you've found as mentioned in the comments. See also this SO post.
Use this tool https://jakearchibald.github.io/svgomg/
But the easiest way is to have your design tool not use CSS if possible.
My workaround: drag and drop the svg files to the Figma design tool and then export all of them to get the standard error-free svg files.
I faced this issue too, then I used 'SVG Cleaner' app to clean and inline SVG code.
By the end it worked properly ^_*