flutter_svga
A Flutter package for parsing and rendering SVGA animations efficiently.
SVGA is a lightweight and powerful animation format used for dynamic UI effects in mobile applications.
π― Why flutter_svga?
β Actively Maintained
Unlike svgaplayer_flutter (archived since Feb 2023), flutter_svga is actively maintained with regular updates, bug fixes, and community support.
β‘ High Performance
- Intelligent caching system reduces network usage and load times
- Binary format smaller than JSON-based Lottie files
- Optimized rendering for smooth 60 FPS animations
π¨ Feature-Rich
- Audio playback integrated directly in animations
- Dynamic elements (replace text, images, colors at runtime)
- Full platform support (Android, iOS, Web, macOS, Linux, Windows)
π¦ Comparison
| Feature | flutter_svga | svgaplayer_flutter | Lottie |
|---|---|---|---|
| Status | β Active | β Archived (Feb 2023) | β Active |
| Caching | β Built-in intelligent cache | β No | β οΈ Manual |
| Audio Support | β Integrated | β No | β Yes |
| File Size | π’ Small (binary) | π’ Small (binary) | π‘ Larger (JSON) |
| Dynamic Elements | β Text, Images, Drawers | β οΈ Limited | β Yes |
| Platform Support | β All 6 platforms | β οΈ Mobile only | β All platforms |
| Performance | β‘ Optimized | β‘ Good | β‘ Good |
π Migrating from svgaplayer_flutter
Switching from the archived svgaplayer_flutter is simple:
1. Update Dependencies
dependencies:
# svgaplayer_flutter: ^2.2.0 # Remove old package
flutter_svga: ^0.0.12 # Add new package
2. Update Imports
// Old
// import 'package:svgaplayer_flutter/svgaplayer_flutter.dart';
// New
import 'package:flutter_svga/flutter_svga.dart';
3. API Usage (mostly compatible)
The API is similar, with some enhancements:
// Both packages use similar controller patterns
SVGAAnimationController controller = SVGAAnimationController(vsync: this);
// Loading remains the same
final videoItem = await SVGAParser.shared.decodeFromAssets("assets/animation.svga");
controller.videoItem = videoItem;
controller.repeat();
π Bonus: You now get automatic caching, audio support, and better performance with zero code changes!
π Features
βοΈ Parse and render SVGA animations in Flutter.
βοΈ Load SVGA files from assets and network URLs.
βοΈ Intelligent caching system for faster loading and reduced network usage.
βοΈ Supports custom dynamic elements (text, images, animations).
βοΈ Optimized playback performance with animation controllers.
βοΈ Integrated audio playback within SVGA animations.
βοΈ Works on Android & iOS (Web & Desktop support coming soon).
βοΈ Easy loop, stop, and seek functions.
π Installation
Add flutter_svga to your pubspec.yaml:
dependencies:
flutter_svga: ^0.0.12
Then, install dependencies:
flutter pub get
π¬ Basic Usage
β Playing an SVGA Animation from Assets
import 'package:flutter/material.dart';
import 'package:flutter_svga/flutter_svga.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Flutter SVGA Example")),
body: Center(
child: SVGAEasyPlayer(
assetsName: "assets/sample_with_audio.svga",
fit: BoxFit.contain,
),
),
),
);
}
}
π Playing SVGA from a Network URL
SVGAEasyPlayer(
resUrl: "https://example.com/sample.svga",
fit: BoxFit.cover,
);
π Advanced Usage: Using SVGAAnimationController
β Controlling Animation Playback
class MySVGAWidget extends StatefulWidget {
@override
_MySVGAWidgetState createState() => _MySVGAWidgetState();
}
class _MySVGAWidgetState extends State<MySVGAWidget>
with SingleTickerProviderStateMixin {
late SVGAAnimationController _controller;
@override
void initState() {
super.initState();
_controller = SVGAAnimationController(vsync: this);
SVGAParser.shared.decodeFromAssets("assets/sample.svga").then((video) {
_controller.videoItem = video;
_controller.repeat();
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SVGAImage(_controller);
}
}
π¨ Customization & Dynamic Elements
β Adding Dynamic Text
controller.videoItem!.dynamicItem.setText(
TextPainter(
text: TextSpan(
text: "Hello SVGA!",
style: TextStyle(color: Colors.red, fontSize: 18),
),
textDirection: TextDirection.ltr,
),
"text_layer",
);
β Replacing an Image Dynamically
controller.videoItem!.dynamicItem.setImageWithUrl(
"https://example.com/new_image.png",
"image_layer",
);
β Hiding a Layer
controller.videoItem!.dynamicItem.setHidden(true, "layer_to_hide");
ποΈ Caching (New!)
Automatic performance optimization with zero breaking changes:
// Caching works automatically - no code changes needed!
final animation = await SVGAParser.shared.decodeFromURL(
"https://example.com/animation.svga"
);
// Optional: Configure cache settings
SVGACache.shared.setMaxCacheSize(50 * 1024 * 1024); // 50MB
SVGACache.shared.setMaxAge(const Duration(days: 3)); // 3 days
// Optional: Manage cache
await SVGACache.shared.clear(); // Clear all cache
final stats = await SVGACache.shared.getStats(); // Get cache info
π See CACHE.md for complete caching documentation and examples.
π― Playback Controls
controller.forward(); // Play once
controller.repeat(); // Loop playback
controller.stop(); // Stop animation
controller.value = 0; // Reset to first frame
π Common Issues & Solutions
β Black Screen when Loading SVGA
β
Solution: Ensure your svga files are correctly placed inside assets/ and registered in pubspec.yaml.
flutter:
assets:
- assets/sample.svga
β SVGA Not Loading from Network
β Solution: Ensure the SVGA file is accessible via HTTPS. Test the URL in a browser.
SVGAEasyPlayer(
resUrl: "https://example.com/sample.svga",
fit: BoxFit.cover,
);
β Animation Freezes or Doesn't Play
β
Solution: Use setState after loading SVGA to rebuild the widget.
setState(() {
_controller.videoItem = video;
});
π± Supported Platforms
| Platform | Supported | Audio Support |
|---|---|---|
| β Android | βοΈ Yes | βοΈ Yes |
| β iOS | βοΈ Yes | βοΈ Yes |
| β Linux | βοΈ Yes | βοΈ Yes |
| β Web | βοΈ Yes | β No |
| β macOS | βοΈ Yes | βοΈ Yes |
| β Desktop | βοΈ Yes | βοΈ Yes |
π Changelog
See the latest changes in CHANGELOG.md.
π License
This package is licensed under the MIT License. See LICENSE for details.
π€ Contributing
- If you find a bug, report it here.
- Pull requests are welcome! See
CONTRIBUTING.mdfor guidelines.
π¨βπ» Authors & Contributors
π Core Author
- 5alafawyyy β Lead Developer, Maintainer, and Flutter Integration Engineer.
π€ Contributors
Special thanks to the amazing contributors who improved flutter_svga:
| Contributor | Contribution | GitHub |
|---|---|---|
| wonderkidshihab | Fixed repeated music playback bug (#3) | π§© |
| Sansuihe | Identified and proposed MD5-based fix for audio cache collision (#6) | π΅ |
| tungpham6195 | Reported protobuf dependency compatibility issue (#7) | π¦ |
Want to contribute? Read CONTRIBUTING.md and submit your PR β weβd love your help!
π Enjoy using SVGA animations in your Flutter app! π