Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shaking text #325

Open
avishail opened this issue Sep 20, 2023 · 1 comment
Open

Shaking text #325

avishail opened this issue Sep 20, 2023 · 1 comment

Comments

@avishail
Copy link

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

@avishail
Copy link
Author

avishail commented Sep 20, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant