Flutter Animation实现缩放和滑动动画效果

作者:GalenWu 时间:2021-09-02 10:33:12 

本文实例为大家分享了Flutter Animation实现缩放和滑动动画的具体代码,供大家参考,具体内容如下

Animation对象是Flutter动画库中的一个核心类,它生成指导动画的值。
Animation对象知道动画的当前状态(例如,它是开始、停止还是向前或向后移动),但它不知道屏幕上显示的内容。
AnimationController管理Animation。
CurvedAnimation 将过程抽象为一个非线性曲线.
Tween在正在执行动画的对象所使用的数据范围之间生成值。例如,Tween可能会生成从红到蓝之间的色值,或者从0到255。
使用Listeners和StatusListeners监听动画状态改变。

import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(new LogoApp());
}
class LogoApp extends StatefulWidget {
  _LogoAppState createState() => new _LogoAppState();
}

class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  initState() {
    super.initState();
    controller = new AnimationController(
        duration: const Duration(milliseconds: 10000), vsync: this);
    animation = new Tween(begin: 0.0, end: 300.0).animate(controller);
    controller.forward();
  }
  
  Widget build(BuildContext context) {
    return new AnimatedLogo(animation: animation);
  }

  dispose() {
    controller.dispose();
    super.dispose();
  }
}

class AnimatedLogo extends AnimatedWidget {
  AnimatedLogo({Key key, Animation<double> animation})
      : super(key: key, listenable: animation);

  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return new Center(
      child: new Container(
        margin: new EdgeInsets.symmetric(vertical: 10.0),
        height: animation.value,
        width: animation.value,
        child: new FlutterLogo(),
      ),
    );
  }
}

缩放功能

class ScaleAnimatedContent extends StatefulWidget {
  final Widget child;
  final bool show;

  const ScaleAnimatedContent({Key key, this.child, this.show = false})
      : super(key: key);

  @override
  ScaleAnimatedContentState createState() => ScaleAnimatedContentState();
}

class ScaleAnimatedContentState extends State<ScaleAnimatedContent>
    with TickerProviderStateMixin {
  AnimationController animationController;
  Animation<double> animation;

  @override
  void initState() {
    animationController = new AnimationController(
      vsync: this,
      duration: new Duration(milliseconds: 600),
    );

    // animationSlideUp = new Tween(begin: 0.0,end: 1.0).animate(animationController);
    animation = Tween(begin: 0.0, end: 1.0).animate(animationController);

    if (widget.show) {
      animationController.forward();
    }

    super.initState();
  }

  @override
  void didUpdateWidget(ScaleAnimatedContent oldWidget) {
    if (widget != oldWidget) {
      if (widget.show && !oldWidget.show) {
        animationController.forward(from: 0.0);
      } else if (!widget.show) {
        animationController.reverse();
      }
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return ScaleTransition(
      scale: animation,
      child: widget.child,
    );
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }
}

滑动效果

class SlideAnimatedContent extends StatefulWidget {
  final Widget child;
  final bool show;

  const SlideAnimatedContent({Key key, this.child, this.show = false})
      : super(key: key);

  @override
  SlideAnimatedContentState createState() => SlideAnimatedContentState();
}

class SlideAnimatedContentState extends State<SlideAnimatedContent>
    with TickerProviderStateMixin {
  AnimationController animationController;
  Animation<Offset> animationSlideUp;

  @override
  void initState() {
    animationController = new AnimationController(
      vsync: this,
      duration: new Duration(milliseconds: 600),
    );

    animationSlideUp = new Tween(
      begin: Offset(0.0, 5.0),
      end: Offset(0.0, 0.0),
    ).animate(CurvedAnimation(parent: animationController, curve: Curves.ease));

    if (widget.show) {
      animationController.forward();
    }

    super.initState();
  }

  @override
  void didUpdateWidget(SlideAnimatedContent oldWidget) {
    if (widget != oldWidget) {
      if (widget.show && !oldWidget.show) {
        animationController.forward(from: 0.0);
      } else if (!widget.show) {
        animationController.reverse();
      }
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: animationSlideUp,
      child: FadeTransition(
        opacity: animationController,
        child: widget.child,
      ),
    );
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }
}

来源:https://blog.csdn.net/m0_38013946/article/details/121764930

标签:Flutter,缩放,滑动
0
投稿

猜你喜欢

  • 从零实现一个简单的Spring Bean容器的代码案例

    2022-07-24 11:42:16
  • SpringBoot集成gRPC微服务工程搭建实践的方法

    2022-03-11 22:10:39
  • Java实现Http工具类的封装操作示例

    2021-08-14 10:27:57
  • c#创建windows服务入门教程实例

    2023-02-24 11:31:31
  • 带你走进Maven的大门-最全Maven配置及集成idea工具总结

    2022-12-06 08:41:40
  • 以Java代码的方式总结几个典型的内存溢出案例

    2023-06-11 04:00:56
  • Netty分布式NioEventLoop任务队列执行源码分析

    2022-10-08 04:07:19
  • C# 图片与Base64码的相互转化问题(代码详解)

    2021-11-30 22:56:16
  • C# 时间戳转换实例

    2022-12-07 12:17:57
  • @Autowired注解在抽象类中失效的原因及解决

    2021-07-15 06:12:04
  • C#实现简单聊天程序的方法

    2022-01-02 22:31:20
  • spring-redis-session 自定义 key 和过期时间

    2022-03-29 14:34:37
  • Java多线程wait()和notify()方法详细图解

    2021-09-19 20:27:32
  • JVM代码缓存区CodeCache原理及用法解析

    2023-08-09 06:02:29
  • java基础的详细了解第六天

    2021-11-05 16:18:49
  • 一文带你搞懂Java中的泛型和通配符

    2023-12-10 16:15:07
  • C#使用三层架构开发Winform的详细案例

    2023-08-08 03:22:24
  • Lombok为啥这么牛逼?SpringBoot和IDEA官方都要支持它

    2021-10-18 23:04:50
  • iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

    2023-07-08 16:52:22
  • IntelliJ IDEA运行SpringBoot项目的详细步骤

    2022-04-29 00:59:08
  • asp之家 软件编程 m.aspxhome.com