From 30906ec752db469703f8f48c807e5ec726470150 Mon Sep 17 00:00:00 2001 From: Micha Date: Thu, 11 Apr 2024 15:08:44 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8Add=20tooltipOffset=20property?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/main.dart | 1 + lib/src/showcase.dart | 8 ++++++++ lib/src/tooltip_widget.dart | 20 +++++++++++++------- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 61a2d2dd..62de8f7c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -419,6 +419,7 @@ class MailTile extends StatelessWidget { targetBorderRadius: const BorderRadius.all( Radius.circular(150), ), + tooltipOffset: const Offset(20, -5), container: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ diff --git a/lib/src/showcase.dart b/lib/src/showcase.dart index c0fc2884..00efab61 100644 --- a/lib/src/showcase.dart +++ b/lib/src/showcase.dart @@ -247,6 +247,11 @@ class Showcase extends StatefulWidget { /// Disables barrier interaction for a particular showCase. final bool disableBarrierInteraction; + /// Defines the tooltip offset from the target widget. + /// + /// Default to Offset.zero + final Offset tooltipOffset; + const Showcase({ required this.key, required this.description, @@ -292,6 +297,7 @@ class Showcase extends StatefulWidget { this.descriptionTextDirection, this.onBarrierClick, this.disableBarrierInteraction = false, + this.tooltipOffset = Offset.zero, }) : height = null, width = null, container = null, @@ -332,6 +338,7 @@ class Showcase extends StatefulWidget { this.tooltipPosition, this.onBarrierClick, this.disableBarrierInteraction = false, + this.tooltipOffset = Offset.zero, }) : showArrow = false, onToolTipClick = null, scaleAnimationDuration = const Duration(milliseconds: 300), @@ -621,6 +628,7 @@ class _ShowcaseState extends State { descriptionPadding: widget.descriptionPadding, titleTextDirection: widget.titleTextDirection, descriptionTextDirection: widget.descriptionTextDirection, + tooltipOffset: widget.tooltipOffset, ), ], ], diff --git a/lib/src/tooltip_widget.dart b/lib/src/tooltip_widget.dart index 841053d8..09454d56 100644 --- a/lib/src/tooltip_widget.dart +++ b/lib/src/tooltip_widget.dart @@ -63,6 +63,7 @@ class ToolTipWidget extends StatefulWidget { final EdgeInsets? descriptionPadding; final TextDirection? titleTextDirection; final TextDirection? descriptionTextDirection; + final Offset tooltipOffset; const ToolTipWidget({ Key? key, @@ -89,6 +90,7 @@ class ToolTipWidget extends StatefulWidget { required this.tooltipBorderRadius, required this.scaleAnimationDuration, required this.scaleAnimationCurve, + required this.tooltipOffset, this.scaleAnimationAlignment, this.isTooltipDismissed = false, this.tooltipPosition, @@ -183,9 +185,9 @@ class _ToolTipWidgetState extends State if ((leftPositionValue + width) > widget.screenSize.width) { return null; } else if ((leftPositionValue) < _kDefaultPaddingFromParent) { - return _kDefaultPaddingFromParent; + return _kDefaultPaddingFromParent + widget.tooltipOffset.dx; } else { - return leftPositionValue; + return leftPositionValue + widget.tooltipOffset.dx; } } return null; @@ -201,7 +203,7 @@ class _ToolTipWidgetState extends State final rightPosition = widget.position!.getCenter() + (width * 0.5); return (rightPosition + width) > widget.screenSize.width - ? _kDefaultPaddingFromParent + ? _kDefaultPaddingFromParent + widget.tooltipOffset.dx : null; } else { return null; @@ -217,7 +219,7 @@ class _ToolTipWidgetState extends State } else if (space < (widget.contentWidth! / 2)) { space = 16; } - return space; + return space + widget.tooltipOffset.dx; } double _getAlignmentX() { @@ -341,10 +343,12 @@ class _ToolTipWidgetState extends State contentOrientation == TooltipPosition.bottom ? 1.0 : -1.0; isArrowUp = contentOffsetMultiplier == 1.0; - final contentY = isArrowUp + var contentY = isArrowUp ? widget.position!.getBottom() + (contentOffsetMultiplier * 3) : widget.position!.getTop() + (contentOffsetMultiplier * 3); + contentY = contentY + widget.tooltipOffset.dy; + final num contentFractionalOffset = contentOffsetMultiplier.clamp(-1.0, 0.0); @@ -546,14 +550,16 @@ class _ToolTipWidgetState extends State double? _getArrowLeft(double arrowWidth) { final left = _getLeft(); if (left == null) return null; - return (widget.position!.getCenter() - (arrowWidth / 2) - left); + return (widget.position!.getCenter() - (arrowWidth / 2) - left) + + widget.tooltipOffset.dx; } double? _getArrowRight(double arrowWidth) { if (_getLeft() != null) return null; return (widget.screenSize.width - widget.position!.getCenter()) - (_getRight() ?? 0) - - (arrowWidth / 2); + (arrowWidth / 2) + + widget.tooltipOffset.dx; } }