From eb95f38c14ff452a678cf75128ce62ae02631feb Mon Sep 17 00:00:00 2001 From: Happy Makadiya Date: Mon, 21 Nov 2022 13:00:23 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Provide=20Manual=20option?= =?UTF-8?q?=20for=20vertical=20position=20of=20tooltip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/main.dart | 2 ++ lib/showcaseview.dart | 3 ++- lib/src/enum.dart | 1 + lib/src/showcase.dart | 6 ++++++ lib/src/tooltip_widget.dart | 29 +++++++++++++++-------------- 5 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 lib/src/enum.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 7ff8e01f..412e5ab9 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -180,6 +180,7 @@ class _MailPageState extends State { children: [ Showcase( key: _one, + tooltipPosition: TooltipPosition.top, description: 'Tap to see menu options', disableDefaultTargetGestures: true, child: GestureDetector( @@ -315,6 +316,7 @@ class _MailPageState extends State { child: Showcase( key: key, description: 'Tap to check mail', + tooltipPosition: TooltipPosition.top, disposeOnTap: true, onTargetClick: () { Navigator.push( diff --git a/lib/showcaseview.dart b/lib/showcaseview.dart index 1b8c8d7e..c9411d1d 100644 --- a/lib/showcaseview.dart +++ b/lib/showcaseview.dart @@ -22,5 +22,6 @@ library showcaseview; +export 'src/enum.dart'; export 'src/showcase.dart'; -export 'src/showcase_widget.dart'; +export 'src/showcase_widget.dart'; \ No newline at end of file diff --git a/lib/src/enum.dart b/lib/src/enum.dart new file mode 100644 index 00000000..b1a8a390 --- /dev/null +++ b/lib/src/enum.dart @@ -0,0 +1 @@ +enum TooltipPosition { top, bottom } diff --git a/lib/src/showcase.dart b/lib/src/showcase.dart index 47f944d1..1b1b6892 100644 --- a/lib/src/showcase.dart +++ b/lib/src/showcase.dart @@ -26,6 +26,7 @@ import 'dart:ui'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'enum.dart'; import 'extension.dart'; import 'get_position.dart'; import 'layout_overlays.dart'; @@ -220,6 +221,8 @@ class Showcase extends StatefulWidget { /// Alignment(-0.2,0.3) or Alignment.centerLeft /// ``` final Alignment? scaleAnimationAlignment; + + final TooltipPosition? tooltipPosition; const Showcase({ required this.key, @@ -260,6 +263,7 @@ class Showcase extends StatefulWidget { this.scaleAnimationDuration = const Duration(milliseconds: 300), this.scaleAnimationCurve = Curves.easeIn, this.scaleAnimationAlignment, + this.tooltipPosition, }) : height = null, width = null, container = null, @@ -301,6 +305,7 @@ class Showcase extends StatefulWidget { this.onTargetLongPress, this.onTargetDoubleTap, this.disableDefaultTargetGestures = false, + this.tooltipPosition, }) : showArrow = false, onToolTipClick = null, scaleAnimationDuration = const Duration(milliseconds: 300), @@ -540,6 +545,7 @@ class _ShowcaseState extends State { scaleAnimationCurve: widget.scaleAnimationCurve, scaleAnimationAlignment: widget.scaleAnimationAlignment, isTooltipDismissed: _isTooltipDismissed, + tooltipPosition: widget.tooltipPosition, ), ], ) diff --git a/lib/src/tooltip_widget.dart b/lib/src/tooltip_widget.dart index d0613307..be405f0a 100644 --- a/lib/src/tooltip_widget.dart +++ b/lib/src/tooltip_widget.dart @@ -24,6 +24,7 @@ import 'dart:math'; import 'package:flutter/material.dart'; +import 'enum.dart'; import 'get_position.dart'; import 'measure_size.dart'; @@ -55,6 +56,7 @@ class ToolTipWidget extends StatefulWidget { final Curve scaleAnimationCurve; final Alignment? scaleAnimationAlignment; final bool isTooltipDismissed; + final TooltipPosition? tooltipPosition; const ToolTipWidget({ Key? key, @@ -83,6 +85,7 @@ class ToolTipWidget extends StatefulWidget { required this.scaleAnimationCurve, this.scaleAnimationAlignment, this.isTooltipDismissed = false, + required this.tooltipPosition, }) : super(key: key); @override @@ -104,24 +107,21 @@ class _ToolTipWidgetState extends State double tooltipScreenEdgePadding = 20; double tooltipTextPadding = 15; - bool isCloseToTopOrBottom(Offset position) { + TooltipPosition findPositionForContent(Offset position) { var height = 120.0; height = widget.contentHeight ?? height; final bottomPosition = position.dy + ((widget.position?.getHeight() ?? 0) / 2); final topPosition = position.dy - ((widget.position?.getHeight() ?? 0) / 2); - return ((widget.screenSize?.height ?? MediaQuery.of(context).size.height) - - bottomPosition) <= - height && - topPosition >= height; - } - - String findPositionForContent(Offset position) { - if (isCloseToTopOrBottom(position)) { - return 'ABOVE'; - } else { - return 'BELOW'; - } + final hasSpaceInTop = topPosition >= height; + final hasSpaceInBottom = + ((widget.screenSize?.height ?? MediaQuery.of(context).size.height) - + bottomPosition) >= + height; + return widget.tooltipPosition ?? + (hasSpaceInTop && !hasSpaceInBottom + ? TooltipPosition.top + : TooltipPosition.bottom); } void _getTooltipWidth() { @@ -305,7 +305,8 @@ class _ToolTipWidgetState extends State Widget build(BuildContext context) { position = widget.offset; final contentOrientation = findPositionForContent(position!); - final contentOffsetMultiplier = contentOrientation == "BELOW" ? 1.0 : -1.0; + final contentOffsetMultiplier = + contentOrientation == TooltipPosition.bottom ? 1.0 : -1.0; isArrowUp = contentOffsetMultiplier == 1.0; final contentY = isArrowUp