【Flutter 实战】动画序列、共享动画、路由动画

灰太狼 2022-11-30 15:46 330阅读 0赞

20200825071144392.png

老孟导读:此篇文章是 Flutter 动画系列文章第四篇,本文介绍动画序列、共享动画、路由动画。

动画序列

Flutter中组合动画使用IntervalInterval继承自Curve,用法如下:

  1. Animation _sizeAnimation = Tween(begin: 100.0, end: 300.0).animate(CurvedAnimation(
  2. parent: _animationController, curve: Interval(0.5, 1.0)));

表示_sizeAnimation动画从0.5(一半)开始到结束,如果动画时长为6秒,_sizeAnimation则从第3秒开始。

Intervalbeginend参数值的范围是0.0到1.0。

下面实现一个先执行颜色变化,在执行大小变化,代码如下:

  1. class AnimationDemo extends StatefulWidget {
  2. @override
  3. State<StatefulWidget> createState() => _AnimationDemo();
  4. }
  5. class _AnimationDemo extends State<AnimationDemo>
  6. with SingleTickerProviderStateMixin {
  7. AnimationController _animationController;
  8. Animation _colorAnimation;
  9. Animation _sizeAnimation;
  10. @override
  11. void initState() {
  12. _animationController =
  13. AnimationController(duration: Duration(seconds: 5), vsync: this)
  14. ..addListener((){setState(() {
  15. });});
  16. _colorAnimation = ColorTween(begin: Colors.red, end: Colors.blue).animate(
  17. CurvedAnimation(
  18. parent: _animationController, curve: Interval(0.0, 0.5)));
  19. _sizeAnimation = Tween(begin: 100.0, end: 300.0).animate(CurvedAnimation(
  20. parent: _animationController, curve: Interval(0.5, 1.0)));
  21. //开始动画
  22. _animationController.forward();
  23. super.initState();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return Center(
  28. child: Column(
  29. mainAxisSize: MainAxisSize.min,
  30. children: <Widget>[
  31. Container(
  32. height: _sizeAnimation.value,
  33. width: _sizeAnimation.value,
  34. color: _colorAnimation.value),
  35. ],
  36. ),
  37. );
  38. }
  39. @override
  40. void dispose() {
  41. _animationController.dispose();
  42. super.dispose();
  43. }
  44. }

效果如下:

20200825071144684.png

我们也可以设置同时动画,只需将2个Interval的值都改为Interval(0.0, 1.0)

想象下面的场景,一个红色的盒子,动画时长为6秒,前40%的时间大小从100->200,然后保持200不变20%的时间,最后40%的时间大小从200->300,这种效果通过TweenSequence实现,代码如下:

  1. _animation = TweenSequence([
  2. TweenSequenceItem(
  3. tween: Tween(begin: 100.0, end: 200.0)
  4. .chain(CurveTween(curve: Curves.easeIn)),
  5. weight: 40),
  6. TweenSequenceItem(tween: ConstantTween<double>(200.0), weight: 20),
  7. TweenSequenceItem(tween: Tween(begin: 200.0, end: 300.0), weight: 40),
  8. ]).animate(_animationController);

weight表示每一个Tween的权重。

最终效果如下:

2020082507114540.png

共享动画

Hero是我们常用的过渡动画,当用户点击一张图片,切换到另一个页面时,这个页面也有此图,那么使用Hero组件就在合适不过了,先看下Hero的效果图:

20200825071145910.png

上面效果实现的列表页面代码如下:

  1. class HeroDemo extends StatefulWidget {
  2. @override
  3. State<StatefulWidget> createState() => _HeroDemo();
  4. }
  5. class _HeroDemo extends State<HeroDemo> {
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. body: GridView(
  10. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  11. crossAxisCount: 3, crossAxisSpacing: 5, mainAxisSpacing: 3),
  12. children: List.generate(10, (index) {
  13. if (index == 6) {
  14. return InkWell(
  15. onTap: () {
  16. Navigator.push(
  17. context,
  18. new MaterialPageRoute(
  19. builder: (context) => new _Hero1Demo()));
  20. },
  21. child: Hero(
  22. tag: 'hero',
  23. child: Container(
  24. child: Image.asset(
  25. 'images/bird.png',
  26. fit: BoxFit.fitWidth,
  27. ),
  28. ),
  29. ),
  30. );
  31. }
  32. return Container(
  33. color: Colors.red,
  34. );
  35. }),
  36. ),
  37. );
  38. }
  39. }

第二个页面代码如下:

  1. class _Hero1Demo extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(),
  6. body: Container(
  7. alignment: Alignment.topCenter,
  8. child: Hero(
  9. tag: 'hero',
  10. child: Container(
  11. child: Image.asset(
  12. 'images/bird.png',
  13. ),
  14. ),
  15. )),
  16. );
  17. }
  18. }

2个页面都有Hero控件,且tag参数一致。

路由动画

转场 就是从当前页面跳转到另一个页面,跳转页面在 Flutter 中通过 Navigator,跳转到新页面如下:

  1. Navigator.push(context, MaterialPageRoute(builder: (context) {
  2. return _TwoPage();
  3. }));

回退到前一个页面:

  1. Navigator.pop(context);

Flutter 提供了两个转场动画,分别为 MaterialPageRouteCupertinoPageRoute,MaterialPageRoute 根据不同的平台显示不同的效果,Android效果为从下到上,iOS效果为从左到右。CupertinoPageRoute 不分平台,都是从左到右。

使用 MaterialPageRoute 案例如下:

  1. class NavigationAnimation extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(),
  6. body: Center(
  7. child: OutlineButton(
  8. child: Text('跳转'),
  9. onPressed: () {
  10. Navigator.push(context, CupertinoPageRoute(builder: (context) {
  11. return _TwoPage();
  12. }));
  13. },
  14. ),
  15. ),
  16. );
  17. }
  18. }
  19. class _TwoPage extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. appBar: AppBar(),
  24. body: Container(
  25. color: Colors.blue,
  26. ),
  27. );
  28. }
  29. }

iOS效果:

20200825071146194.png

如果要自定义转场动画如何做?

自定义任何组件都是一样的,如果系统有类似的,直接看源代码是如何实现的,然后按照它的模版自定义组件。

回到正题,看 MaterialPageRoute 的继承关系:

20200825071146402.png

PageRoute 的继承关系:

20200825071146514.png

MaterialPageRoute 和 CupertinoPageRoute 都是继承PageRoute,所以重点是 PageRoute,PageRoute 是一个抽象类,其子类还有一个 PageRouteBuilder,看其名字就知道这是一个可以自定义动画效果,PageRouteBuilder源代码:

20200825071146886.png

pageBuilder 表示跳转的页面。

transitionsBuilder 表示页面的动画效果,默认值代码:

  1. Widget _defaultTransitionsBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  2. return child;
  3. }

通过源代码发现,默认情况下没有动画效果。

自定义转场动画只需修改transitionsBuilder即可:

  1. Navigator.push(
  2. context,
  3. PageRouteBuilder(pageBuilder: (
  4. BuildContext context,
  5. Animation<double> animation,
  6. Animation<double> secondaryAnimation,
  7. ) {
  8. return _TwoPage();
  9. }, transitionsBuilder: (BuildContext context,
  10. Animation<double> animation,
  11. Animation<double> secondaryAnimation,
  12. Widget child) {
  13. return SlideTransition(
  14. position: Tween(begin: Offset(-1, 0), end: Offset(0, 0))
  15. .animate(animation),
  16. child: child,
  17. );
  18. }));

2020082507114747.png

将其封装,方便使用:

  1. class LeftToRightPageRoute extends PageRouteBuilder {
  2. final Widget newPage;
  3. LeftToRightPageRoute(this.newPage)
  4. : super(
  5. pageBuilder: (
  6. BuildContext context,
  7. Animation<double> animation,
  8. Animation<double> secondaryAnimation,
  9. ) =>
  10. newPage,
  11. transitionsBuilder: (
  12. BuildContext context,
  13. Animation<double> animation,
  14. Animation<double> secondaryAnimation,
  15. Widget child,
  16. ) =>
  17. SlideTransition(
  18. position: Tween(begin: Offset(-1, 0), end: Offset(0, 0))
  19. .animate(animation),
  20. child: child,
  21. ),
  22. );
  23. }

使用:

  1. Navigator.push(context, LeftToRightPageRoute(_TwoPage()));

不仅是这些平移动画,前面所学的旋转、缩放等动画直接替换 SlideTransition 即可。

上面的动画只对新的页面进行了动画,如果想实现当前页面被新页面从顶部顶出的效果,实现方式如下:

  1. class CustomPageRoute extends PageRouteBuilder {
  2. final Widget currentPage;
  3. final Widget newPage;
  4. CustomPageRoute(this.currentPage, this.newPage)
  5. : super(
  6. pageBuilder: (
  7. BuildContext context,
  8. Animation<double> animation,
  9. Animation<double> secondaryAnimation,
  10. ) =>
  11. currentPage,
  12. transitionsBuilder: (
  13. BuildContext context,
  14. Animation<double> animation,
  15. Animation<double> secondaryAnimation,
  16. Widget child,
  17. ) =>
  18. Stack(
  19. children: <Widget>[
  20. SlideTransition(
  21. position: new Tween<Offset>(
  22. begin: const Offset(0, 0),
  23. end: const Offset(0, -1),
  24. ).animate(animation),
  25. child: currentPage,
  26. ),
  27. SlideTransition(
  28. position: new Tween<Offset>(
  29. begin: const Offset(0, 1),
  30. end: Offset(0, 0),
  31. ).animate(animation),
  32. child: newPage,
  33. )
  34. ],
  35. ),
  36. );
  37. }

本质就是对两个页面做动画处理,使用:

  1. Navigator.push(context, CustomPageRoute(this, _TwoPage()));

20200825071147184.png

除了自定义路由动画,在 Flutter 1.17 发布大会上,Flutter 团队还发布了新的 Animations 软件包,该软件包提供了实现新的 Material motion 规范的预构建动画。

里面提供了一系列动画,部分效果: 20200825071147950.png 20200825071149431.png

详情:https://juejin.im/post/6847902223909781511

交流

老孟Flutter博客地址(330个控件用法):http://laomengit.com

欢迎加入Flutter交流群(微信:laomengit)、关注公众号【老孟Flutter】:














发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Flutter Animation动画

    一,概述    `  Flutter`动画库的核心类是`Animation`对象,它生成指导动画的值,`Animation`对象指导动画的当前状态(例如,是开始、停止还是

    相关 Flutter中的动画

    今天来学习一下Flutter中的动画。我们在js+css中,已经习惯用css3中的animation,transform,transition这三个加在一起去做。 我们像前端