flutter_svg icon indicating copy to clipboard operation
flutter_svg copied to clipboard

Support the <pattern> element

Open rahuldange09 opened this issue 5 years ago • 16 comments

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="273" width="298"> <defs> <image id="1a54d514-63f6-47f8-a0f4-ed80dsdddsf04a"

Are this type of images supported ? And if how to use them?

rahuldange09 avatar Feb 04 '20 13:02 rahuldange09

Yes.

Please share an svg that fails to render.

dnfield avatar Feb 04 '20 14:02 dnfield

Here please have a look https://easyupload.io/rcu2hb

rahuldange09 avatar Feb 04 '20 18:02 rahuldange09

This SVG uses <pattern>, which is why it doesn't work. It just so happens someone else asked me a very similar question yesterday. I'm not sure when I'll have time to look more about this, but PRs are welcome to implement it. Here's the relevant part of the spec and MDN:

https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Patterns

https://developer.mozilla.org/en-US/docs/Web/SVG/Element/pattern

https://www.w3.org/TR/SVG11/pservers.html#Patterns

dnfield avatar Feb 04 '20 18:02 dnfield

FWIW, you can generally avoid using pattern elements in SVGs, particularly to display images, but there doesn't seem to be a good reason yet to not support this in flutter_svg.

dnfield avatar Feb 04 '20 18:02 dnfield

Here's an example SVG that should render correctly, pulled from MDN:

<svg viewBox="0 0 230 100" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="star" viewBox="0,0,10,10" width="10%" height="10%">
      <polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2"/>
    </pattern>
  </defs>

  <circle cx="50"  cy="50" r="50" fill="url(#star)"/>
  <circle cx="180" cy="50" r="40" fill="none" stroke-width="20" stroke="url(#star)"/>
</svg>

dnfield avatar Feb 04 '20 18:02 dnfield

Here's an example SVG that should render correctly, pulled from MDN:

<svg viewBox="0 0 230 100" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="star" viewBox="0,0,10,10" width="10%" height="10%">
      <polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2"/>
    </pattern>
  </defs>

  <circle cx="50"  cy="50" r="50" fill="url(#star)"/>
  <circle cx="180" cy="50" r="40" fill="none" stroke-width="20" stroke="url(#star)"/>
</svg>

Oh I see. So for now I need to convert them.

rahuldange09 avatar Feb 05 '20 06:02 rahuldange09

FWIW, you can generally avoid using pattern elements in SVGs, particularly to display images, but there doesn't seem to be a good reason yet to not support this in flutter_svg.

@dnfield, I was trying to place an image covered to its viewbox, preserving aspect ratio. By default, it fits to the viewbox. I found this stackoverflow link, how to achieve the same. But since, pattern is not supported, can I achieve the same without using pattern and image tags only ?

code I am trying to render : -

<pattern id="a" height="100%" preserveAspectRatio="xMidYMid slice" viewBox="0 0 56 54" width="100%”>
<image width="56" height="54" rx="2.194"  x="17" y="20" xlink:href="$base64" />
</pattern>

I tried this, but this doesn't scale properly. <image width="100%" height="100%" rx="2.194" x="17" y="20" xlink:href="$base64" preserveAspectRatio="xMidYMid slice"/>

klaes-ashford avatar May 26 '20 08:05 klaes-ashford

@dnfield I have a similar problem with svg network, so I cann't edit code of svg file. How to fix it? :(

gympro159 avatar Dec 23 '20 04:12 gympro159

Guys any update on this issue?

ali-raza-flutter avatar Jul 01 '21 13:07 ali-raza-flutter

same here. Probably switching to a PNG for now but would be good to have it fixed since SVG scales better.

thiagorobertodc avatar Jul 14 '21 18:07 thiagorobertodc

I was only facing this issue in two images in my whole project.

So I use this website to convert those images from PNG to SVJ Now they work perfectly and my code is also consistent.

Leaving it here if someone is also facing this issue and want a quick fix

ali-raza-flutter avatar Jul 14 '21 18:07 ali-raza-flutter

God bless you @ali-raza-flutter thanks a lot

thiagorobertodc avatar Jul 14 '21 19:07 thiagorobertodc

any updates on this please?

srivastavarnav avatar Aug 23 '21 12:08 srivastavarnav

any update?

marcoberetta96 avatar Dec 02 '21 12:12 marcoberetta96

any news on this please?

guigzp avatar Apr 05 '22 19:04 guigzp

I was only facing this issue in two images in my whole project.

So I use this website to convert those images from PNG to SVJ Now they work perfectly and my code is also consistent.

Leaving it here if someone is also facing this issue and want a quick fix

The problem I have with this is that the PNGs from Figma already have low quality for some reason, only the SVGs look crisp, but the problem surfaces with 30% of them so far, and moving the definitions hasn´t helped. So if I convert a blurry PNG I get a blurry SVG...

Pedanfer avatar Apr 30 '22 17:04 Pedanfer

Any Updates

Aryan1g avatar Oct 12 '22 10:10 Aryan1g

I'm working on a new release that will support this. Alternatively, vector_graphics supports this today.

dnfield avatar Oct 12 '22 13:10 dnfield

Any Updates to this ?

Navil avatar Nov 17 '22 11:11 Navil

Hope this tool can help you all friends .

https://svg2widget.web.app/

wee can convert the svg file as a flutter widget by using this tool and it provides the way for best use svg to the flutter developers . find out the sample project and performance metrics here . https://github.com/maheshwaran-p/samplesvg

maheshwaran-p avatar Nov 17 '22 16:11 maheshwaran-p