导航菜单
首页 >  assets/sendfiles.svg  > flutter

flutter

flutter_svg #

Pub

Flutter Logo which can be rendered by this package!

Draw SVG files using Flutter.

Getting Started #

Basic usage (to create an SVG rendering widget from an asset):

final String assetName = 'assets/image.svg';final Widget svg = SvgPicture.asset( assetName, semanticsLabel: 'Acme Logo');

You can color/tint the image like so:

final String assetName = 'assets/up_arrow.svg';final Widget svgIcon = SvgPicture.asset( assetName, colorFilter: ColorFilter.mode(Colors.red, BlendMode.srcIn), semanticsLabel: 'A red up arrow');

The default placeholder is an empty box (LimitedBox) - although if a heightor width is specified on the SvgPicture, a SizedBox will be used instead(which ensures better layout experience). There is currently no way to show anError visually, however errors will get properly logged to the console in debugmode.

You can also specify a placeholder widget. The placeholder will display duringparsing/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 = SvgPicture.asset( assetName,);final Widget networkSvg = SvgPicture.network( 'https://site-that-takes-a-while.com/image.svg', semanticsLabel: 'A shark?!', placeholderBuilder: (BuildContext context) => Container( padding: const EdgeInsets.all(30.0), child: const CircularProgressIndicator()),);

If you'd like to render the SVG to some other canvas, you can do something like:

import 'package:flutter_svg/flutter_svg.dart';final String rawSvg = '''...''';final PictureInfo pictureInfo = await vg.loadPicture(SvgStringLoader(rawSvg), null);// You can draw the picture to a canvas:canvas.drawPicture(pictureInfo.picture);// Or convert the picture to an image:final ui.Image image = pictureInfo.picture.toImage(...);pictureInfo.picture.dispose();

The SvgPicture helps to automate this logic, and it provides some conveniencewrappers for getting assets from multiple sources. Unlike the vector_graphicspackage, this package does not render the data to an Image at any point.This carries a performance penalty for some common use cases, but also allowsfor more flexibility around scaling.

Precompiling and Optimizing SVGs #

The vector_graphics backend supports SVG compilation which produces a binaryformat that is faster to parse and can optimize SVGs to reduce the amount ofclipping, masking, and overdraw. The SVG compilation is provided bypackage:vector_graphics_compiler.

dart run vector_graphics_compiler -i assets/foo.svg -o assets/foo.svg.vec

The output foo.svg.vec can be loaded using the default constructor ofSvgPicture.

import 'package:flutter_svg/flutter_svg.dart';import 'package:vector_graphics/vector_graphics.dart';final Widget svg = SvgPicture( const AssetBytesLoader('assets/foo.svg.vec'));Check SVG compatibility #

An SVG can be tested for compatibility with the vector graphics backend byrunning the compiler locally to see if any errors are thrown.

dart run vector_graphics_compiler -i $SVG_FILE -o $TEMPORARY_OUTPUT_TO_BE_DELETED --no-optimize-masks --no-optimize-clips --no-optimize-overdraw --no-tessellateRecommended Adobe Illustrator SVG Configuration #In Styling: choose Presentation Attributes instead of Inline CSS because CSS is not fully supported.In Images: choose Embded not Linked to other file to get a single svg with no dependency to other files.In Objects IDs: choose layer names to add every layer name to svg tags or you can use minimal,it is optional.Export configurationSVG sample attribution #

SVGs in /assets/w3samples pulled from W3 sample files

SVGs in /assets/deborah_ufw provided by @deborah-ufw

SVGs in /assets/simple are pulled from trivial examples or generated to testbasic functionality - some of them come directly from the SVG 1.1 spec. Somehave also come or been adapted from issues raised in this repository.

SVGs in /assets/wikimedia are pulled from Wikimedia Commons

Android Drawables in /assets/android_vd are pulled from Android Documentationand examples.

The Flutter Logo created based on the Flutter Logo Widget © Google.

The Dart logo is fromdartlang.org© Google

SVGs in /assets/noto-emoji are from Google i18n noto-emoji,licensed under the Apache license.

Please submit SVGs that can't render properly (e.g. that don't render here theway they do in chrome), as long as they're not using anything "probably out ofscope" (above).

Commemoration #

This package was originally authored byDan Field and has been forked herefrom dnfield/flutter_svg.Dan was a member of the Flutter team at Google from 2018 until his deathin 2024. Dan’s impact and contributions to Flutter were immeasurable, and wehonor his memory by continuing to publish and maintain this package.

相关推荐: