Skip to content

Instantly share code, notes, and snippets.

@lukepighetti
Created December 26, 2023 00:40
Show Gist options
  • Save lukepighetti/3759b8ac2da330cc4441cba672f0cfe6 to your computer and use it in GitHub Desktop.
Save lukepighetti/3759b8ac2da330cc4441cba672f0cfe6 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class Tappable extends StatefulWidget {
const Tappable({
super.key,
required this.onPressed,
required this.child,
});
final VoidCallback? onPressed;
final Widget child;
@override
State<Tappable> createState() => _TappableState();
}
class _TappableState extends State<Tappable> {
var pressed = false;
@override
Widget build(BuildContext context) {
final disabled = widget.onPressed == null;
return GestureDetector(
onTap: widget.onPressed,
onTapDown: (_) => setState(() => pressed = true),
onTapUp: (_) => setState(() => pressed = false),
onTapCancel: () => setState(() => pressed = false),
child: AnimatedOpacity(
opacity: pressed || disabled ? 0.5 : 1.0,
duration: const Duration(milliseconds: 100),
child: widget.child,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment