aagarwal1012 / Animated-Text-Kit

🔔 A flutter package to create cool and beautiful text animations. [Flutter Favorite Package]
https://animated-text-kit.web.app
MIT License
1.66k stars 304 forks source link

Shaking text #325

Open avishail opened 1 year ago

avishail commented 1 year ago

It would be nice to have a shaking text effect (for friendly error message for example)

avishail commented 1 year ago

ok, I've managed to get it but I don't have the time to make a proper PR. Feel free to take it.

class ShakingAnimatedText extends AnimatedText {
  int numberOfShakes;
  int shakeOffset;
  double _animationValue = 0;
  ShakingAnimatedText(
    String text, {
    TextAlign textAlign = TextAlign.start,
    TextStyle? textStyle,
    Duration duration = const Duration(milliseconds: 500),
    this.numberOfShakes = 2,
    this.shakeOffset = 7,
  }) : super(
          text: text,
          textAlign: textAlign,
          textStyle: textStyle,
          duration: duration,
        );

  @override
  Widget animatedBuilder(BuildContext context, Widget? child) {
    final sineValue = sin(numberOfShakes * 2 * pi * _animationValue);
    return Transform.translate(
      offset: Offset(sineValue * shakeOffset, 0),
      child: child,
    );
  }

  @override
  void initAnimation(AnimationController controller) {
    controller.addListener(() {
      _animationValue = controller.value;
    });
  }
}