Skip to content

Commit

Permalink
feat: ✨ Provide Manual option for vertical position of tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyMakadiyaS committed Nov 21, 2022
1 parent 8a158dd commit eb95f38
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class _MailPageState extends State<MailPage> {
children: <Widget>[
Showcase(
key: _one,
tooltipPosition: TooltipPosition.top,
description: 'Tap to see menu options',
disableDefaultTargetGestures: true,
child: GestureDetector(
Expand Down Expand Up @@ -315,6 +316,7 @@ class _MailPageState extends State<MailPage> {
child: Showcase(
key: key,
description: 'Tap to check mail',
tooltipPosition: TooltipPosition.top,
disposeOnTap: true,
onTargetClick: () {
Navigator.push<void>(
Expand Down
3 changes: 2 additions & 1 deletion lib/showcaseview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@

library showcaseview;

export 'src/enum.dart';
export 'src/showcase.dart';
export 'src/showcase_widget.dart';
export 'src/showcase_widget.dart';
1 change: 1 addition & 0 deletions lib/src/enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum TooltipPosition { top, bottom }
6 changes: 6 additions & 0 deletions lib/src/showcase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -540,6 +545,7 @@ class _ShowcaseState extends State<Showcase> {
scaleAnimationCurve: widget.scaleAnimationCurve,
scaleAnimationAlignment: widget.scaleAnimationAlignment,
isTooltipDismissed: _isTooltipDismissed,
tooltipPosition: widget.tooltipPosition,
),
],
)
Expand Down
29 changes: 15 additions & 14 deletions lib/src/tooltip_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'dart:math';

import 'package:flutter/material.dart';

import 'enum.dart';
import 'get_position.dart';
import 'measure_size.dart';

Expand Down Expand Up @@ -55,6 +56,7 @@ class ToolTipWidget extends StatefulWidget {
final Curve scaleAnimationCurve;
final Alignment? scaleAnimationAlignment;
final bool isTooltipDismissed;
final TooltipPosition? tooltipPosition;

const ToolTipWidget({
Key? key,
Expand Down Expand Up @@ -83,6 +85,7 @@ class ToolTipWidget extends StatefulWidget {
required this.scaleAnimationCurve,
this.scaleAnimationAlignment,
this.isTooltipDismissed = false,
required this.tooltipPosition,
}) : super(key: key);

@override
Expand All @@ -104,24 +107,21 @@ class _ToolTipWidgetState extends State<ToolTipWidget>
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() {
Expand Down Expand Up @@ -305,7 +305,8 @@ class _ToolTipWidgetState extends State<ToolTipWidget>
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
Expand Down

0 comments on commit eb95f38

Please sign in to comment.