| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import 'package:flutter/material.dart';
- class ClickOpacity extends StatefulWidget {
- final Widget child;
- final VoidCallback? onTap;
- final VoidCallback? onLongPress;
- final double opacity;
- final Duration duration;
- final bool disabled;
- const ClickOpacity({
- super.key,
- required this.child,
- this.onTap,
- this.onLongPress,
- this.opacity = 0.5,
- this.duration = const Duration(milliseconds: 100),
- this.disabled = false,
- });
- @override
- State<ClickOpacity> createState() => _ClickOpacityState();
- }
- class _ClickOpacityState extends State<ClickOpacity> {
- bool _isPressed = false;
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTapDown: widget.disabled ? null : (_) => _setPressed(true),
- onTapUp: widget.disabled ? null : (_) => _setPressed(false),
- onTapCancel: widget.disabled ? null : () => _setPressed(false),
- onTap: widget.disabled ? null : widget.onTap,
- onLongPress: widget.disabled ? null : widget.onLongPress,
- child: AnimatedOpacity(
- opacity: _isPressed ? widget.opacity : 1.0,
- duration: widget.duration,
- child: widget.child,
- ),
- );
- }
- void _setPressed(bool value) {
- if (mounted && _isPressed != value) {
- setState(() {
- _isPressed = value;
- });
- }
- }
- }
|