mirror of
https://github.com/gabehf/Fladder.git
synced 2026-03-07 21:48:14 -08:00
fix: More fixes custom tooltip
This commit is contained in:
parent
b9e9b50d72
commit
da8a729b9e
1 changed files with 46 additions and 70 deletions
|
|
@ -26,39 +26,23 @@ enum TooltipPosition { top, bottom, left, right }
|
||||||
|
|
||||||
class CustomTooltipState extends State<CustomTooltip> {
|
class CustomTooltipState extends State<CustomTooltip> {
|
||||||
OverlayEntry? _overlayEntry;
|
OverlayEntry? _overlayEntry;
|
||||||
Timer? _timer;
|
Timer? _tooltipTimer;
|
||||||
final GlobalKey _tooltipKey = GlobalKey();
|
final GlobalKey _tooltipKey = GlobalKey();
|
||||||
|
|
||||||
Timer? _timeOut;
|
|
||||||
|
|
||||||
void _resetTimer() {
|
|
||||||
_timeOut?.cancel();
|
|
||||||
_timeOut = Timer(const Duration(seconds: 2), () {
|
|
||||||
_hideTooltip();
|
|
||||||
_timeOut = null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void _showTooltip() {
|
void _showTooltip() {
|
||||||
_timer = Timer(widget.showDelay, () {
|
_tooltipTimer?.cancel();
|
||||||
_overlayEntry ??= _createOverlayEntry();
|
|
||||||
if (_overlayEntry != null) {
|
_tooltipTimer = Timer(widget.showDelay, () {
|
||||||
|
if (_overlayEntry == null) {
|
||||||
|
_overlayEntry = _createOverlayEntry();
|
||||||
Overlay.of(context).insert(_overlayEntry!);
|
Overlay.of(context).insert(_overlayEntry!);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
_timeOut = Timer(const Duration(seconds: 2), () {
|
|
||||||
_hideTooltip();
|
|
||||||
_timeOut = null;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _hideTooltip() {
|
void _hideTooltip() {
|
||||||
_timer?.cancel();
|
_tooltipTimer?.cancel();
|
||||||
_timeOut?.cancel();
|
_overlayEntry?.remove();
|
||||||
if (_overlayEntry?.mounted == true) {
|
|
||||||
_overlayEntry?.remove();
|
|
||||||
}
|
|
||||||
_overlayEntry = null;
|
_overlayEntry = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,58 +51,51 @@ class CustomTooltipState extends State<CustomTooltip> {
|
||||||
Offset targetPosition = renderBox.localToGlobal(Offset.zero);
|
Offset targetPosition = renderBox.localToGlobal(Offset.zero);
|
||||||
Size targetSize = renderBox.size;
|
Size targetSize = renderBox.size;
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
return OverlayEntry(
|
||||||
final tooltipRenderBox = _tooltipKey.currentContext?.findRenderObject() as RenderBox?;
|
builder: (context) {
|
||||||
if (tooltipRenderBox != null) {
|
final tooltipRenderBox = _tooltipKey.currentContext?.findRenderObject() as RenderBox?;
|
||||||
Size tooltipSize = tooltipRenderBox.size;
|
if (tooltipRenderBox != null) {
|
||||||
|
Size tooltipSize = tooltipRenderBox.size;
|
||||||
|
|
||||||
Offset tooltipPosition;
|
Offset tooltipPosition;
|
||||||
switch (widget.position) {
|
switch (widget.position) {
|
||||||
case TooltipPosition.top:
|
case TooltipPosition.top:
|
||||||
tooltipPosition = Offset(
|
tooltipPosition = Offset(
|
||||||
targetPosition.dx + (targetSize.width - tooltipSize.width) / 2,
|
targetPosition.dx + (targetSize.width - tooltipSize.width) / 2,
|
||||||
targetPosition.dy - tooltipSize.height - widget.offset,
|
targetPosition.dy - tooltipSize.height - widget.offset,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case TooltipPosition.bottom:
|
case TooltipPosition.bottom:
|
||||||
tooltipPosition = Offset(
|
tooltipPosition = Offset(
|
||||||
targetPosition.dx + (targetSize.width - tooltipSize.width) / 2,
|
targetPosition.dx + (targetSize.width - tooltipSize.width) / 2,
|
||||||
targetPosition.dy + targetSize.height + widget.offset,
|
targetPosition.dy + targetSize.height + widget.offset,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case TooltipPosition.left:
|
case TooltipPosition.left:
|
||||||
tooltipPosition = Offset(
|
tooltipPosition = Offset(
|
||||||
targetPosition.dx - tooltipSize.width - widget.offset,
|
targetPosition.dx - tooltipSize.width - widget.offset,
|
||||||
targetPosition.dy + (targetSize.height - tooltipSize.height) / 2,
|
targetPosition.dy + (targetSize.height - tooltipSize.height) / 2,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case TooltipPosition.right:
|
case TooltipPosition.right:
|
||||||
tooltipPosition = Offset(
|
tooltipPosition = Offset(
|
||||||
targetPosition.dx + targetSize.width + widget.offset,
|
targetPosition.dx + targetSize.width + widget.offset,
|
||||||
targetPosition.dy + (targetSize.height - tooltipSize.height) / 2,
|
targetPosition.dy + (targetSize.height - tooltipSize.height) / 2,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
_overlayEntry = OverlayEntry(
|
return Positioned(
|
||||||
builder: (context) => Positioned(
|
|
||||||
left: tooltipPosition.dx,
|
left: tooltipPosition.dx,
|
||||||
top: tooltipPosition.dy,
|
top: tooltipPosition.dy,
|
||||||
child: Material(
|
child: Material(
|
||||||
color: Colors.transparent,
|
color: Colors.transparent,
|
||||||
child: widget.tooltipContent,
|
child: widget.tooltipContent,
|
||||||
),
|
),
|
||||||
),
|
);
|
||||||
);
|
|
||||||
|
|
||||||
if (_overlayEntry != null) {
|
|
||||||
Overlay.of(context).insert(_overlayEntry!);
|
|
||||||
}
|
}
|
||||||
}
|
return const SizedBox.shrink();
|
||||||
});
|
},
|
||||||
|
|
||||||
return OverlayEntry(
|
|
||||||
builder: (context) => const SizedBox.shrink(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -128,7 +105,6 @@ class CustomTooltipState extends State<CustomTooltip> {
|
||||||
return MouseRegion(
|
return MouseRegion(
|
||||||
onEnter: (_) => _showTooltip(),
|
onEnter: (_) => _showTooltip(),
|
||||||
onExit: (_) => _hideTooltip(),
|
onExit: (_) => _hideTooltip(),
|
||||||
onHover: (_) => _resetTimer(),
|
|
||||||
child: Stack(
|
child: Stack(
|
||||||
children: [
|
children: [
|
||||||
widget.child,
|
widget.child,
|
||||||
|
|
@ -147,8 +123,8 @@ class CustomTooltipState extends State<CustomTooltip> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_timer?.cancel();
|
_tooltipTimer?.cancel();
|
||||||
_overlayEntry?.remove();
|
_hideTooltip(); // Ensure the tooltip is hidden on dispose
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue