Created
June 5, 2020 10:01
-
-
Save bobagold/3cf690d17243a53bdee9fba5f14f15d3 to your computer and use it in GitHub Desktop.
Shake any widget in flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
@immutable | |
class ShakeWidget extends StatelessWidget { | |
final Duration duration; | |
final double deltaX; | |
final Widget child; | |
final Curve curve; | |
const ShakeWidget({ | |
Key key, | |
this.duration = const Duration(milliseconds: 500), | |
this.deltaX = 20, | |
this.curve = Curves.bounceOut, | |
this.child, | |
}) : super(key: key); | |
/// convert 0-1 to 0-1-0 | |
double shake(double animation) => | |
2 * (0.5 - (0.5 - curve.transform(animation)).abs()); | |
@override | |
Widget build(BuildContext context) { | |
return TweenAnimationBuilder<double>( | |
key: key, | |
tween: Tween(begin: 0.0, end: 1.0), | |
duration: duration, | |
builder: (context, animation, child) => Transform.translate( | |
offset: Offset(deltaX * shake(animation), 0), | |
child: child, | |
), | |
child: child, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment