svg

package
v1.17.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: BSD-3-Clause Imports: 23 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package svg draws SVG images in Glutter apps, with the widgets of Flutter's flutter_svg package.

SvgPicture loads an SVG from an asset, a string, memory, a file or the network, parses it once (the Cache keeps the parsed document by source and theme), and draws it scaled to its box with a BoxFit and an Alignment. Parse, Document.Picture and its relatives are for an app that draws an SVG itself, in a CustomPainter.

The package is apart from material, as flutter_svg is apart from Flutter: it is not re-exported by it. The parser and the painter are in internal/svg and are plain Go, so SVG looks the same on every platform. SPEC §26 says what is parsed and what is drawn.

flutter_svg: svg.dart

Index

Constants

View Source
const (
	// TagSvgParseFailed is the tag of an XML that is not well formed; the metadata are "line" and "column".
	TagSvgParseFailed = svg.TagSvgParseFailed
	// TagSvgNotSvg is the tag of a document whose root element is not svg; the metadata is "element".
	TagSvgNotSvg = svg.TagSvgNotSvg
	// TagSvgInvalidValue is the tag of a value that must be valid and is not, such as a viewBox with a negative size.
	TagSvgInvalidValue = svg.TagSvgInvalidValue
)

The tags of the errors of the package (SPEC §26.5, rm4n0s/errors).

View Source
const DefaultCacheMaximumSizeBytes = 16 << 20

DefaultCacheMaximumSizeBytes is the limit of a Cache that was given none: 16 MiB, about a thousand icons.

flutter_svg: cache.dart Cache.maximumSize

View Source
const (
	// TagSvgLoadFailed is the tag of an asset, a file or a URL that could not be read; the metadata is "source" (the
	// asset key, the path or the URL), and "statusCode" for a status of HTTP other than 200. The cause, if any, is the
	// Unwrap of the error.
	TagSvgLoadFailed = "SvgLoadFailed"
)

The tags of the errors that this file makes (rm4n0s/errors, R5).

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache keeps the parsed documents of the SvgPicture widgets, by source and theme, so that the same icon in a list is parsed once. It is a least recently used cache with a limit in bytes. It is safe to use from several goroutines.

There is no global cache. An app has one for each binding (CacheOf), or gives its own to a subtree with NewSvgCacheScope.

Deviation: flutter_svg's cache is the global svg.cache and counts entries (100 by default); this one belongs to the binding and counts bytes, as SPEC §26.1 says (SPEC §12.10).

flutter_svg: cache.dart Cache

func CacheOf

func CacheOf(bc widgets.BuildContext) *Cache

CacheOf is the cache that the widgets in the context use: the one of the closest SvgCacheScope, else the cache of the binding, made on first use with the default limit. The context depends on the scope.

flutter_svg: svg.dart Svg.cache

func NewCache

func NewCache(o ...CacheOpts) *Cache

NewCache creates an empty cache.

flutter_svg: cache.dart Cache.new

func (*Cache) Clear

func (c *Cache) Clear()

Clear evicts every document. The widgets that show one keep it; they load it again when they are rebuilt with another source or theme.

flutter_svg: cache.dart Cache.clear

func (*Cache) Count

func (c *Cache) Count() int

Count is the number of documents in the cache.

flutter_svg: cache.dart Cache.count

func (*Cache) MaximumSizeBytes

func (c *Cache) MaximumSizeBytes() int

MaximumSizeBytes is the most memory that the cache keeps.

flutter_svg: cache.dart Cache.maximumSize

func (*Cache) SetMaximumSizeBytes

func (c *Cache) SetMaximumSizeBytes(n int)

SetMaximumSizeBytes changes the limit. If the documents in the cache are larger than the new limit, the least recently used ones are evicted at once; zero clears the cache.

flutter_svg: cache.dart Cache.maximumSize

func (*Cache) SizeBytes

func (c *Cache) SizeBytes() int

SizeBytes is the memory that the documents in the cache hold, as Document.ApproximateBytes estimates it.

flutter_svg: no counterpart

type CacheOpts

type CacheOpts struct {
	// MaximumSizeBytes is the most memory that the documents in the cache may hold, as Document.ApproximateBytes
	// estimates it; the default is DefaultCacheMaximumSizeBytes. Zero means that nothing is kept.
	MaximumSizeBytes foundation.Opt[int]
}

CacheOpts are the optional parameters of NewCache.

flutter_svg: cache.dart Cache.new

type DefaultSvgTheme

type DefaultSvgTheme struct {
	widgets.InheritedTheme
	// contains filtered or unexported fields
}

DefaultSvgTheme is the theme of the SvgPicture widgets below it that were not given one.

flutter_svg: default_theme.dart DefaultSvgTheme

func DefaultSvgThemeOf

func DefaultSvgThemeOf(bc widgets.BuildContext) (*DefaultSvgTheme, bool)

DefaultSvgThemeOf is the closest DefaultSvgTheme above the context, and false if there is none. The context depends on it.

flutter_svg: default_theme.dart DefaultSvgTheme.of

func NewDefaultSvgTheme

func NewDefaultSvgTheme(theme SvgTheme, child widgets.Widget, o ...widgets.WidgetOpts) *DefaultSvgTheme

NewDefaultSvgTheme creates the widget.

flutter_svg: default_theme.dart DefaultSvgTheme.new

func (*DefaultSvgTheme) Theme

func (t *DefaultSvgTheme) Theme() SvgTheme

Theme is the theme to apply.

flutter_svg: default_theme.dart DefaultSvgTheme.theme

func (*DefaultSvgTheme) UpdateShouldNotify

func (t *DefaultSvgTheme) UpdateShouldNotify(old widgets.Widget) bool

UpdateShouldNotify is true when the theme is another one.

flutter_svg: default_theme.dart DefaultSvgTheme.updateShouldNotify

func (*DefaultSvgTheme) Wrap

Wrap copies the theme around the child, for a route that is built outside its subtree.

flutter_svg: default_theme.dart DefaultSvgTheme.wrap

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document is a parsed SVG document, for one theme. It is never changed, so it can be drawn from several goroutines.

flutter_svg: no counterpart (vector_graphics PictureInfo)

func Parse

func Parse(data []byte, o ...ParseOpts) (*Document, error)

Parse reads an SVG document (XML, optionally compressed with gzip). It fails with SvgParseFailed when the XML is not well formed, SvgNotSvg when the root is not an svg element and SvgInvalidValue for a value that must be valid and is not; any other mistake of the document is ignored, as browsers do.

flutter_svg: no counterpart (vg.encodeSvg)

func (*Document) Desc

func (d *Document) Desc() string

Desc is the text of the desc element of the document.

flutter_svg: no counterpart

func (*Document) Picture

func (d *Document) Picture() *ui.Picture

Picture records the document into a picture of the document's size (Size), to draw at scale 1.

flutter_svg: no counterpart (vector_graphics PictureInfo.picture)

func (*Document) PictureAt

func (d *Document) PictureAt(scale float64) *ui.Picture

PictureAt is Picture for a picture that is drawn scale device pixels to a unit of the document.

flutter_svg: no counterpart

func (*Document) PictureWith

func (d *Document) PictureWith(o PictureOpts) *ui.Picture

PictureWith is PictureAt with the text engine of the app.

flutter_svg: no counterpart

func (*Document) Size

func (d *Document) Size() ui.Size

Size is the size of the picture: the width and height of the document, or its viewBox.

flutter_svg: no counterpart (vector_graphics PictureInfo.size)

func (*Document) Title

func (d *Document) Title() string

Title is the text of the title element of the document, which labels the picture for screen readers when the app gives no label.

flutter_svg: no counterpart

type ParseOpts

type ParseOpts struct {
	// Theme gives currentColor, em and ex; the zero value is the default theme.
	Theme SvgTheme
	// Locale picks the child of a switch element by its systemLanguage; the zero value is English.
	Locale ui.Locale
}

ParseOpts are the optional parameters of Parse.

flutter_svg: no counterpart

type PictureOpts

type PictureOpts struct {
	// Scale is the number of device pixels to a unit of the document (1 if it is not positive). It sets the
	// resolution of pattern tiles and filters, and the width of strokes that do not scale; the rest is vector.
	Scale float64
	// Text is the text engine that sets the text of the document, with the fonts of the app (the PlatformDispatcher's
	// TextEngine). With nil the text is set in the fonts that Glutter bundles.
	Text ui.ParagraphFactory
}

PictureOpts are the optional parameters of PictureWith.

flutter_svg: no counterpart

type SvgCacheScope

type SvgCacheScope struct {
	widgets.InheritedWidget
	// contains filtered or unexported fields
}

SvgCacheScope gives the SvgPicture widgets below it a Cache of their own, in place of the cache of the binding.

flutter_svg: no counterpart

func NewSvgCacheScope

func NewSvgCacheScope(cache *Cache, child widgets.Widget, o ...widgets.WidgetOpts) *SvgCacheScope

NewSvgCacheScope creates a scope whose descendants cache their documents in the cache.

flutter_svg: no counterpart

func (*SvgCacheScope) Cache

func (s *SvgCacheScope) Cache() *Cache

Cache is the cache of the scope.

flutter_svg: no counterpart

func (*SvgCacheScope) UpdateShouldNotify

func (s *SvgCacheScope) UpdateShouldNotify(old widgets.Widget) bool

UpdateShouldNotify says that the descendants load their documents again when the cache is another one.

flutter_svg: no counterpart

type SvgErrorBuilder

type SvgErrorBuilder func(bc widgets.BuildContext, err error) widgets.Widget

SvgErrorBuilder builds the widget that SvgPicture shows when its SVG could not be loaded or parsed. The error is the one that is reported to the app's error reporter too (SvgLoadFailed, SvgParseFailed, SvgNotSvg or SvgInvalidValue).

flutter_svg: no counterpart (vector_graphics VectorGraphic.errorBuilder)

type SvgPicture

type SvgPicture struct {
	widgets.StatefulWidget
	// contains filtered or unexported fields
}

SvgPicture is a widget that draws an SVG document.

The size of the widget comes from Width and Height, the constraints and the size of the document (its width and height, or its viewBox), as flutter_svg's does. The document is loaded and parsed once for all the widgets with the same source and theme (CacheOf), on a goroutine that ends when the widget leaves the tree; PlaceholderBuilder is shown meanwhile. A document that is in the cache already is drawn in the first frame.

The picture is recorded once for a document and scaled to the box of the widget. A document with a pattern, a filter or a stroke that does not scale (Document.ScaleDependent) is recorded at the scale it is drawn at: the device pixel ratio of the view times the scale of the fit, rounded up to a multiple of 1/8, and again when that changes.

Deviation: flutter_svg turns the SVG into vector_graphics's binary format on an isolate; this parses the SVG into a tree on a goroutine and records it into a ui.Picture (SPEC §26, §12.10).

flutter_svg: svg.dart SvgPicture

func NewSvgPictureAsset

func NewSvgPictureAsset(assetName string, o ...SvgPictureAssetOpts) *SvgPicture

NewSvgPictureAsset creates a widget that draws the SVG asset of a bundle: the one of the options or the binding's. The asset is loaded on a goroutine.

flutter_svg: svg.dart SvgPicture.asset

func NewSvgPictureFile

func NewSvgPictureFile(path string, o ...SvgPictureOpts) *SvgPicture

NewSvgPictureFile creates a widget that draws the SVG in the file. The file is read on a goroutine. On the web and on Android a path is not a file the app can read, and the widget shows its ErrorBuilder.

flutter_svg: svg.dart SvgPicture.file

func NewSvgPictureMemory

func NewSvgPictureMemory(bytes []byte, o ...SvgPictureOpts) *SvgPicture

NewSvgPictureMemory creates a widget that draws the SVG in the bytes (text, or compressed with gzip). It is parsed when the widget is first built.

flutter_svg: svg.dart SvgPicture.memory

func NewSvgPictureNetwork

func NewSvgPictureNetwork(url string, o ...SvgPictureNetworkOpts) *SvgPicture

NewSvgPictureNetwork creates a widget that draws the SVG at the URL. It is requested on a goroutine with the HTTP client of the binding and the headers. All network pictures are cached whatever the headers of the answer say.

flutter_svg: svg.dart SvgPicture.network

func NewSvgPictureString

func NewSvgPictureString(source string, o ...SvgPictureOpts) *SvgPicture

NewSvgPictureString creates a widget that draws the SVG in the string. It is parsed when the widget is first built.

flutter_svg: svg.dart SvgPicture.string

func (*SvgPicture) Alignment

func (w *SvgPicture) Alignment() painting.AlignmentGeometry

Alignment is how the picture is aligned in the box.

flutter_svg: svg.dart SvgPicture.alignment

func (*SvgPicture) CreateState

func (w *SvgPicture) CreateState() widgets.State

CreateState creates the state.

flutter_svg: svg.dart SvgPicture.build

func (*SvgPicture) Fit

func (w *SvgPicture) Fit() painting.BoxFit

