2.显式动画(手动控制)
RotationTransition
RotationTransition(
turns: _controller,
child: Container(
width: 50,
height: 50,
),
),
_controller = AnimationController(
upperBound: 5.0,
lowerBound: 3.0,
duration: Duration(seconds: 1),
vsync: this,
);
_controller.repeat();
_controller.stop();
_controller.reset();
_controller.forward();
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
bool _loading = false;
AnimationController _controller;
int _count = 0;
_increment() {
setState(() {
this._count++;
});
}
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 1),
vsync: this,
);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: RotationTransition(
turns: _controller,
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
gradient: LinearGradient(colors: [Colors.red, Colors.blue]),
borderRadius: BorderRadius.circular(50),
boxShadow: [BoxShadow(blurRadius: 10)]),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if (_loading) {
_controller.repeat();
} else {
_controller.stop();
}
_loading = !_loading;
},
child: Icon(Icons.add),
),
);
}
}
FadeTransition
FadeTransition(
opacity: _controller,
child: Container(
height: 300,
width: 300,
color: Colors.blue,
),
),
ScaleTransition
ScaleTransition(
scale: _controller,
child: Container(
height: 300,
width: 300,
color: Colors.blue,
),
),
_controller.repeat(reverse:true);
SlideTransition
SlideTransition(
position: _controller
child: Container(
height: 300,
width: 300,
color: Colors.blue,
),
),
Tween控制器串联补间
SlideTransition(
position: Tween(begin: Offset(0, -1), end: Offset(0, 1))
.chain(CurveTween(curve: Curves.elasticInOut))
.animate(_controller),
child: Container(
height: 300,
width: 300,
color: Colors.blue,
),
),
SlideTransition(
position: Tween(begin: Offset(0.0, 0.0), end: Offset(0.1, 0.0))
.chain(CurveTween(curve: _interval))
.animate(_controller),
child: Container(height: 100, width: 300, color: this._color));
}
SlidingBox(this._controller, Colors.blue[100], Interval(0.0, 0.2)),
SlidingBox(this._controller, Colors.blue[300], Interval(0.2, 0.4)),
SlidingBox(this._controller, Colors.blue[500], Interval(0.4, 0.6)),
SlidingBox(this._controller, Colors.blue[700], Interval(0.6, 0.8)),
SlidingBox(this._controller, Colors.blue[900], Interval(0.8, 1.0)),
AnimatedBuilder自定义动画
AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Opacity(
opacity: Tween(begin: 0.5, end: 0.8).evaluate(_controller),
child: Container(
height: Tween(begin: 100.0, end: 200.0).evaluate(_controller),
width: 300,
color: Colors.blue,
child: child,
),
);
},
child: Center(
child: Text(
'Hi',
style: TextStyle(fontSize: 35),
),
),
),
final Animation opacityAnimation =
Tween(begin: 0.5, end: 0.8).animate(_controller);
final Animation heightAnimation =
Tween(begin: 100.0, end: 200.0).animate(_controller);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Opacity(
opacity: opacityAnimation.value,
child: Container(
height: heightAnimation.value,
width: 300,
color: Colors.blue,
child: child,
),
);
},
实例478呼吸法
Animation stopsAnimation1 = Tween(begin: 0.0, end: 1.0)
.chain(CurveTween(
curve: Interval(0.0, 0.2),
))
.animate(_controller);
Animation stopsAnimation3 = Tween(begin: 1.0, end: 0.0)
.chain(CurveTween(
curve: Interval(0.4, 0.95),
))
.animate(_controller);
AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Container(
height: 200,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Colors.blue,
gradient: RadialGradient(
colors: [Colors.blue[600], Colors.blue[100]],
stops: _controller.value <= 0.2
? [stopsAnimation1.value, _controller.value + 0.1]
: [stopsAnimation3.value, _controller.value + 0.1]),
));
},
)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】