How to add SVG image in Flutter – Using the Scalable Vector Graphics (SVG) image formats, designers can create visuals that look great at any size. The flutter_svg package allows images into 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: SVG image in Flutter makes use of an efficient rendering svg images 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.
After adding the dependency, you can use the SvgPicture widget to display SVG image in 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', // string assetname semanticsLabel: 'My SVG Image', ); } }
this example, we’re using the SvgPicture.asse
t constructor to called flutter svg 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 supports 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 in Flutter 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 code image. Here’s a example:
import 'package:flutter_svg/flutter_svg.dart'; class MySvgWidget extends StatelessWidget { @override Widget build(BuildContext context) { return SvgPicture.asset( 'assets/my_icon.svg', color: Colors.red, // change the color here ); } }
The SVG asset at the given path is loaded by the SvgPicture widget, and the color property is set to Colors.red. To alter the SVG’s hue, just swap out Colors.red
for a different hue.
The colorBlendMode
property allows you to specify how colors from multiple paths or groups within an SVG image in Flutter file should be blended together. For example:
import 'package:flutter_svg/flutter_svg.dart'; class MySvgWidget extends StatelessWidget { @override Widget build(BuildContext context) { return SvgPicture.asset( 'assets/my_icon.svg', color: Colors.red, // change the color here colorBlendMode: BlendMode.srcIn, ); } }
The colorBlendMode
attribute is changed to BlendMode.srcIn
, which replaces the old color fully. Try different mix modes for different effects.
Cache Svg Image Flutter
The CachedNetworkImage
widget, included in the cached network image package, can be used to save an SVG image in Flutter cache.
Here’s a example:
import 'package:cached_network_image/cached_network_image.dart'; class MySvgWidget extends StatelessWidget { @override Widget build(BuildContext context) { return CachedNetworkImage( imageUrl: 'https://example.com/icon.svg', placeholder: (context, url) => CircularProgressIndicator(), errorWidget: (context, url, error) => Icon(Icons.error), ); } }
The CachedNetworkImage
widget retrieves the SVG file at the specified URL and stores it locally. As the image is loading, the errorWidget
argument defines a widget to display if an error occurs, and the placeholder parameter specifies a widget to display during successful image loading.
The CachedNetworkImage
widget accepts extra parameters, such as cacheManager and maxAge
, that allow you to fine-tune the caching behavior.
CachedNetworkImage( imageUrl: 'https://example.com/my_icon.svg', cacheManager: CacheManager( Config( 'my_cache_key', stalePeriod: Duration(days: 30), maxNrOfCacheObjects: 100, repo: JsonCacheInfoRepository(databaseName: 'my_cache_database'), fileService: HttpFileService(), ), ), placeholder: (context, url) => CircularProgressIndicator(), errorWidget: (context, url, error) => Icon(Icons.error), );
To control how data is stored in cache, a Config object is sent into the CacheManager
constructor. The maxNrOfCacheObjects
parameter caps the amount of items that can be cached, while the stalePeriod parameter determines how long cached objects remain valid before they are declared stale. The cache manager package is used here, specifically the JsonCacheInfoRepository and HttpFileService
classes.
Output
Conclusion
With SVG files, you can make crisp, high-resolution visuals in Flutter that scale well. Display SVGs in your app with the flutter svg package and modify their appearance with the available options.