hover_highlight.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'package:fis_ui/define.dart';
  2. import 'package:fis_ui/widgets/container/container.dart';
  3. import 'package:fis_ui/widgets/core.dart';
  4. import 'package:fis_ui/widgets/gesture/mouse_region.dart';
  5. import 'package:flutter/material.dart';
  6. /// 悬浮高亮组件容器
  7. class FHoverHighlightContainer extends FStatefulWidget {
  8. /// 子组件
  9. final FWidget child;
  10. /// 高亮颜色
  11. final Color? highlightColor;
  12. /// 装饰
  13. final BoxDecoration? decoration;
  14. FHoverHighlightContainer({
  15. required this.child,
  16. this.highlightColor,
  17. this.decoration,
  18. super.key,
  19. });
  20. @override
  21. _HoverHighlightState createState() => _HoverHighlightState();
  22. }
  23. class _HoverHighlightState extends FState<FHoverHighlightContainer> {
  24. bool _isHovered = false;
  25. @override
  26. FWidget build(BuildContext context) {
  27. final highLightColor =
  28. widget.highlightColor ?? Theme.of(context).highlightColor;
  29. final color = _isHovered ? highLightColor : Colors.transparent;
  30. final BoxDecoration decoration;
  31. if (widget.decoration != null) {
  32. decoration = widget.decoration!.copyWith(color: color);
  33. } else {
  34. decoration = BoxDecoration(color: color);
  35. }
  36. return FMouseRegion(
  37. onEnter: (_) => setState(() => _isHovered = true),
  38. onExit: (_) => setState(() => _isHovered = false),
  39. child: FContainer(
  40. decoration: decoration,
  41. child: widget.child,
  42. ),
  43. );
  44. }
  45. }