Skip to content

[flutter_svg] Stroke opacity not applied #158735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stuartmorgan-g opened this issue Nov 13, 2024 · 0 comments · May be fixed by flutter/packages#8986
Open

[flutter_svg] Stroke opacity not applied #158735

stuartmorgan-g opened this issue Nov 13, 2024 · 0 comments · May be fixed by flutter/packages#8986
Labels
p: flutter_svg The Flutter SVG drawing packages P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels.

Comments

@stuartmorgan-g
Copy link
Contributor

Imported from dnfield/flutter_svg#891

Original report by @Maatteogekko on Mar 17, 2023

Take this svg as an example

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
  <path fill="#293540" stroke="#293540" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
    d="m19 21-7-5-7 5V5c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2v16Z" />
</svg>

I am using the color mapper API to remap colors like this:

class _ColorMapper implements ColorMapper {
  const _ColorMapper({
    required this.baseColor,
  });

  static const _rawBaseColor = Color(0xFF293540);

  final Color baseColor;

  @override
  Color substitute(String? id, String elementName, String attributeName, Color color) {
    if (color == _rawBaseColor) return baseColor;

    return color;
  }
}

// ---

SvgPicture(
  SvgStringLoader(
    icon,
    colorMapper: _ColorMapper(
      baseColor: iconColor,
    ),
  ),
);

If I use a color with opacity != 1, let's say iconColor = Color(0x802260DD) as the base color, only the fill gets the opacity applied; the stroke remains fully opaque.

Maybe related to dnfield/flutter_svg#617


Comment by @dnfield on Mar 17, 2023
Yeah, there's a bug here for sure. Taking a look.


Comment by @dnfield on Mar 17, 2023
Ok. So I can definitely fix the whole "stroke opacity is getting ignored" part, but be aware that strokes and fills get composited individually and the opacity will be multiplicative - so your case will be something like this SVG:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
  <path fill="#293540" stroke="#293540" fill-opacity=".5" stroke-opacity=".5" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
    d="m19 21-7-5-7 5V5c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2v16Z" />
</svg>

Which will not look like a single uniform color.


Comment by @Maatteogekko on Mar 18, 2023
You're right. To get a uniform color I tried with this:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
  <path fill="#293540" stroke="#293540" opacity=".5" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
    d="m19 21-7-5-7 5V5c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2v16Z" />
</svg>

But it still doesn't render correctly. I guess it's the same problem?


Comment by @dnfield on Mar 19, 2023
If you want it uniform just remove the stroke attribute(s)


Comment by @devmaslove on Oct 18, 2023
I have the same problem. In svg file: stroke="currentColor"
If I set currentColor with opacity -

theme: SvgTheme(
                currentColor: Colors.white.withOpacity(0.5),
              ),

opacity is ignored

@stuartmorgan-g stuartmorgan-g added p: flutter_svg The Flutter SVG drawing packages package flutter/packages repository. See also p: labels. labels Nov 13, 2024
@jonahwilliams jonahwilliams added the P3 Issues that are less important to the Flutter project label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: flutter_svg The Flutter SVG drawing packages P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants