rect_icon_button.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flyinsono/lab/color/lab_colors.dart';
  4. import 'package:flyinsono/lab/components/lab_text_tool_tip.dart';
  5. class RectIconButton extends StatefulWidget {
  6. const RectIconButton({
  7. super.key,
  8. this.onPressed,
  9. this.isSelected = false,
  10. this.icon = Icons.grid_view,
  11. this.tooltipMessage,
  12. this.bgColor = Colors.transparent,
  13. this.hoverBgColor = LabColors.base300,
  14. this.selectedBgColor = LabColors.buttonColor,
  15. this.iconColor = LabColors.text600,
  16. this.selectedIconColor = LabColors.text100,
  17. this.size = const Size(40, 40),
  18. this.iconSize = 25,
  19. });
  20. final VoidCallback? onPressed;
  21. final String? tooltipMessage;
  22. final bool isSelected;
  23. final IconData icon;
  24. final Color bgColor;
  25. final Color hoverBgColor;
  26. final Color selectedBgColor;
  27. final Color iconColor;
  28. final Color selectedIconColor;
  29. final Size size;
  30. final double iconSize;
  31. @override
  32. State<RectIconButton> createState() => _RectIconButtonState();
  33. }
  34. class _RectIconButtonState extends State<RectIconButton> {
  35. bool _isHover = false;
  36. @override
  37. Widget build(BuildContext context) {
  38. Color bgColor = _isHover ? widget.hoverBgColor : widget.bgColor;
  39. if (widget.isSelected) {
  40. bgColor = widget.selectedBgColor;
  41. }
  42. return LabTextTooltip(
  43. message: widget.tooltipMessage ?? '',
  44. disable: widget.isSelected || widget.tooltipMessage == null,
  45. offset: Offset(5, -8),
  46. child: Focus(
  47. onFocusChange: (value) {
  48. setState(() {
  49. _isHover = value;
  50. });
  51. },
  52. onKey: (node, event) {
  53. if (!(event is RawKeyDownEvent)) {
  54. return KeyEventResult.ignored;
  55. }
  56. if (event.logicalKey.keyLabel == 'Enter' ||
  57. event.logicalKey.keyLabel == 'Numpad Enter') {
  58. widget.onPressed?.call();
  59. return KeyEventResult.handled;
  60. }
  61. return KeyEventResult.ignored;
  62. },
  63. child: SizedBox(
  64. width: widget.size.width,
  65. height: widget.size.height,
  66. child: MouseRegion(
  67. cursor: SystemMouseCursors.click,
  68. onEnter: _handleMouseEnter,
  69. onExit: _handleMouseExit,
  70. child: GestureDetector(
  71. onTap: () {
  72. widget.onPressed?.call();
  73. },
  74. child: Container(
  75. decoration: BoxDecoration(
  76. color: bgColor,
  77. borderRadius: BorderRadius.circular(5),
  78. ),
  79. child: Icon(
  80. widget.icon,
  81. size: widget.iconSize,
  82. color: widget.isSelected
  83. ? widget.selectedIconColor
  84. : widget.iconColor,
  85. ),
  86. ),
  87. ),
  88. ),
  89. ),
  90. ),
  91. );
  92. }
  93. void _handleMouseEnter(PointerEvent details) {
  94. setState(() {
  95. _isHover = true;
  96. });
  97. }
  98. void _handleMouseExit(PointerEvent details) {
  99. setState(() {
  100. _isHover = false;
  101. });
  102. }
  103. }