SVG images in Flutter

Hasnain Mirrani
3 min readMay 9, 2023

--

Why we should use SVG in flutter?

How to implement SVG in Flutter ?

SVG Animation

SVG stands for Scalable Vector Graphics, which is a vector image format based on XML markup. In Flutter, you can use SVG images by using the flutter_svg package. Here are some benefits of using SVG images in Flutter:

Flutter SVG Implementation.

SVG stands for Scalable Vector Graphics, which is a vector image format based on XML markup. In Flutter, you can use SVG images by using the flutter_svg package. Here are some benefits of using SVG images in Flutter:

  • Scalability: SVG images are vector images, which means they can be scaled up or down without losing quality. This is especially useful for designing interfaces that need to support different screen sizes and resolutions.
  • Small size: SVG images are typically smaller in size compared to raster images, which can help reduce the overall size of your app.
  • Customization: SVG images can be easily customized by changing the properties of the SVG elements using CSS or code.
  • Animations: SVG images can be animated using CSS or code, which can help bring your interfaces to life.

Here is an example of how to use the flutter_svg package to display an SVG image in Flutter:

First, add the flutter_svg package to your project by adding the following line to your pubspec.yaml file:

dependencies:
flutter_svg: ^0.22.0

Next, import the flutter_svg package in your Dart code:

import 'package:flutter_svg/flutter_svg.dart';

Then, you can use the SvgPicture.asset() method to display an SVG image. For example:

SvgPicture.asset(
'assets/images/example.svg',
width: 200,
height: 200,
),

In this example, the SvgPicture.asset() method is used to load an SVG image from the assets/images directory and display it with a width and height of 200. You can also customize the SVG image further using properties such as color, alignment, fit, and more.

SVG Animation

To animate the SVG image, you can use the AnimatedSvg() widget provided by the flutter_svg package. This widget allows you to specify the animation controller, duration, and curve for the animation. For example:

import 'package:flutter_svg/flutter_svg.dart';

AnimatedSvg(
'assets/images/animated.svg',
duration: const Duration(seconds: 2),
curve: Curves.easeInOut,
animationController: AnimationController(
vsync: this,
duration: const Duration(seconds: 5),
),
),

In this example, the AnimatedSvg() widget is used to load the animated.svg file and animate it over a period of 5 seconds with an ease-in-out curve. The vsync property is set to this to use the TickerProviderStateMixin for the animation controller.

Finally, you can use the animationController to control the animation. For example, you can start the animation by calling animationController.forward():

final AnimationController _animationController = AnimationController(
vsync: this,
duration: const Duration(seconds: 5),
);

@override
void initState() {
super.initState();
_animationController.forward();
}

You can also use other methods such as reverse() and repeat() to control the animation.

By following these steps, you can add SVG animation to your Flutter app and create visually appealing and engaging interfaces.

for more information https://pub.dev/packages/flutter_svg

--

--

Hasnain Mirrani

Update the lattest and well explain All about Flutter make you from Zero to Hero in Flutter. follow https://www.linkedin.com/in/hasnain-mirrani-b47ab7131