In this article, we are going to learn how to use SVG in Flutter. So let’s get started.
What is SVG and why to use ?
You may be heard of something like SVG icons because of their popularity among designers or developers.
SVG stands for Scalable Vector Graphics which is an XML-based markup language for describing 2D-based vector graphics. SVG images can be rendered at any size without losing quality.
Read more about Flutter app development services from mobikul.
Using SVG’s in Flutter
If you use SVGs like others i.e. png, jpg into your Image.network or Image.asset, you will get an image codec error.
1 2 3 |
Image.network( "https://www.svgrepo.com/show/217740/android.svg", ) // an image codec error |
Because they are not supported yet.
Fortunately, there is a solution from the Flutter community, the flutter_svg package.
We start by adding this package to pubspec.yaml:
1 2 3 4 |
dependencies: flutter: sdk: flutter flutter_svg: ^1.0.2 |
After that, import it and use it like any other widget into your Flutter project:
1 |
import 'package:flutter_svg/flutter_svg.dart'; |
I have used this SVG: https://www.svgrepo.com/show/217740/android.svg
1 2 3 |
SvgPicture.network( "https://www.svgrepo.com/show/217740/android.svg", ) |
Issue with this package
If you use SVG that contains <style> element your SVG will be rendered into the flutter app as there are no <style> elements in your SVG or colorless because this package doesn’t support CSS style in SVG yet. You can use inline styles in your SVG which are supported by this package as detailed here.
Thanks for reading.
Happy Coding 🙂