【发布时间】:2018-09-11 08:48:36
【问题描述】:
我想通过单击按钮来控制 gif 动画,即,如果我按下“单击我”按钮动画开始并再次按下动画应该停止。我正在参考这个问题How to display an animated picture in Flutter?
我不想将 gif 图像拆分为帧,我必须只使用一个 gif 图像并仅控制它。这是我的代码。
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
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: 3))
..stop();
_animation = new IntTween(begin: 0, end: 7).animate(_controller);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
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(0, '0');
return new Image.asset(
'assets/lips.gif',
gaplessPlayback: true,
);
},
),
new RaisedButton(
child: new Text('click me'),
onPressed: () {
if (_controller.isAnimating) {
_controller.stop();
} else {
_controller.repeat();
}
},
),
],
),
);
}
}
【问题讨论】:
-
这在今天是不可能的,除非您自己将所有帧解码为单独的图像。