animation - Flutter - ClipPath + AnimatedContainer - 路径动画不正确

标签 animation flutter clip

我希望为 AnimatedContainerClipPath 在 y 轴上移动设置动画。

这是我当前的代码:

class Clip extends StatelessWidget {

  final double height;

  Clip(this.height);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ClipPath(
        clipper: RoundedClipper(height),
        child: AnimatedContainer(
          duration: Duration(seconds: 5),
          height: height,
          color: Colors.amber,
        ),
      ),
    );
  }
}

class RoundedClipper extends CustomClipper<Path> {
  final double height;

  RoundedClipper(this.height);

  @override
  Path getClip(Size size) {
    var path = Path();
    path.lineTo(0.0, height - 200);
    path.quadraticBezierTo(
      size.width / 2,
      height,
      size.width,
      height - 200,
    );
    path.lineTo(size.width, 0.0);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}

根据我传递给此类的高度,AnimatedContainer 将通过动画在两者之间转换,而裁剪器不会设置动画。

它是这样的:

example

我尝试用 AnimatedContainer 包装剪裁器并在其上设置动画,但没有成功。

我怎样才能使剪切路径与 AnimatedContainer 一起垂直动画?

感谢所有愿意提供帮助的人

最佳答案

动画容器使用它自己的动画,所以,我不知道剪辑路径和动画容器是否可以一起使用相同的动画。但是我通过使用自定义动画尝试了类似的方法来满足您的需求。请看看这是否是您想要的。

我将剪辑类转换为有状态的以使用动画。

    class Clip extends StatefulWidget {
      final double height;

      Clip(this.height);

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

    class _ClipState extends State<Clip> with TickerProviderStateMixin {
      AnimationController _controller;
      Animation<double> animation;
      final double startingHeight  =20.0;

      @override
      void initState() {
        super.initState();
        _controller =
            AnimationController(vsync: this, duration: Duration(seconds: 5));
            animation = Tween<double>(begin: startingHeight, end: widget.height).animate(_controller);
        _controller.forward(from: 0.0);
      }

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

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: AnimatedBuilder(
            builder: (context, anim) {
              return ClipPath(
                clipper: RoundedClipper(animation.value),
                child: Container(
                  height: animation.value,
                  color: Colors.amber,
                ),
              );
            },
            animation: _controller,
          ),
        );
      }
    }

在这里,您可以使用 _controller 控制您的动画。

关于animation - Flutter - ClipPath + AnimatedContainer - 路径动画不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649155/

相关文章:

javascript - jQuery 动画提前结束

Dart 无法读取 readAsStringSync

Flutter BottomNavigationBarItem 只接受 Icon 而没有其他小部件?

dart - 如何在 Flutter 中创建带有圆角的自定义模糊形状

css - 将图像裁剪为大屏幕上的条纹

android - 如何在 PropertyValuesHolder 中为旋转动画设置 X 和 Y 轴

动画时 z-index on::before 的 CSS 问题

animation - 视频转webp软件

Flutter ListView 延迟加载

Cocoa 绘制带有圆角和阴影的图像