Flutter tween 动画 trans Transform Interval

代码段

import 'dart:math' as math;

import 'package:flutter/material.dart';

class BrickPage  extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _BrickPageState();
  }
}

class _BrickPageState extends State<BrickPage> with TickerProviderStateMixin{

  AnimationController animationController;
  Tween tween;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    animationController = new AnimationController(
        vsync: this,
        duration: Duration(seconds: 5));

    tween = new Tween(begin: 0,end: 5);

    animationController.repeat();

  }
  //Interval begin 代表延迟多长时间开始 end 代表超过多少直接到达动画的100%
  Animation get animOne => tween.animate(
    CurvedAnimation(
        parent: animationController,
        curve: Interval(0.0, 0.125,curve: Curves.linear))
  );

  //Brick 1
  Animation get animTwo => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.125,
        0.25,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );
  //Brick 2
  Animation get animThree => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.25,
        0.375,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );
  //Brick 3
  Animation get animFour => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.375,
        0.5,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );

  //Brick 4
  Animation get animFive => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.5,
        0.625,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );
  //Brick 4
  Animation get animSix => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.625,
        0.750,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );

  //Brick 3
  Animation get animSeven => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.750,
        0.875,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );
  //Brick 2
  Animation get animEight => tween.animate(
    CurvedAnimation(
      curve: Interval(
        0.875,
        1.0,
        curve: Curves.linear,
      ),
      parent: animationController,
    ),
  );

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



  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: new AppBar(
        title: new Text("tween animation"),
      ),
      body: Center(
        child: Row(
          children: <Widget>[
            AnimatedBrick(
              animations: [animOne, animTwo],
              controller: animationController,
              marginLeft: 0.0,
              alignment: Alignment.centerLeft,
              isClockWise: true,
            ),
            AnimatedBrick(
              animations: [animThree, animEight],
              controller: animationController,
              marginLeft: 0.0,
              isClockWise: false,
            ),
            AnimatedBrick(
              animations: [animFour, animSeven],
              controller: animationController,
              marginLeft: 30.0,
              isClockWise: true,
            ),
            AnimatedBrick(
              animations: [animFive, animSix],
              controller: animationController,
              marginLeft: 30.0,
              isClockWise: false,
            ),
          ],
        ),
      ),
    );
  }
}
class AnimatedBrick  extends AnimatedWidget{
  final AnimationController controller;
  final List<Animation> animations;
  final double marginLeft;
  final bool isClockWise;
  final Alignment alignment;

  AnimatedBrick({this.controller,this.animations,this.marginLeft,this.isClockWise,this.alignment}):super(listenable:controller);

  Matrix4 clockWise(animation) =>
      Matrix4.rotationZ(animation.value * math.pi * 2.0 * 0.5);
  Matrix4 antiClockWise(animation) =>
      Matrix4.rotationZ(-(animation.value * math.pi * 2.0 * 0.5));

  @override
  Widget build(BuildContext context) {
    var firstTransformation, secondTransformation;

    if (isClockWise) {
      firstTransformation = clockWise(animations[0]);
      secondTransformation = clockWise(animations[1]);
    } else {
      firstTransformation = antiClockWise(animations[0]);
      secondTransformation = antiClockWise(animations[1]);
    }

    /*Transform 使用来做旋转 平移 动画的 这里做的是旋转动画*/
    return Transform(
      transform: firstTransformation,
      alignment: alignment,
      child: Transform(
        transform: secondTransformation,
        alignment: alignment,
        child: Brick(
          marginLeft: marginLeft,
        ),
      ),
    );;
  }
}

class Brick extends StatelessWidget {
  final double marginLeft;

  Brick({this.marginLeft = 15.0});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: marginLeft),
      height: 10.0,
      width: 40.0,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15.0),
        color: Colors.green,
      ),
    );
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 寶貝,兒童節快樂!哈哈哈~ 今天媽媽說醫院的事情有了很大的進展,合作方的院長已經開始親自介入,日程大大地加速了!之...
    NCNeverland阅读 825评论 0 0
  • 当我唱起这一首歌 为你点亮烛光 照亮我们心每个地方 每次相聚都那么的快乐和难忘 回忆不断心中轻唱 遇见了你是我生命...
    起新阅读 1,020评论 0 0