如何在Flutter中将Curves类动画添加到AnimationController?

时间:2019-03-26 18:32:12

标签: dart flutter flutter-animation

我试图将曲线类动画'Curves.bounceOut'添加到使用AnimationController的代码中。

这是我的代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  var squareScale = 1.0;
  static AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(
        vsync: this,
        lowerBound: 0.5,
        upperBound: 1.0,
        duration: Duration(milliseconds: 300));

    _controller.addListener(() {
      setState(() {
        squareScale = _controller.value;
      });
    });

    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Bounce Example"),
      ),
      body: Stack(
        children: <Widget>[
          Container(
            width: 150.0,
            height: 150.0,
            color: Colors.yellowAccent,
          ),
          Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  GestureDetector(
                    onTap: () {
                      _controller.forward(from: 0.0);
                    },
                    child: Transform.scale(
                      scale: squareScale,
                      child: Container(
                        width: 150.0,
                        height: 150.0,
                        color: Colors.green,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}

当前,绿色容器的动画范围是0.5到1.0,但是不会反弹。如何添加“ Curves.bounceOut”动画,以便在轻按时容器反弹?

在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您将要同时使用动画控制器和补间,这将允许您添加Curves.bounceOut

在代码中的某处添加:

AnimationController _controller;
var scaleAnimation;

在您的initState()方法中:

_controller = new AnimationController(
 duration: new Duration(milliseconds: 300),
 vsync: this
)
..addListener(() {
  setState(() {});
});
scaleAnimation = new Tween(
  begin: 0.5,
  end: 1.0,
).animate(new CurvedAnimation(
  parent: _controller,
  curve: Curves.bounceOut
));

请注意,..addListener()是一段不错的飞镖代码,可让您轻松添加侦听器。补间基本上是告诉您的动画,它需要在begin所给定的时间范围内从end的值到AnimationController的值。要在代码中使用此值,您现在可以设置动画结果的比例:

child: Transform.scale(
  scale: scaleAnimation.value,
  child: Container(
    width: 150.0,
    height: 150.0,
    color: Colors.green,
  ),
),

调用动画后,它将自动更新容器的比例。

编辑:

将小部件更改为此:

child: isChanging ? Transform.scale(
  scale: scaleAnimation.value,
  child: Container(
    width: 150.0,
    height: 150.0,
    color: Colors.green,
  ),
) : Transform.scale(
  scale: 1.0,
  child: Container(
    width: 150.0,
    height: 150.0,
    color: Colors.green,
  ),
),

分别将beginend参数分别设置为0.51.0,但在转发控制器之前,请使用onTap()方法:

onTap: () {
  setState(() {
    isChanging = true
  });
  _controller.forward(from: 0.0);
}

在代码的任何位置添加行bool isChanging。最后,您需要在动画结束时重置显示的小部件,因此将scaleAnimation更改为:

child: Transform.scale(
  scale: scaleAnimation.value,
  child: Container(
    width: 150.0,
    height: 150.0,
    color: Colors.green,
  ),
)..addStatusListener((AnimationStatus status) {
  if (status == AnimationStatus.completed) { //the animation is finished
    _controller.reset();
    setState(() {
      isChanging = false;
    }
  }
});

答案 1 :(得分:0)

更简洁的方法是使用 controller.drive()CurveTween

anim = animController.drive(CurveTween(curve: Curves.easeOut));

然后在小部件树中使用 anim.value

如果您愿意,也可以内联执行此操作:

Opacity(opacity: controller.drive(CurveTween(curve: Curves.easeOut)).value)