import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

/// 自定义实现，替代已停维的 `hovering` 包的 [HoverWidget]。
///
/// API 兼容：
/// ```dart
/// HoverWidget(
///   child: ...,
///   hoverChild: ...,
///   onHover: (PointerHoverEvent event) { ... },
/// )
/// ```
///
/// 鼠标悬停时显示 [hoverChild]，否则显示 [child]。
class HoverWidget extends StatefulWidget {
  final Widget child;
  final Widget? hoverChild;
  final void Function(PointerHoverEvent)? onHover;

  const HoverWidget({
    super.key,
    required this.child,
    this.hoverChild,
    this.onHover,
  });

  @override
  State<HoverWidget> createState() => _HoverWidgetState();
}

class _HoverWidgetState extends State<HoverWidget> {
  bool _hovering = false;

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      onHover: widget.onHover,
      onEnter: (_) => setState(() => _hovering = true),
      onExit: (_) => setState(() => _hovering = false),
      child: (_hovering && widget.hoverChild != null)
          ? widget.hoverChild!
          : widget.child,
    );
  }
}
