123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flyinsono/lab/color/lab_colors.dart';
- import 'package:flyinsono/lab/components/lab_text_tool_tip.dart';
- class RectIconButton extends StatefulWidget {
- const RectIconButton({
- super.key,
- this.onPressed,
- this.isSelected = false,
- this.icon = Icons.grid_view,
- this.tooltipMessage,
- this.bgColor = Colors.transparent,
- this.hoverBgColor = LabColors.base300,
- this.selectedBgColor = LabColors.buttonColor,
- this.iconColor = LabColors.text600,
- this.selectedIconColor = LabColors.text100,
- this.size = const Size(40, 40),
- this.iconSize = 25,
- });
- final VoidCallback? onPressed;
- final String? tooltipMessage;
- final bool isSelected;
- final IconData icon;
- final Color bgColor;
- final Color hoverBgColor;
- final Color selectedBgColor;
- final Color iconColor;
- final Color selectedIconColor;
- final Size size;
- final double iconSize;
- @override
- State<RectIconButton> createState() => _RectIconButtonState();
- }
- class _RectIconButtonState extends State<RectIconButton> {
- bool _isHover = false;
- @override
- Widget build(BuildContext context) {
- Color bgColor = _isHover ? widget.hoverBgColor : widget.bgColor;
- if (widget.isSelected) {
- bgColor = widget.selectedBgColor;
- }
- return LabTextTooltip(
- message: widget.tooltipMessage ?? '',
- disable: widget.isSelected || widget.tooltipMessage == null,
- offset: Offset(5, -8),
- child: Focus(
- onFocusChange: (value) {
- setState(() {
- _isHover = value;
- });
- },
- onKey: (node, event) {
- if (!(event is RawKeyDownEvent)) {
- return KeyEventResult.ignored;
- }
- if (event.logicalKey.keyLabel == 'Enter' ||
- event.logicalKey.keyLabel == 'Numpad Enter') {
- widget.onPressed?.call();
- return KeyEventResult.handled;
- }
- return KeyEventResult.ignored;
- },
- child: SizedBox(
- width: widget.size.width,
- height: widget.size.height,
- child: MouseRegion(
- cursor: SystemMouseCursors.click,
- onEnter: _handleMouseEnter,
- onExit: _handleMouseExit,
- child: GestureDetector(
- onTap: () {
- widget.onPressed?.call();
- },
- child: Container(
- decoration: BoxDecoration(
- color: bgColor,
- borderRadius: BorderRadius.circular(5),
- ),
- child: Icon(
- widget.icon,
- size: widget.iconSize,
- color: widget.isSelected
- ? widget.selectedIconColor
- : widget.iconColor,
- ),
- ),
- ),
- ),
- ),
- ),
- );
- }
- void _handleMouseEnter(PointerEvent details) {
- setState(() {
- _isHover = true;
- });
- }
- void _handleMouseExit(PointerEvent details) {
- setState(() {
- _isHover = false;
- });
- }
- }
|