首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >曲线动画中的AnimationController重复

曲线动画中的AnimationController重复
EN

Stack Overflow用户
提问于 2020-08-30 08:35:46
回答 1查看 904关注 0票数 0

如何制作一个重复的AnimatedController,但从曲线动画开始。

代码:

AnimationController:

代码语言:javascript
复制
var _animating = false;

  AnimationController _rotationAnimationController;
  Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _rotationAnimationController = AnimationController(
      duration: Duration(milliseconds: 2000),
      vsync: this,
    );
    _animation =
        Tween<double>(begin: 0, end: 4 * pi ).animate(_rotationAnimationController)
      ..addListener(() {
        setState(() {});
      });
  }

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

按钮:

代码语言:javascript
复制
GestureDetector(
                       onTap: () {
                         Duration startStopTime = Duration(seconds: 3);
                         if(_animating) {
                           _rotationAnimationController.animateBack(1, duration: startStopTime, curve: Curves.easeOut);
                           setState(() {});
                         } else {
                           _rotationAnimationController.repeat() //This needs to start with a curve
                         }
                         setState(() {
                           _animating = !_animating;
                         });
                       },
                       child: [...]
                     ),

如果你也能做到,当重复停止的时候,它用一个曲线来做,那也会很棒:)

谢谢你的帮助:D

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-30 09:18:28

我最理解你的问题,你想要一个CurvedAnimation,对吗?这意味着您的动画将重复,但遵循一个特定的曲线。所以在这里我能为你做的最好的:

这样定义您的AnimationController

代码语言:javascript
复制
 Animation<double> _animation;
 AnimationController _animationController;


  @override
  void initState() {
    super.initState();

    _animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 2))
          ..addListener(() {
            setState(() {});
          });

    final Animation curve =
        CurvedAnimation(parent: _animationController, curve: Curves.easeOut);

    _animation = Tween<double>(begin: 0, end: pi * 4).animate(curve);
  }

你的GestureDetector是这样的:

代码语言:javascript
复制
     GestureDetector(
                   onTap: () {
                        if (_animationController.isAnimating) 
  {
   _animationController.animateBack(0,duration: Duration(seconds: 2), curve: Curves.easeIn);
  } 

  else {
      _animationController.repeat();
    }
  },
                       child: [...]
                     ),

编辑

我用了一个TweenAnimationBuilder来达到你想要的效果:

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'dart:math' as math;

class TweenAnimatedBuilderRotate extends StatefulWidget {
  TweenAnimatedBuilderRotate({Key key}) : super(key: key);

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

class TweenAnimatedBuilderRotateState
    extends State<TweenAnimatedBuilderRotate> {
  double _newAngle = 0;
  Curve curveThatChanges = Curves.easeIn;
  bool isAnimating = false;
  int _millsecs = 2000;
  

  void onCompletion() {
    if (isAnimating) {
      _newAngle += 4 * math.pi;
      curveThatChanges = Curves.linear;
      _millsecs = 1000;

      setState(() {});
    } else {
      _newAngle = 0;
      _millsecs = 2000;
    }
  }

  void onContainerTap() {
    if (isAnimating) {
      isAnimating = false;
      _newAngle = _newAngle;
      setState(() {});
    } else {
      curveThatChanges = Curves.easeIn;
      _newAngle += 4 * math.pi;
      isAnimating = true;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return TweenAnimationBuilder(
        tween: Tween<double>(begin: 0, end: _newAngle),
        duration: Duration(milliseconds: _millsecs),
        onEnd: () => onCompletion(),
        curve: curveThatChanges,
        builder: (
          BuildContext ctx,
          double angle,
          Widget child,
        ) {
          _newAngle = angle;
          return Center(
            child: Transform(
              transform: Matrix4.identity()..rotateZ(_newAngle),
              alignment: FractionalOffset.center,
              child: GestureDetector(
                child: Container(
                  color: Colors.blueGrey,
                  width: 200,
                  height: 200,
                ),
                onTap: () => onContainerTap(),
              ),
            ),
          );
        });
  }
}

您可以参考媒体文章来了解TweenAnimationdBuilder是如何工作的。您还可以修改_millsecs变量以加快/降低动画速度。在TweenAnimatedBuilderRotate()body参数中传递Scaffold(...)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63655545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档