美文网首页
Flutter 动画

Flutter 动画

作者: 喜剧收尾_XWX | 来源:发表于2020-08-06 22:55 被阅读0次

字体放大效果-补间动画

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      title: "字体放大动画",
      home: FontAnimation(),
    ));

class FontAnimation extends StatefulWidget {
  _FontAnimation createState() => _FontAnimation();
}

class _FontAnimation extends State<FontAnimation>
    with SingleTickerProviderStateMixin {
  @override
  //补间动画
  Animation<double> tween;

  //动画控制对象
  AnimationController controller;

  /*初始化状态*/
  initState() {
    super.initState();
    /*
    * Duration 动画时间
    * vsync:防止动画离屏之后继续消耗资源
    * */
    controller = AnimationController(
        duration: const Duration(microseconds: 9000000), vsync: this);

    tween = Tween(begin: 0.0, end: 1.0).animate(controller)
      ..addListener(() {
        setState(() {
          print(tween.value);
        });
      });
    controller.forward(); //执行动画
  }

  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('字体放大动画'),
      ),

      //添加手势组件
      body: GestureDetector(
        onTap: () {},
        child: Center(
          child: Text(
            '字体放大特效',
            style: TextStyle(fontSize: 60 * controller.value),
          ),
        ),
      ),

      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          startAnimation();
        },
        backgroundColor: Colors.red,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }

  startAnimation() {
    setState(() {
      controller.forward(from: 0.0);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    //控制器销毁对象
    controller.dispose();

    super.dispose();
  }
}

缓动动画

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

void main() => runApp(MaterialApp(
      title: "缓动动画",
      home: EasingAnimation(),
    ));

class EasingAnimation extends StatefulWidget {
  _EasingAnimation createState() => _EasingAnimation();
}

class _EasingAnimation extends State<EasingAnimation>
    with TickerProviderStateMixin {
  @override
  //补间动画
  Animation _animation;

  //动画控制对象
  AnimationController _controller;

  /*初始化状态*/
  initState() {
    super.initState();
    /*
    * Duration 动画时间
    * vsync:防止动画离屏之后继续消耗资源
    * */
    _controller =
        AnimationController(duration: const Duration(seconds: 10), vsync: this);

    _animation = Tween(begin: 0.0, end: 0.5).animate(

        //非线性动画
        CurvedAnimation(
      parent: _controller,
      //缓动动画,决定了运动的轨迹,快出慢进
      curve: Curves.fastOutSlowIn,
    ))
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          //移除状态监听
          _animation.removeStatusListener((status) {});
          //动画重置
          _controller.reset();
          //再次创建补间对象,从0-到1进行估值

          _animation = Tween(begin: 0.0, end: 0.5).animate(
              CurvedAnimation(parent: _controller, curve: Curves.fastLinearToSlowEaseIn))
            ..addStatusListener((status) {
              if (status == AnimationStatus.completed) {}
            });

          //执行动画
          _controller.forward();
        }
      });

    //执行动画
    _controller.forward();
  }

  Widget build(BuildContext context) {
    //计算当前页面宽度
    final double width = MediaQuery.of(context).size.width;

    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('缓动动画'),
        ),

        //添加手势组件
        body: AnimatedBuilder(
          //动画控制器
          animation: _controller,
          builder: (context, child) {
            return Transform(
              transform:
                  Matrix4.translationValues(_animation.value * width, 0.0, 0.0),
              child: Center(
                child: Container(
                  width: 100,
                  height: 100,
                  color: Colors.greenAccent,
                ),
              ),
            );
          },
        ));
  }

  @override
  void dispose() {
    // TODO: implement dispose
    //控制器销毁对象
    _controller.dispose();

    super.dispose();
  }
}

3.遮罩动画

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

void main() => runApp(MaterialApp(
      title: "遮罩动画",
      home: MaskAnimation(),
    ));

//遮罩动画
class MaskAnimation extends StatefulWidget {
  @override
  _MaskAnimation createState() => _MaskAnimation();
}

class _MaskAnimation extends State<MaskAnimation>
    with TickerProviderStateMixin {
  AnimationController _controller;

  //容器宽高值补间对象
  Animation<double> transitionTween;

  //容器边框弧度补间对象
  Animation<BorderRadius> borderRadius;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _controller =
        AnimationController(duration: Duration(seconds: 10), vsync: this)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              print("动画完成喽");
            }
          });

    //补间对象
    transitionTween = Tween<double>(begin: 50.0, end: 200.0)
        .animate(CurvedAnimation(parent: _controller, curve: Curves.ease));

    borderRadius = BorderRadiusTween(
      begin: BorderRadius.circular(75.0),
      end: BorderRadius.circular(0),
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: Curves.ease,
    ));

    //执行动画
//    _controller.forward();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return AnimatedBuilder(
      animation: _controller,
      builder: (BuildContext context, Widget child) {
        return Scaffold(
          appBar: AppBar(
            title: Text('遮罩动画'),
          ),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              _controller.forward();
            },
          ),
          body: Center(
            //使用组件
            child: Stack(
              children: <Widget>[
                Center(
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.lightGreen,
                  ),
                ),
                Center(
                  child: Container(
//                alignment: Alignment.bottomCenter,
                    width: transitionTween.value,
                    height: transitionTween.value,
                    decoration: BoxDecoration(
                      color: Colors.black26,
                      borderRadius: borderRadius.value,
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}

4.数字动画

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

void main() => runApp(MaterialApp(
      title: "数字动画",
      home: ValueAnimation(),
    ));

//遮罩动画
class ValueAnimation extends StatefulWidget {
  @override
  _ValueAnimation createState() => _ValueAnimation();
}

class _ValueAnimation extends State<ValueAnimation>
    with TickerProviderStateMixin {
  AnimationController _controller;

  //容器宽高值补间对象
  Animation _animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _controller =
        AnimationController(duration: Duration(seconds: 10), vsync: this)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              print("动画完成喽");
            }
          });

    //补间动画-IntTween
    final Animation curve =
        CurvedAnimation(parent: _controller, curve: Curves.easeInOut);

    _animation = IntTween(begin: 0, end: 30).animate(curve)
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          //当动画完成后,执行翻转动画,如0-10,10-1

          _controller.reverse();
        }
      });

    //执行动画
//    _controller.forward();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return AnimatedBuilder(
      animation: _controller,
      builder: (BuildContext context, Widget child) {
        return Scaffold(
          appBar: AppBar(
            title: Text('数字变化'),
          ),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              _controller.forward();
            },
          ),
          body: Center(
            child: Text(
              _animation.value.toString(),
              style: TextStyle(fontSize: 30),
            ),
          ),
        );
      },
    );
  }
}

5.图表动画

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

void main() => runApp(MaterialApp(
      title: "图标动画",
      home: ChartsAnimation(),
    ));

class ChartsAnimation extends StatefulWidget {
  @override
  _ChartsAnimation createState() => _ChartsAnimation();
}

class _ChartsAnimation extends State<ChartsAnimation> {
  //定义图标高度
  var height = 100.0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('图标动画'),
      ),
      body: Center(
        child: Container(
          height: 400,
          alignment: AlignmentDirectional.bottomStart,
          child: InkWell(
            onTap: () {
              setState(() {
                height = 320;
              });
            },
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                AnimatedContainer(
                  //动画时长
                  duration: Duration(seconds: 1),
                  width: 40,
                  height: height - 40,
                  color: Colors.red,
                ),
                AnimatedContainer(
                  //动画时长
                  margin: EdgeInsets.only(left: 10),
                  duration: Duration(seconds: 1),
                  width: 40,
                  height: height - 10,
                  color: Colors.orange,
                ),
                AnimatedContainer(
                  //动画时长
                  margin: EdgeInsets.only(left: 10),
                  duration: Duration(seconds: 1),
                  width: 40,
                  height: height - 50,
                  color: Colors.greenAccent,
                ),
                AnimatedContainer(
                  //动画时长
                  margin: EdgeInsets.only(left: 10),
                  duration: Duration(seconds: 1),
                  width: 40,
                  height: height - 20,
                  color: Colors.yellow,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

相关文章

  • Flutter 动画效果

    在动画方面 Flutter 提供了 Animation 类提供使用。 动画 Flutter 中创建动画,请创建名为...

  • Flutter动画animation

    参考 动画 flutter中动画抽象 划重点 Flutter 中的动画系统基于Animation对象,widget...

  • Flutter中的动画

    Flutter中的动画 Flutter中的动画 https://flutterchina.club/tutoria...

  • Flutter从入门到写出完整App Day12

    20.3.27 Flutter实现动画 Flutter有自己的渲染闭环 一. 动画API认识 1. Animati...

  • Flutter轮播图

    前端开发当中最有意思的就是实现动画特效,Flutter提供的各种动画组件可以方便实现各种动画效果。Flutter中...

  • 深入理解Flutter动画原理

    基于Flutter 1.5,从源码视角来深入剖析flutter动画原理,相关源码目录见文末附录 一、概述 动画效果...

  • Flutter动画: Animation动画基础(一)

    我的博客 要使用Flutter中动画, 首先要熟悉Flutter的动画基础概念和相关类。 Animation:Fl...

  • Flutter - 创建拍手动画

    在本文中,我们将从头开始探索Flutter动画。我们将通过在Flutter中创建拍手动画的模型来学习关于动画的一些...

  • Flutter开发之帧动画

    最近写了一些flutter的小动画,在这里也写几个篇章介绍下flutter的动画的实现,先实现个简单的帧动画,举例...

  • Flutter:一个简单的动画实现原理浅析

    Flutter 动画 本文主要介绍 Flutter 动画相关的内容,对相关的知识点进行了梳理,并从实际例子出发,进...

网友评论

      本文标题:Flutter 动画

      本文链接:https://www.haomeiwen.com/subject/wxzzrktx.html