Flutter SVG rendering
DartFlutterDart Problem Overview
I tried adding an image with a SVG source to my flutter application.
new AssetImage("assets/images/candle.svg"))
But I didn't get any visual feedback. How can I render an SVG picture in Flutter?
Dart Solutions
Solution 1 - Dart
Fonts are a great option for a lot of cases.
I've been working on a library to render SVGs on a canvas, available here: https://github.com/dnfield/flutter_svg
The API as of right now would look something like
new SvgPicture.asset('asset_name.svg')
to render asset_name.svg (sized to its parent, e.g. a SizedBox
). You can also specify a color
and blendMode
for tinting the asset..
It's now available on pub and works with a minimum of Flutter version 0.3.6. It handles a bunch of cases but not everything - see the GitHub repo for updates and to file issues.
The original GitHub issue referenced by Colin Jackson is really not meant to be primarily focused on SVG in Flutter. I opened another issue here for that: https://github.com/flutter/flutter/issues/15501
Solution 2 - Dart
Flutter does not currently support SVG. Follow issue 1831 for updates.
If you absolutely need vector drawing you can see the Flutter Logo widget as an example of how to draw using the Canvas
API, or rasterize your image on the native side and pass it to Flutter as a bitmap, but for now your best bet is probably to embed high-resolution rasterized asset images.
Solution 3 - Dart
Developers from the Flutter community created a lib to handle svg files. We can use it as
new SvgPicture.asset(
'assets/images/candle.svg',
height: 20.0,
width: 20.0,
allowDrawingOutsideViewBox: true,
),
I found a small example of SVG implementation here.
Solution 4 - Dart
The work around for now is use fonts
fonts:
- family: icomoon
fonts:
- asset: assets/fonts/icomoon.ttf
Useage
static const IconData TabBarHome= const IconData(0xe906, fontFamily: 'icomoon');
static const IconData TabBarExplore= const IconData(0xe902, fontFamily: 'icomoon');
Replace the ### eg (906)
Solution 5 - Dart
You can follow the below steps
- visit http://fluttericon.com
- upload your SVG icons
- click on download button
- you will get two files
- iconname.dart
2. iconname.ttf font file
- use this file in flutter & import iconname.dart
Solution 6 - Dart
You can use this library for rendering SVG Images - https://pub.dev/packages/flutter_svg
Example -
Container(
child: SvgPicture.asset("assets/images/yourImage.svg")
)
Solution 7 - Dart
step 1. add dependency in your pubspec.yaml
dependencies:
flutter_svg: any
the Advantage of version any is you can use in any SDK version
step 2. import the flutter svg package in your app
import 'package:flutter_svg/flutter_svg.dart';
step 3. just use like below
SvgPicture.asset(
'assets/images/bm-icon1.svg',
width: 18.0,
height: 18.0,
),
Solution 8 - Dart
You can use flare to create animations and just import .flr as an asset
import 'package:flare_flutter/flare_actor.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new FlareActor("assets/Filip.flr", alignment:Alignment.center, fit:BoxFit.contain, animation:"idle");
}
}
visit flare_flutter https://pub.dev/packages/flare_flutter
Solution 9 - Dart
Of possible interest: There's a newer Flutter SVG library called jovial_svg. I wrote it to handle a more robust subset of SVG than what I was able to find on pub.dev at the time. While I was at it, I also made a binary format that SVG assets can be compiled to. It's a little more compact, and about 10x faster to read, so it's a good choice for assets you package with your application.
The library is at https://pub.dev/packages/jovial_svg
Solution 10 - Dart
flutter support special type of svg
if have style tag then not load svg image in flutter flutter support inline styling in svg
Solution 11 - Dart
Svg in flutter https://pub.dev/packages/flutter_svg
-
add package in pubspec.yaml
dependencies: flutter_svg: any
assets:
-assets/images/ -
to insert svg.
SvgPicture.asset('assets/images/password-icon.svg', width: 24, height: 29.2),