add hover detector to wrap a hovering widget

This commit is contained in:
lukas-heiligenbrunner 2022-10-07 14:33:07 +02:00
parent 9373df296c
commit d2fa3022d7

25
lib/hover_detector.dart Normal file
View File

@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
class HoverDetector extends StatefulWidget {
const HoverDetector(
{Key? key, required this.defaultChild, required this.hoverChild})
: super(key: key);
final Widget defaultChild;
final Widget hoverChild;
@override
State<HoverDetector> createState() => _HoverDetectorState();
}
class _HoverDetectorState extends State<HoverDetector> {
bool amIHovering = false;
@override
Widget build(BuildContext context) {
return MouseRegion(
child: amIHovering ? widget.hoverChild : widget.defaultChild,
onEnter: (PointerEvent details) => setState(() => amIHovering = true),
onExit: (PointerEvent details) => setState(() => amIHovering = false),
);
}
}