📜  心形容器颤动 - Dart (1)

📅  最后修改于: 2023-12-03 15:39:35.288000             🧑  作者: Mango

以'心形容器颤动 - Dart'作主题

在Dart中,可以使用动画来创造一个心形容器颤动的效果。这个效果可以用在各种场景中,例如特别的情人节动画或者经典的表情包。

创建心形容器

首先,我们需要创建一个心形容器。可以使用Flutter中的CustomPaint和CustomPainter来实现这个目标。下面是用于创建心形容器的代码片段:

class HeartShape extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..style = PaintingStyle.fill
      ..color = Colors.red;
    final path = Path();

    path.moveTo(size.width / 2, size.height / 5);
    path.arcToPoint(
      Offset(size.width * 0.85, size.height / 2),
      radius: Radius.circular(size.width / 5),
      clockwise: false,
    );
    path.arcToPoint(
      Offset(size.width / 2, size.height * 0.85),
      radius: Radius.circular(size.width / 5),
      clockwise: false,
    );
    path.arcToPoint(
      Offset(size.width * 0.15, size.height / 2),
      radius: Radius.circular(size.width / 5),
      clockwise: false,
    );
    path.arcToPoint(
      Offset(size.width / 2, size.height / 5),
      radius: Radius.circular(size.width / 5),
      clockwise: false,
    );

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return oldDelegate != this;
  }
}

class HeartShapeWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: HeartShape(),
    );
  }
}
添加动画

接下来,我们需要为心形容器添加一个颤动的动画。这里我们可以使用Flutter中的Animation和AnimationController,它们可以协作地实现一个平滑的动画效果。

class HeartBeatAnimation extends StatefulWidget {
  final Widget child;

  const HeartBeatAnimation({required this.child});

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

class _HeartBeatAnimationState extends State<HeartBeatAnimation>
    with SingleTickerProviderStateMixin {
  late final AnimationController _controller = AnimationController(
    duration: const Duration(milliseconds: 500),
    reverseDuration: const Duration(milliseconds: 200),
    vsync: this,
  )..addListener(() => setState(() {}));

  late final Animation<double> _animation = CurvedAnimation(
    parent: _controller,
    curve: Curves.elasticOut,
    reverseCurve: Curves.easeOut,
  );

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

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) => _controller.forward(),
      onTapCancel: () => _controller.reverse(),
      onTap: () => _controller.reverse(),
      child: Transform.scale(
        scale: 1 + _animation.value * 0.1,
        child: widget.child,
      ),
    );
  }
}
最终效果

将以上两段代码结合起来,就可以得到一个心形容器颤动的动画效果。

class HeartShapeWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return HeartBeatAnimation(
      child: CustomPaint(
        painter: HeartShape(),
      ),
    );
  }
}

效果演示:

heartbeat-animation.gif

总结

通过Flutter中的CustomPaint和CustomPainter以及Animation和AnimationController,我们可以轻松地实现一个心形容器颤动的动画效果。这个效果可以用在各种场景中,例如特别的情人节动画或者表情包。