Skip to content

Instantly share code, notes, and snippets.

@simonpham
Created April 16, 2020 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonpham/418cafe7e711c945c76a9d92c61a5914 to your computer and use it in GitHub Desktop.
Save simonpham/418cafe7e711c945c76a9d92c61a5914 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class Tappable extends StatefulWidget {
final Widget child;
final GestureTapCallback onTap;
final GestureTapCallback onDoubleTap;
final GestureLongPressCallback onLongPress;
final GestureTapDownCallback onTapDown;
final GestureTapCancelCallback onTapCancel;
final ValueChanged<bool> onHighlightChanged;
final ValueChanged<bool> onHover;
const Tappable({
Key key,
this.child,
this.onTap,
this.onDoubleTap,
this.onLongPress,
this.onTapDown,
this.onTapCancel,
this.onHighlightChanged,
this.onHover,
}) : super(key: key);
@override
_TappableState createState() => _TappableState();
}
class _TappableState extends State<Tappable> with TickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 75),
);
animation = Tween(begin: 1.0, end: 0.9).animate(
CurvedAnimation(
parent: animationController,
curve: Curves.easeOut,
),
);
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
child: AnimatedBuilder(
animation: animation,
builder: (context, _) {
return Transform.scale(
scale: animation.value,
child: Container(
color: Colors.transparent,
child: widget.child,
),
);
},
),
onTap: () async {
if (widget.onTap != null) {
widget.onTap();
}
await animationController.forward();
animationController.reverse();
},
onDoubleTap: widget.onDoubleTap,
onLongPress: widget.onLongPress,
onTapDown: (TapDownDetails details) {
animationController.forward();
if (widget.onTapDown != null) {
widget.onTapDown(details);
}
},
onTapUp: (TapUpDetails details) {
animationController.reverse();
},
onTapCancel: () {
animationController.reverse();
if (widget.onTapCancel != null) {
widget.onTapCancel();
}
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment