Skip to content

Instantly share code, notes, and snippets.

@luo3house
Last active April 12, 2023 08:17
Show Gist options
  • Save luo3house/baf89511f0c9e1b9784a5cb3f1eedb49 to your computer and use it in GitHub Desktop.
Save luo3house/baf89511f0c9e1b9784a5cb3f1eedb49 to your computer and use it in GitHub Desktop.
Flutter Widget: TouchOpacity
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