flutter_svg icon indicating copy to clipboard operation
flutter_svg copied to clipboard

My colored SVG is rendered as black?

Open RegisSaffi opened this issue 7 years ago • 20 comments

Hey, i have SVG images and they are colored, when i import one in my flutter app,it come in black color,while i did not set the color. what is the problem?

RegisSaffi avatar May 22 '19 12:05 RegisSaffi

Same problem observed!

MeshkaniMohammad avatar May 22 '19 14:05 MeshkaniMohammad

Why can't we just see the default svg color? I can see that my svg file is being brushed with the primary color.

Tolga-GA avatar May 23 '19 06:05 Tolga-GA

I have same problem

AresVampire avatar May 23 '19 16:05 AresVampire

Can you provide a sample SVG that demonstrates this?

dnfield avatar May 23 '19 16:05 dnfield

Sample.txt

This is the file. I have renamed the extension to .txt for uploading.

AresVampire avatar May 23 '19 16:05 AresVampire

@AresVampire your problem is using a style element which isn't supported - see #105

dnfield avatar May 25 '19 06:05 dnfield

Try this: https://github.com/dnfield/flutter_svg/issues/105#issuecomment-479724077

toanalien avatar Mar 05 '20 16:03 toanalien

You can watch this answer from StackOverflow

https://stackoverflow.com/a/62748066/11681472

aliflab avatar Jul 06 '20 01:07 aliflab

Same problem, my SVG assets render black when using SvgPicture.asset. I do not believe they are corrupted as they display properly on my Mac and in Android Studio.

agersea avatar Aug 13 '20 21:08 agersea

These are the settings I used when exporting from Illustrator and it worked. I think a couple big things here is

  1. I made compound paths ( Object > Compound Path > Make ) Note: when doing this with multiple colors if I tried to make the entire thing a compound path it would mess up. So I made compound paths out of each color

  2. Notice the option CSS Properties. I noticed when doing them inline I was able to get the results I was looking for. I don't know if my subsetting setting helped at all BUT I am fairly confident making sure my css was inline was a major factor.

image

bannostookaylo avatar Sep 03 '20 15:09 bannostookaylo

flutter_svg can not understand Internal or Embedded CSS.

Hence you need to convert the internal css <style>.a{fill:#566e83;}</style> into inline css.

Example:- Convert this <path class="a" ... to <path style="fill:#566e83;" ...

You can use freecodeformat to edit your svg files.

FoolishRobo avatar Sep 19 '20 08:09 FoolishRobo

resolved: I use webview to display svg:

import 'package:webview_flutter/webview_flutter.dart';

Container(
  height: 100,
  width: 200,
  child: WebView(
    initialUrl: 'about:blank',
    onWebViewCreated:
        (WebViewController webViewController) async {
      String svgText =
          await rootBundle.loadString('assets/white-logo.svg');
      String fileText =
          '<html><style>html,body {background: #C2185B;padding: 0;margin: 0;} svg {width: 100vw; height: 100vh;}</style><body>' +
              svgText +
              '</body></html>';
      webViewController.loadUrl(
        Uri.dataFromString(
          fileText,
          mimeType: 'text/html',
          encoding: Encoding.getByName('utf-8'),
        ).toString(),
      );
    },
  ),
)

daixianceng avatar Feb 24 '21 03:02 daixianceng

Using a webview will you get you pretty complete SVG support. But it will also be pretty slow.

It's not a bad option if you have a really special and complex SVG that you want to show in full screen mode or something. But it's not a good one for showing e.g. an icon on a button :)

dnfield avatar Feb 24 '21 04:02 dnfield

If you are using the Svgpicture.asset() omit the second parameter color: Colors.black;

  const svg = 'assets/pic.svg';

  // omit the second color parameter, and your colors will be visible.
  SvgPicture.asset(svg, 
    color: Colors.black, 
    semanticsLabel: 'My SVG Picture' 
  );

 // something  like this.
  SvgPicture.asset(svg, 
    semanticsLabel: 'My SVG Picture' 
  ); 

pavittarx avatar Aug 01 '21 05:08 pavittarx

In my case, i just re-run project, i feel after adding asset svg file will work fine only after re-run project.

muhammadshahidraficp avatar Mar 02 '22 09:03 muhammadshahidraficp

The issue is that The flutter_svg is unable to load the css written inside style tag. flutter_svg only support inline css . Write inline css instead of inside the style tag

iamprawesh avatar Jun 04 '23 11:06 iamprawesh

The issue is that The flutter_svg is unable to load the css written inside style tag. flutter_svg only support inline css . Write inline css instead of inside the style tag

This definitely solves the issue! thank you @iamprawesh

AndreaDellaValle avatar Nov 05 '23 11:11 AndreaDellaValle

flutter_svg can not understand Internal or Embedded CSS.

Hence you need to convert the internal css <style>.a{fill:#566e83;}</style> into inline css.

Example:- Convert this <path class="a" ... to <path style="fill:#566e83;" ...

You can use freecodeformat to edit your svg files.

Works like a charm, thank you very much 🙏

Cyrille-Dakhlia avatar Nov 08 '23 17:11 Cyrille-Dakhlia

@AresVampire your problem is using a style element which isn't supported - see #105

Thank you ! it works

TidelxD avatar Jan 08 '24 11:01 TidelxD

resolved: I use webview to display svg:

import 'package:webview_flutter/webview_flutter.dart';

Container( height: 100, width: 200, child: WebView( initialUrl: 'about:blank', onWebViewCreated: (WebViewController webViewController) async { String svgText = await rootBundle.loadString('assets/white-logo.svg'); String fileText = '' + svgText + ''; webViewController.loadUrl( Uri.dataFromString( fileText, mimeType: 'text/html', encoding: Encoding.getByName('utf-8'), ).toString(), ); }, ), )

Thanks, this was the best options out there. Especially when you are rendering third part svg icons that you have no control over. Also for those saying it's pretty slow . The only difference I noticed was about like 1 second, but that's not much considering the trade off of having no icon or a black one

Yonkokilasi avatar Nov 04 '25 10:11 Yonkokilasi