Created
December 26, 2023 00:40
-
-
Save lukepighetti/3759b8ac2da330cc4441cba672f0cfe6 to your computer and use it in GitHub Desktop.
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'; | |
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