Animated-Text-Kit icon indicating copy to clipboard operation
Animated-Text-Kit copied to clipboard

Shaking text

Open avishail opened this issue 1 year ago • 1 comments

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

avishail avatar Sep 20 '23 06:09 avishail

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;
    });
  }
}

avishail avatar Sep 20 '23 08:09 avishail