How to add SVG image in Flutter – Using the Scalable Vector Graphics (SVG) file format, designers can create visuals that look great at any size. The flutter_svg package allows SVG files to be utilized as assets and rendered in Flutter.
Features flutter_svg
- Scalable Vector Graphics (SVG) support: With the help of Flutter_svg, programmers may include scalable and cross-platform SVG files into their Flutter apps.
- Customizable widgets:Using Flutter svg, programmers may include scalable and cross-platform SVG files into their Flutter apps.
- Efficient rendering: Flutter svg makes use of an efficient rendering engine that can render SVG pictures rapidly and with great quality.
- Easy to use: Developers like Flutter svg because it’s simple and works well with Flutter.
- Cross-platform compatibility:Flutter_svg is adaptable for developers that wish to build apps for Android and iOS.
- Open source: Developers may freely use and contribute to Flutter_svg.
- Support for advanced SVG features: Developers are given the freedom to create sophisticated and aesthetically pleasing graphics because to Flutter svg’s support for advanced SVG features like gradients, filters, and masks.
Flutter requires the flutter_svg requirement, which you can add to your pubspec.yaml file:
After adding the dependency, you can use the SvgPicture widget to display SVG files in your Flutter application.
dependencies: flutter_svg: ^0.22.0
example:
import 'package:flutter_svg/flutter_svg.dart'; class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return SvgPicture.asset( 'assets/my_svg_file.svg', semanticsLabel: 'My SVG Image', ); } }
In this example, we’re using the SvgPicture.asset constructor to load an SVG file from the assets folder and display it in our widget. The semanticsLabel argument is optional and provides a description of the image for accessibility purposes.
You may further adjust the look of SVG pictures in your Flutter app with the help of the flutter svg package capabilities like color and opacity modification, scaling, and caching.
How to add SVG image in Flutter
To add an SVG image in Flutter, follow these steps:
- Install the flutter_svg package by adding it to your pubspec.yaml file and running
flutter pub get
. - Import the package in your Dart file where you want to use SVG image.
import 'package:flutter_svg/flutter_svg.dart;'
- Use the SvgPicture.asset() method to load an SVG file from your assets folder.
SvgPicture.asset( 'assets/images/my_image.svg', height: 100, width: 100, );
- You can also use the SvgPicture.network() method to load an SVG file from a URL.
SvgPicture.network( 'https://example.com/my_image.svg', height:100, width:100, );
- You can also customize the color of the SVG image using the color property.
SvgPicture.asset( 'assets/images/my_image.svg', height: 100, width: 100, color: Colors.blue, );
That’s it! Now you can easily add SVG images in your Flutter app.
Change colors flutter svg
The color attribute of the SvgPicture
widget in Flutter may be used to modify the color
of an SVG image. Here’s a case in point:
import 'package:flutter_svg/flutter_svg.dart'; class MySvgImage extends StatelessWidget { @override Widget build(BuildContext context) { return SvgPicture.asset( 'assets/my_image.svg', color: Colors.red, ); } }
In the above code snippet, the SvgPicture
widget is used to display a red SVG picture loaded from the assets folder. You are free to substitute whatever
color you’d like for Colors.red. If you’re using an AnimatedContainer or another animation widget, you can also animate the color of an SVG image by setting its color property to a keyframe. A new color value may be entered into the color
property to alter the SVG’s hue.
Text direction
Flutter_svg supports text directionality in SVG files using the textDirection
property of the SvgPicture
widget.
example:
import 'package:flutter_svg/flutter_svg.dart'; class MySvgImage extends StatelessWidget { @override Widget build(BuildContext context) { return SvgPicture.asset( 'assets/my_image.svg', textDirection: TextDirection.rtl, ); } }
The SvgPicture
widget textDirection
attribute is set to TextDirection.rtl
to show SVG text right-to-left. To show SVG text left-to-right, set textDirection to TextDirection.ltr
. The textDirection
parameter only affects text in the SVG file. Modifying the SVG file changes the orientation of other components.
PlaceholderBuilder Flutter_svg
In Flutter_svg, the placeholderBuilder
property of the SvgPicture
widget allows you to specify a widget that will be displayed while the SVG image is being loaded.
example:
import 'package:flutter_svg/flutter_svg.dart'; class MySvgImage extends StatelessWidget { @override Widget build(BuildContext context) { return SvgPicture.asset( 'assets/my_image.svg', placeholderBuilder: (BuildContext context) => CircularProgressIndicator(), g); } }
A CircularProgressIndicator
widget is set as the SvgPicture
widget placeholderBuilder
. This widget will appear while the SVG image loads. The graphic replaces the CircularProgressIndicator
after loading. . PlaceholderBuilder is optional. The SVG image will load without a placeholderBuilder
widget. A CircularProgressIndicator
widget is set as the SvgPicture widget’s placeholderBuilder. This widget will appear while the SVG image loads. The graphic replaces the CircularProgressIndicator
after loading. You may use an Image.asset
widget or a custom widget in the placeholderBuilder
widget. PlaceholderBuilder is optional. The SVG image will load without a placeholderBuilder widget.
Conclusion
Flutter’s support for SVG (Scalable Vector Graphics) allows for enhanced resolution independence, faster loading times, and more adaptable and interactive UIs. To simplify the process of incorporating SVG graphics into your app, Flutter provides a number of libraries and packages, such as flutter svg and svg path parser.
You should be aware of the potential performance impact of utilizing SVG in Flutter and optimize the SVG files accordingly, for as by deleting extraneous code and reducing file size. Best practices for SVG usage include things like using semantic markup and maintaining a consistent design.