Попробуйте что-то вроде этого:
class InfiniteAnimation extends StatefulWidget {
final Widget child;
final int durationInSeconds;
InfiniteAnimation({@required this.child, this.durationInSeconds = 2,});
@override
_InfiniteAnimationState createState() => _InfiniteAnimationState();
}
class _InfiniteAnimationState extends State<InfiniteAnimation>
with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(seconds: widget.durationInSeconds),
);
animation = Tween<double>(
begin: 0,
end: 12.5664, // 2Radians (360 degrees)
).animate(animationController);
animationController.forward();
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
animationController.repeat();
}
});
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animationController,
builder: (context, child) => Transform.rotate(
angle: animation.value,
child: widget.child,
),
);
}
}
Вам необходимо создать StatefulWidget
, который смешивает (with
ключевое слово) с SingleTickerProviderStateMixin
, обеспечивает AnimationController
, запускается анимация, а затем, когда анимация завершится, повторите.
AnimationBuilder
- это лучший способ сообщить виджету об обновлении каждого кадра без необходимости прослушивания animationController
и явного вызова setState
.
Вы можете использовать его так:
InfiniteAnimation(
durationInSeconds: 2, // this is the default value
child: Icon(
Icons.expand_more,
size: 50.0,
color: Colors.white,
),
)