Skip to content

Instantly share code, notes, and snippets.

@buehler
Created March 3, 2023 04:25
Show Gist options
  • Save buehler/66f4d5de6eb78d71a5f84bc91b3fe62f to your computer and use it in GitHub Desktop.
Save buehler/66f4d5de6eb78d71a5f84bc91b3fe62f to your computer and use it in GitHub Desktop.
"Clickable" widget for flutter that makes any widget clickable and hoverable.
import 'package:flutter/widgets.dart';
/// Makes any widget inside clickable and hover-able.
/// If the onPressed callback is null, the widget will not catch any
/// click events (but still hover events).
class Clickable extends StatefulWidget {
final Widget Function(BuildContext context, bool isHovered) builder;
final VoidCallback? onPressed;
const Clickable({super.key, this.onPressed, required this.builder});
@override
State<Clickable> createState() => _ClickableState();
}
class _ClickableState extends State<Clickable> {
var _isHovered = false;
@override
Widget build(BuildContext context) => MouseRegion(
onEnter: (_) => setState(() => _isHovered = true),
onExit: (_) => setState(() => _isHovered = false),
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: widget.onPressed,
child: widget.builder(context, _isHovered),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment