hover.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:flutter/material.dart';
  2. // ignore: unused_import
  3. import 'package:flowy_infra/time/duration.dart';
  4. typedef HoverBuilder = Widget Function(BuildContext context, bool onHover);
  5. class FlowyHover extends StatefulWidget {
  6. final HoverDisplayConfig config;
  7. final HoverBuilder builder;
  8. const FlowyHover({
  9. Key? key,
  10. required this.builder,
  11. required this.config,
  12. }) : super(key: key);
  13. @override
  14. State<FlowyHover> createState() => _FlowyHoverState();
  15. }
  16. class _FlowyHoverState extends State<FlowyHover> {
  17. bool _onHover = false;
  18. @override
  19. Widget build(BuildContext context) {
  20. return MouseRegion(
  21. cursor: SystemMouseCursors.click,
  22. onEnter: (p) => setOnHover(true),
  23. onExit: (p) => setOnHover(false),
  24. child: render(),
  25. );
  26. }
  27. void setOnHover(bool value) => setState(() => _onHover = value);
  28. Widget render() {
  29. if (_onHover) {
  30. return FlowyHoverBackground(
  31. config: widget.config,
  32. child: widget.builder(context, _onHover),
  33. );
  34. } else {
  35. return widget.builder(context, _onHover);
  36. }
  37. }
  38. }
  39. class HoverDisplayConfig {
  40. final Color borderColor;
  41. final double borderWidth;
  42. final Color hoverColor;
  43. final BorderRadius borderRadius;
  44. const HoverDisplayConfig(
  45. {this.borderColor = Colors.transparent,
  46. this.borderWidth = 0,
  47. this.borderRadius = const BorderRadius.all(Radius.circular(6)),
  48. required this.hoverColor});
  49. }
  50. class FlowyHoverBackground extends StatelessWidget {
  51. final HoverDisplayConfig config;
  52. final Widget child;
  53. const FlowyHoverBackground({
  54. Key? key,
  55. required this.child,
  56. required this.config,
  57. }) : super(key: key);
  58. @override
  59. Widget build(BuildContext context) {
  60. final hoverBorder = Border.all(
  61. color: config.borderColor,
  62. width: config.borderWidth,
  63. );
  64. return Container(
  65. decoration: BoxDecoration(
  66. border: hoverBorder,
  67. color: config.hoverColor,
  68. borderRadius: config.borderRadius,
  69. ),
  70. child: child,
  71. );
  72. }
  73. }