Fit is how the picture is inscribed into the box.

flutter_svg: svg.dart SvgPicture.fit

func (*SvgPicture) Height

func (w *SvgPicture) Height() foundation.Opt[float64]

Height is the height the picture is given, if any.

flutter_svg: svg.dart SvgPicture.height

func (*SvgPicture) String

func (w *SvgPicture) String() string

String describes the source of the picture, as flutter_svg's loaders do ("SvgAssetLoader(icons/home.svg)").

flutter_svg: svg.dart SvgPicture.bytesLoader

func (*SvgPicture) Width

func (w *SvgPicture) Width() foundation.Opt[float64]

Width is the width the picture is given, if any.

flutter_svg: svg.dart SvgPicture.width

type SvgPictureAssetOpts

type SvgPictureAssetOpts struct {
	SvgPictureOpts
	// Bundle is the bundle to load the asset from; the default is the root bundle of the binding.
	Bundle services.AssetBundle
	// Package is the package that holds the asset; the key is then "packages/<Package>/<assetName>".
	Package string
}

SvgPictureAssetOpts are the optional parameters of NewSvgPictureAsset.

flutter_svg: svg.dart SvgPicture.asset

type SvgPictureNetworkOpts

type SvgPictureNetworkOpts struct {
	SvgPictureOpts
	// Headers are the HTTP headers of the request.
	Headers map[string]string
}

SvgPictureNetworkOpts are the optional parameters of NewSvgPictureNetwork.

flutter_svg: svg.dart SvgPicture.network

type SvgPictureOpts

type SvgPictureOpts struct {
	// Key is the key of the widget.
	Key foundation.Key
	// Width is the width of the picture. If only one of Width and Height is set, the other keeps the aspect ratio of
	// the picture; with neither, the size of the picture is the size of the widget, within the constraints.
	Width foundation.Opt[float64]
	// Height is the height of the picture.
	Height foundation.Opt[float64]
	// Fit is how the picture is inscribed into the space the layout gives it; the default is BoxFitContain.
	Fit painting.BoxFit
	// Alignment is how the picture is aligned in that space; the default is AlignmentCenter.
	Alignment painting.AlignmentGeometry
	// MatchTextDirection flips the picture horizontally in right to left contexts.
	MatchTextDirection bool
	// AllowDrawingOutsideViewBox lets the picture draw outside its viewBox; by default it is clipped to it. Use it
	// with caution: the picture may draw beyond the space it was given.
	AllowDrawingOutsideViewBox bool
	// ClipBehavior is how the picture is clipped to the widget when it is larger than it (Fit cover, or
	// an alignment that crops); the default is ClipHardEdge.
	ClipBehavior ui.Clip
	// ColorFilter is applied to the picture as a whole.
	ColorFilter *ui.ColorFilter
	// Theme gives currentColor, em and ex to the document; the default is that of the closest DefaultSvgTheme, else the
	// default theme.
	Theme foundation.Opt[SvgTheme]
	// PlaceholderBuilder builds the widget shown while the SVG loads; the default is an empty box of Width and Height.
	PlaceholderBuilder widgets.WidgetBuilder
	// ErrorBuilder builds the widget shown when the SVG could not be loaded or parsed; the default is an empty box
	// of Width and Height. The error is reported to the app's error reporter in both cases.
	ErrorBuilder SvgErrorBuilder
	// SemanticsLabel is the label that describes the picture to screen readers; the default is the title element of the
	// document, if it has one.
	SemanticsLabel foundation.Opt[string]
	// ExcludeFromSemantics leaves the picture out of the semantics tree, and SemanticsLabel with it.
	ExcludeFromSemantics bool
}

SvgPictureOpts are the optional parameters of the constructors of SvgPicture.

flutter_svg: svg.dart SvgPicture.new

type SvgTheme

type SvgTheme struct {
	// CurrentColor is the value of currentColor at the root of the document (black by default).
	CurrentColor ui.Color
	// FontSize is the font size of the root and the value of 1em there (14 by default).
	FontSize float64
	// XHeight is the value of 1ex at the root (half of FontSize by default).
	XHeight float64
}

SvgTheme gives a document the values that its text leaves to the app: what currentColor is, and what an em and an ex are. A document is parsed for one theme, so the theme is part of the key of the cache.

The zero value is the default theme: black, a font size of 14 and an x-height of half of it. A zero field is replaced by its default.

flutter_svg: loaders.dart SvgTheme

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL