Last active
April 12, 2023 08:17
-
-
Save luo3house/baf89511f0c9e1b9784a5cb3f1eedb49 to your computer and use it in GitHub Desktop.
Flutter Widget: TouchOpacity
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/widgets.dart'; | |
class TouchOpacity extends StatefulWidget { | |
final Widget child; | |
final void Function()? onTap; | |
const TouchOpacity({ | |
super.key, | |
this.child = const SizedBox(), | |
this.onTap, | |
}); | |
@override | |
createState() => _TouchOpacity(); | |
} | |
class _TouchOpacity extends State<TouchOpacity> { | |
var press = false; | |
@override | |
Widget build(BuildContext context) { | |
return Listener( | |
behavior: HitTestBehavior.opaque, | |
onPointerDown: (e) => setState(() => press = true), | |
onPointerUp: (e) => setState(() => press = false), | |
child: GestureDetector( | |
behavior: HitTestBehavior.opaque, | |
onTap: widget.onTap, | |
child: Opacity( | |
opacity: press ? .65 : 1, | |
child: widget.child, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment