Want to display an animated picture in Flutter? Use an Animation
For example, the following code displays the frames of an animated GIF that was created by Guillaume Kurkdjian.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import 'package:flutter/material.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( theme: new ThemeData.dark(), home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override State createState() => new MyHomePageState(); } class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { AnimationController _controller; Animation<int> _animation; @override void initState() { _controller = new AnimationController(vsync: this, duration: const Duration(seconds: 5)) ..repeat(); _animation = new IntTween(begin: 0, end: 116).animate(_controller); } Widget build(BuildContext context) { return new Scaffold( body: new Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ new AnimatedBuilder( animation: _animation, builder: (BuildContext context, Widget child) { String frame = _animation.value.toString().padLeft(3, '0'); return new Image.asset( 'assets/frame_${frame}_delay-0.04s.png', gaplessPlayback: true, ); }, ), new Text('Image: Guillaume Kurkdjian', style: new TextStyle(fontStyle: FontStyle.italic)), ], ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.