Skip to content

Instantly share code, notes, and snippets.

@bobagold
Created June 5, 2020 10:01
Show Gist options
  • Save bobagold/3cf690d17243a53bdee9fba5f14f15d3 to your computer and use it in GitHub Desktop.
Save bobagold/3cf690d17243a53bdee9fba5f14f15d3 to your computer and use it in GitHub Desktop.
Shake any widget in flutter
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