Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import 'package:flutter/material.dart';
class TapScaleAnimated extends StatefulWidget {
final Widget child;
final Function onTap;
final double scale;
TapScaleAnimated({this.child, @required this.onTap, this.scale = 0.9});
@override
_TapScaleAnimatedState createState() => _TapScaleAnimatedState();
}
class _TapScaleAnimatedState extends State<TapScaleAnimated> with SingleTickerProviderStateMixin {
AnimationController _scaleController;
@override
void initState() {
super.initState();
_scaleController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 200),
lowerBound: 0,
upperBound: 1 - widget.scale
)..addListener(() {
setState(() {});
});
}
@override
void dispose() {
_scaleController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTapDown: (details){
_scaleController.forward();
},
onTapUp: (details){
_scaleController.reverse();
widget.onTap();
},
child: Transform.scale(
scale: 1 - _scaleController.value,
child: widget.child,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment