【问题标题】:Rotating container indefinitely无限期旋转容器
【发布时间】:2020-05-06 23:01:33
【问题描述】:

我想无限期旋转图像。

此容器是堆栈中的小部件之一,并希望它能够连续不停地旋转。

final AnimationController animation = AnimationController(
  duration: const Duration(milliseconds: 1800),
  vsync: const NonStopVSync(),
)..repeat();

final Tween tween = Tween(begin: 0.0, end: math.pi);

var square = Container(
  width: 100,
  height: 100,
  transform: Matrix4.identity(),
  color: Colors.amber,
);

...

class Foo extends State<Bar> {
    ...


    animation.addListener((){
       square.transform = Matrix4.rotationZ(tween.evaluate(animation));
    });

    Widget build(BuildContext context) {
        return Stack(
           children: [
              ...
              Center(
                 child: square
              )
           ]
        )
    }
}

我得到了这个error: 'transform' can't be used as a setter because it's final. (assignment_to_final at [digital_clock] lib/digital_clock.dart:139)

我将如何做我想做的事?

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    试试这样的:

    class InfiniteAnimation extends StatefulWidget {
      final Widget child;
      final int durationInSeconds;
    
      InfiniteAnimation({@required this.child, this.durationInSeconds = 2,});
    
      @override
      _InfiniteAnimationState createState() => _InfiniteAnimationState();
    }
    
    class _InfiniteAnimationState extends State<InfiniteAnimation>
        with SingleTickerProviderStateMixin {
      AnimationController animationController;
      Animation<double> animation;
    ​
      @override
      void initState() {
        super.initState();
        animationController = AnimationController(
          vsync: this,
          duration: Duration(seconds: widget.durationInSeconds),
        );
        animation = Tween<double>(
          begin: 0,
          end: 12.5664, // 2Radians (360 degrees)
        ).animate(animationController);
    ​
        animationController.forward();
    ​
        animation.addStatusListener((status) {
          if (status == AnimationStatus.completed) {
            animationController.repeat();
          }
        });
      }
    ​
      @override
      Widget build(BuildContext context) {
        return AnimatedBuilder(
          animation: animationController,
          builder: (context, child) => Transform.rotate(
            angle: animation.value,
            child: widget.child,
          ),
        );
      }
    
      @override
      void dispose() {
        animationController?.dispose();
    
        super.dispose();
      }
    }
    

    您基本上需要创建一个StatefulWidget 混合(with 关键字)SingleTickerProviderStateMixin,提供一个AnimationController,启动动画,然后在动画完成时重复。

    AnimationBuilder 是一种更好的方式来告诉小部件在每一帧上更新,而无需监听 animationController 并显式调用 setState

    你可以这样使用它:

    InfiniteAnimation(
      durationInSeconds: 2, // this is the default value
      child: Icon(
        Icons.expand_more,
        size: 50.0,
        color: Colors.white,
      ),
    )
    

    【讨论】:

    • 谢谢,这让我有了更多的了解。我是 Dart 和 Flutter 的新手,正在寻找解决方法。目前我正在尝试在这个类中实现。 https://github.com/flutter/flutter_clock/blob/master/digital_clock/lib/digital_clock.dart 让一些设计元素不断旋转。
    • 没问题!如果您需要更多详细信息,请告诉我。
    • 我在我之前评论的链接中的_DigitalClockState 类中实现了这个类,我得到了StatefulWidget.createState must return a subtype of State&lt;InfiniteAnimation&gt;
    • State&lt;InfiniteAnimation&gt; 更改为 State&lt;DigitalClock&gt;
    • 我的主类_DigitalClockState可以使用我创建的第二类InfiniteAnimation和类DigitalClock吗?比如https://github.com/flutter/flutter_clock/blob/master/digital_clock/lib/digital_clock.dart#L32?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2022-01-05
    • 2016-10-30
    相关资源
    最近更新 更多