配置vsync,需要实现一下with SingleTickerProviderStateMixin
class _MyHomePageState extends State < MyHomePage> with SingleTickerProviderStateMixin{
late AnimationController _controller;
@override
void initState ( ) {
super . initState ( ) ;
_controller = AnimationController (
duration: const Duration ( milliseconds: 500 ) ,
vsync: this ,
lowerBound: 0 ,
upperBound: 1 ,
) ;
}
@override
Widget build ( BuildContext context ) {
return Scaffold (
appBar: AppBar (
title: const Text ( '标题' ) ,
) ,
body: Center (
child: Column (
children: [
RotationTransition (
turns: _controller,
child: Container (
alignment: Alignment. center,
width: 100 ,
height: 100 ,
color: Colors. red,
) ,
) ,
ElevatedButton ( onPressed: ( ) {
_controller. repeat ( ) ;
} , child: const Text ( '旋转重复' ) ) ,
ElevatedButton ( onPressed: ( ) {
_controller. stop ( ) ;
} , child: const Text ( '旋转停止' ) ) ,
ElevatedButton ( onPressed: ( ) {
_controller. forward ( ) ;
} , child: const Text ( '正转1次' ) ) ,
ElevatedButton ( onPressed: ( ) {
_controller. reverse ( ) ;
} , child: const Text ( '倒转一次' ) ) ,
ElevatedButton ( onPressed: ( ) {
_controller. reset ( ) ;
} , child: const Text ( '重置' ) ) ,
] ,
) ,
) ,
) ;
}
}