Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter flutter logo svgs

svgs parsing, rendering, and widget library for flutter.

getting started

this is a dart-native rendering library. issues/prs will be raised in flutter and flutter/engine as necessary for features that are not good candidates for dart implementations (especially if they’re impossible to implement without engine support). however, not everything that skia can easily do needs to be done by skia; for example, the path parsing logic here isn’t much slower than doing it in native, and skia isn’t always doing low level gpu accelerated work where you might think it is (e.g. dash paths).

all of the svgs in the assets/ folder (except the text related one(s)) now have corresponding pngs in the golden/ folder that were rendered using flutter test tool/gen_golden.dart and compared against their rendering output in chrome. automated tests will continue to compare these to ensure code changes do not break known-good renderings.

basic usage (to create an svg rendering widget from an asset):

final string assetname = 'assets/image.svg';
final widget svg = new svgpicture.asset(
  assetname,
);

you can color/tint the image like so:

final string assetname = 'assets/icon.svg';
final widget svgicon = new svgpicture.asset(
  assetname,
  color: colors.red,
);

the default placeholder is an empty box (limitedbox) – although if a height or
width is specified on the svgpicture, a sizedbox will be used instead (which
ensures better layout experience). there is currently no way to show an
error visually, however errors will get properly logged to the console in debug
mode.

you can also specify a placeholder widget. the placeholder will display during
parsing/loading (normally only relevant for network access).

// will print error messages to the console.
final string assetname = 'assets/image_that_does_not_exist.svg';
final widget svg = new svgpicture.asset(
  assetname,
);

final widget networksvg = new svgimage.network(
  'https://site-that-takes-a-while.com/image.svg',
  loadingplaceholderbuilder: (buildcontext context) => new container(
      padding: const edgeinsets.all(30.0),
      child: const circularprogressindicator()),
);

if you’d like to render svgs to some other canvas, you can do something like:

import 'package:flutter_svg/flutter_svg.dart';
final string rawsvg = '''<svg viewbox="...">...</svg>''';
final drawableroot svgroot = await svg.fromsvgstring(rawsvg, rawsvg);

// if you only want the final picture output, just use
final picture picture = svgroot.topicture();

// otherwise, if you want to draw it to a canvas:
// optional, but probably normally desirable: scale the canvas dimensions to
// the svg's viewbox
svgroot.scalecanvastoviewbox(canvas);

// optional, but probably normally desireable: ensure the svg isn't rendered
// outside of the viewbox bounds
svgroot.clipcanvastoviewbox(canvas);
svgroot.draw(canvas, size);

the svgpicture helps to automate this logic, and it provides some convenience
wrappers for getting assets from multiple sources and caching the resultant
picture. it does not render the data to an image at any point; you certainly
can do that in flutter, but you then lose some of the benefit of having a vector
format to begin with.

while i’m making every effort to avoid needlessly changing the api, it’s not
guarnateed to be stable yet (hence the pre-1.0.0 version). to date, the biggest change
is deprecating the svgimage widgets in favor of svgpicture – it became very confusing
to maintain that name, as pictures are the underlying mechanism for rendering
rather than images.

use cases

  • your designer creates a vector asset that you want to include without
    converting to 5 different raster format resolutions.
  • your vector drawing is meant to be static and non (or maybe minimally)
    interactive.
  • you want to load svgs dynamically from network sources at runtime.
  • you want to paint svg data and render it to an image.

todo

this list is not very well ordered. i’m mainly picking up things that seem
interesting or useful, or where i’ve gotten a request to fix something/example
of something that’s broken.

  • [ ] text support.
  • [x] gradient support (linear: mostly done, radial: finishing this will require
    newer version of flutter).
  • [x] dash path support.
  • [ ] dash path with percentage dasharray values.
  • [ ] more svg samples to cover more complicated cases (getting there – please
    file issues for things that don’t work!).
  • [ ] display/visibility support.
  • [x] unit tests. in particular, tests that validate xml -> drawable*
    structures. (vastly improved as of 0.2.)
    this is getting there,
    just need to stay on top of it.
  • [ ] inheritance of inheritable properties (necessary? preprocess?
    significant progress, still some rough edges, particularly for definitions).
  • [ ] support for minimal css/styles? see also
    svgcleaner (partial – style
    attribute mostly supported).
  • [ ] xlink/ref support (necessary? partially supported for gradients).
  • [ ] glyph support?
  • [ ] markers.
  • [ ] filters/effects.
  • [ ] android vector drawable support (poc implementation so far).
  • [x] caching of image.
  • [ ] the xml parsing implementation is heavy for what this really needs. i’ve
    made efforts to keep the api forward-reading-only compatible to
    eventually be able to use a sax/xmlreader streaming style parser.
  • [x] color swapping/hue shifting/tinting of asset.
svgs

probably out of scope

  • smil animations. that just seems crazy. i think it’ll be possible to animate
    the svgs but probably in a more flutter driven way.
  • full (any?) css support – preprocess your svgs (perhaps with svgcleaner to
    get rid of all css?)
  • scripting in svgs
  • foreign elements
  • rendering properties/hints

download the full project for this post from the following button

this source is fully free for all time

download as zip


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.