forked from bensonarafat/super_tooltip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuper_tooltip.dart
More file actions
1036 lines (921 loc) · 34.3 KB
/
Copy pathsuper_tooltip.dart
File metadata and controls
1036 lines (921 loc) · 34.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ignore_for_file: comment_references
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:super_tooltip/src/utils.dart';
import 'bubble_shape.dart';
import 'enums.dart';
import 'shape_overlay.dart';
import 'super_tooltip_controller.dart';
import 'tooltip_position_delegate.dart';
typedef DecorationBuilder = Decoration Function(
Offset target,
);
/// A powerful and customizable tooltip widget for Flutter.
///
/// `SuperTooltip` provides a flexible and feature-rich way to display tooltips
/// in your Flutter applications. It offers several advantages over the standard
/// Flutter `Tooltip` widget, including:
///
/// * **Flexible Positioning:** Control the tooltip's position relative to its
/// target widget using the `popupDirection`, `top`, `right`, `bottom`, and
/// `left` parameters.
/// * **Customizable Appearance:** Customize the tooltip's background color,
/// border, shadow, and more using the `backgroundColor`, `decoration`,
/// `borderColor`, and other styling parameters.
/// * **Barrier and Blur:** Optionally display a barrier (scrim) and blur effect
/// behind the tooltip using the `showBarrier`, `barrierColor`,
/// `showDropBoxFilter`, `sigmaX`, and `sigmaY` parameters.
/// * **Close Button:** Add a close button to allow users to manually dismiss the
/// tooltip using the `showCloseButton` and `closeButtonType` parameters.
/// * **Animation:** Smooth fade-in and fade-out animations for a visually
/// appealing experience.
/// * **Event Callbacks:** Trigger actions when the tooltip is shown or hidden
/// using the `onShow` and `onHide` callbacks.
/// * **Touch-Through Area:** Define an area that allows touch events to pass
/// through the barrier using the `touchThroughArea` parameter.
///
/// To use `SuperTooltip`, wrap your target widget with a `GestureDetector`,
/// `MouseRegion`, or `InkWell` and use the `controller` to manage the
/// tooltip's visibility.
///
/// ```dart
/// final _controller = SuperTooltipController();
///
/// GestureDetector(
/// onTap: () => _controller.showTooltip(),
/// child: SuperTooltip(
/// controller: _controller,
/// content: const Text('This is a tooltip!'),
/// child: const Icon(Icons.info),
/// ),
/// )
/// ```
class SuperTooltip extends StatefulWidget {
// Creates a `SuperTooltip` widget.
///
/// The `content` parameter is required and specifies the widget to be
/// displayed inside the tooltip.
final Widget content;
/// The direction in which the tooltip should appear relative to its
/// target widget.
///
/// Defaults to [TooltipDirection.down].
///
/// See also:
///
/// * [TooltipDirection], which defines the possible tooltip directions.
final TooltipDirection popupDirection;
/// The direction in which the tooltip should appear relative to its
/// target widget.
///
/// Defaults to [TooltipDirection.down].
///
/// See also:
///
/// * [TooltipDirection], which defines the possible tooltip directions.
final TooltipDirection Function()? popupDirectionBuilder;
/// A [SuperTooltipController] to manage the tooltip's visibility and state.
///
/// If not provided, a new [SuperTooltipController] will be created
/// internally.
final SuperTooltipController? controller;
/// Called when the user long presses the target widget.
final void Function()? onLongPress;
/// Called when the tooltip is shown.
final void Function()? onShow;
/// Called when the tooltip is hidden.
final void Function()? onHide;
/// Whether the tooltip should snap to the top or bottom of the screen
/// if there's not enough space in the preferred direction.
///
/// Defaults to `false`.
final bool snapsFarAwayVertically;
/// Whether the tooltip should snap to the left or right of the screen
/// if there's not enough space in the preferred direction.
///
/// Defaults to `false`.
final bool snapsFarAwayHorizontally;
/// Whether the tooltip should have a shadow.
///
/// Defaults to `true`.
final bool? hasShadow;
/// The color of the shadow.
///
/// If not provided, the default color will be used.
final Color? shadowColor;
/// The blur radius of the shadow.
///
/// If not provided, the default blur radius will be used.
final double? shadowBlurRadius;
/// The spread radius of the shadow.
///
/// If not provided, the default spread radius will be used.
final double? shadowSpreadRadius;
/// The offset of the shadow.
///
/// If not provided, the default offset will be used.
final Offset? shadowOffset;
/// [top], [right], [bottom], [left] define the distance between the respective
/// edges of the tooltip and the corresponding edges of the screen.
///
/// If not provided, the tooltip will be positioned as close as possible
/// to the specified edge, respecting the `minimumOutsideMargin`.
final double? top, right, bottom, left;
/// Whether to display a close button inside the tooltip.
///
/// Defaults to `false`.
final bool showCloseButton;
/// The type of close button to display.
///
/// Defaults to [CloseButtonType.inside].
///
/// See also:
///
/// * [CloseButtonType], which defines the possible close button types.
final CloseButtonType closeButtonType;
/// The color of the close button.
///
/// If not provided, the default color will be used.
final Color? closeButtonColor;
/// The size of the close button.
///
/// If not provided, the default size will be used.
final double? closeButtonSize;
/// The minimum margin between the tooltip and the edges of the screen.
///
/// Defaults to `20.0`.
final double minimumOutsideMargin;
/// The vertical offset of the tooltip from its target widget.
///
/// Defaults to `0.0`.
final double verticalOffset;
/// The target widget to which the tooltip is attached.
final Widget? child;
/// The border color of the tooltip.
///
/// Defaults to `Colors.black`.
final Color borderColor;
/// Box constraints for the tooltip's size.
///
/// Defaults to:
/// ```dart
/// const BoxConstraints(
/// minHeight: 0.0,
/// maxHeight: double.infinity,
/// minWidth: 0.0,
/// maxWidth: double.infinity,
/// )
/// ```
final BoxConstraints constraints;
/// The background color of the tooltip.
///
/// If not provided, the default background color will be used.
final Color? backgroundColor;
/// A custom decoration for the tooltip.
///
/// If not provided, the default decoration will be used.
final DecorationBuilder? decorationBuilder;
/// The elevation of the tooltip.
///
/// Defaults to `0.0`.
final double elevation;
/// The duration of the fade-in animation.
///
/// Defaults to `const Duration(milliseconds: 150)`.
final Duration fadeInDuration;
/// The duration of the fade-out animation.
///
/// Defaults to `const Duration(milliseconds: 0)`.
final Duration fadeOutDuration;
/// The length of the tooltip's arrow.
///
/// Defaults to `20.0`.
final double arrowLength;
/// The width of the tooltip's arrow base.
///
/// Defaults to `20.0`.
final double arrowBaseWidth;
/// The distance between the arrow tip and the target widget.
///
/// Defaults to `2.0`.
final double arrowTipRadius;
final double arrowTipDistance;
/// The border radius of the tooltip.
///
/// Defaults to `10.0`.
final double borderRadius;
/// The width of the tooltip's border.
///
/// Defaults to `0.0`.
final double borderWidth;
/// Whether to display a barrier (scrim) behind the tooltip.
///
/// Defaults to `true`.
final bool? showBarrier;
/// The color of the barrier.
///
/// If not provided, the default color will be used.
final Color? barrierColor;
/// A rectangular area that allows touch events to pass through the barrier.
final Rect? touchThroughArea;
/// The shape of the touch-through area.
///
/// Defaults to [ClipAreaShape.oval].
///
/// See also:
///
/// * [ClipAreaShape], which defines the possible touch-through area shapes.
final ClipAreaShape touchThroughAreaShape;
/// The corner radius of the touch-through area.
///
/// Defaults to `5.0`.
final double touchThroughAreaCornerRadius;
/// EdgeInsetsGeometry for the overlay.
///
/// Defaults to `const EdgeInsets.all(10)`.
final EdgeInsetsGeometry overlayDimensions;
/// EdgeInsetsGeometry for the bubble.
///
/// Defaults to `const EdgeInsets.all(10)`.
final EdgeInsetsGeometry bubbleDimensions;
/// Whether to hide the tooltip when tapped.
///
/// Defaults to `false`.
final bool hideTooltipOnTap;
/// Whether to hide the tooltip when the barrier is tapped.
///
/// Defaults to `true`.
final bool hideTooltipOnBarrierTap;
/// Whether to toggle the tooltip's visibility when tapped.
///
/// Defaults to `false`.
final bool toggleOnTap;
final bool showOnTap;
/// Whether to show a blur filter behind the tooltip.
///
/// Defaults to `false`.
final bool showDropBoxFilter;
/// The sigmaX value for the blur filter (if `showDropBoxFilter` is `true`).
///
/// Defaults to `5.0`.
final double sigmaX;
/// The sigmaY value for the blur filter (if `showDropBoxFilter` is `true`).
///
/// Defaults to `5.0`.
final double sigmaY;
/// A list of box shadows to apply to the tooltip.
final List<BoxShadow>? boxShadows;
/// Whether the tooltip should be click-through.
///
/// Defaults to `false`.
final bool clickThrough;
/// Whether to automatically show the tooltip when the mouse pointer hovers over the [child].
///
/// This feature utilizes [MouseRegion] and is primarily intended for Web and Desktop platforms.
/// On touch-based mobile devices, this parameter is generally ignored unless a mouse is connected.
///
/// Defaults to `false`.
final bool showOnHover;
/// Whether to automatically hide the tooltip when the mouse pointer leaves the [child]'s bounds.
///
/// This is primarily intended for Web and Desktop platforms.
///
/// **Note:** On Web/Desktop, enabling this will automatically disable the modal barrier
/// (regardless of [showBarrier]) to ensure the mouse can exit the widget area without
/// being blocked by the overlay.
///
/// Defaults to `false`.
final bool hideOnHoverExit;
SuperTooltip({
Key? key,
required this.content,
this.popupDirection = TooltipDirection.down,
this.controller,
this.onLongPress,
this.onShow,
this.onHide,
this.popupDirectionBuilder,
/**
* showCloseButton
* This will enable the closeButton
*/
this.showCloseButton = false,
this.closeButtonType = CloseButtonType.inside,
this.closeButtonColor,
this.closeButtonSize,
this.showBarrier,
this.barrierColor,
this.snapsFarAwayVertically = false,
this.snapsFarAwayHorizontally = false,
this.hasShadow,
this.shadowColor,
this.shadowBlurRadius,
this.shadowSpreadRadius,
this.shadowOffset,
this.top,
this.right,
this.bottom,
this.left,
// TD: Make edgeinsets instead
this.minimumOutsideMargin = 20.0,
this.verticalOffset = 0.0,
this.elevation = 0.0,
// TD: The native flutter tooltip uses verticalOffset
// to space the tooltip from the child. But we'll likely
// need just offset, since it's 4 way directional
// this.verticalOffset = 24.0,
this.backgroundColor,
//
//
//
this.decorationBuilder,
this.child,
this.borderColor = Colors.black,
this.constraints = const BoxConstraints(
minHeight: 0.0,
maxHeight: double.infinity,
minWidth: 0.0,
maxWidth: double.infinity,
),
this.fadeInDuration = const Duration(milliseconds: 150),
this.fadeOutDuration = const Duration(milliseconds: 0),
this.arrowLength = 20.0,
this.arrowBaseWidth = 20.0,
this.arrowTipRadius = 0.0,
this.arrowTipDistance = 2.0,
this.touchThroughAreaShape = ClipAreaShape.oval,
this.touchThroughAreaCornerRadius = 5.0,
this.touchThroughArea,
this.borderWidth = 0.0,
this.borderRadius = 10.0,
this.overlayDimensions = const EdgeInsets.all(10),
this.bubbleDimensions = const EdgeInsets.all(10),
this.hideTooltipOnTap = false,
this.sigmaX = 5.0,
this.sigmaY = 5.0,
this.showDropBoxFilter = false,
this.hideTooltipOnBarrierTap = true,
this.toggleOnTap = false,
this.showOnTap = true,
this.boxShadows,
this.clickThrough = false,
this.showOnHover = false,
this.hideOnHoverExit = false,
}) : assert(showDropBoxFilter ? showBarrier ?? false : true,
'showDropBoxFilter or showBarrier can\'t be false | null'),
super(key: key);
/// Key used to identify the inside close button.
static Key insideCloseButtonKey = const Key("InsideCloseButtonKey");
/// Key used to identify the outside close button.
static Key outsideCloseButtonKey = const Key("OutsideCloseButtonKey");
/// Key used to identify the barrier.
static Key barrierKey = const Key("barrierKey");
/// Key used to identify the bubble.
static Key bubbleKey = const Key("bubbleKey");
@override
State createState() => _SuperTooltipState();
}
class _SuperTooltipState extends State<SuperTooltip>
with SingleTickerProviderStateMixin {
final LayerLink _layerLink = LayerLink();
late AnimationController _animationController;
SuperTooltipController? _superTooltipController;
OverlayEntry? _entry;
OverlayEntry? _barrierEntry;
OverlayEntry? blur;
bool showCloseButton = false;
CloseButtonType closeButtonType = CloseButtonType.inside;
Color? closeButtonColor;
double? closeButtonSize;
late bool showBarrier;
Color? barrierColor;
late bool hasShadow;
late Color shadowColor;
late double shadowBlurRadius;
late double shadowSpreadRadius;
late Offset shadowOffset;
late bool showBlur;
late TooltipDirection _resolvedDirection;
@override
void initState() {
_animationController = AnimationController(
duration: widget.fadeInDuration,
reverseDuration: widget.fadeOutDuration,
vsync: this,
);
_superTooltipController = widget.controller ?? SuperTooltipController();
_superTooltipController!.addListener(_onChangeNotifier);
// TD: Mouse stuff
super.initState();
}
@override
void didUpdateWidget(SuperTooltip oldWidget) {
if (_superTooltipController != widget.controller) {
_superTooltipController!.removeListener(_onChangeNotifier);
if (oldWidget.controller == null) {
_superTooltipController?.dispose();
}
_superTooltipController = widget.controller ?? SuperTooltipController();
_superTooltipController!.addListener(_onChangeNotifier);
}
super.didUpdateWidget(oldWidget);
}
// @override
@override
void dispose() {
if (_entry != null) _removeEntries();
_superTooltipController?.removeListener(_onChangeNotifier);
if (widget.controller == null) {
_superTooltipController?.dispose();
}
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
showCloseButton = widget.showCloseButton;
closeButtonType = widget.closeButtonType;
closeButtonColor = widget.closeButtonColor ?? Colors.black;
closeButtonSize = widget.closeButtonSize ?? 30.0;
barrierColor = widget.barrierColor ?? Colors.black54;
hasShadow = widget.hasShadow ?? true;
shadowColor = widget.shadowColor ?? Colors.black54;
shadowBlurRadius = widget.shadowBlurRadius ?? 10.0;
shadowSpreadRadius = widget.shadowSpreadRadius ?? 5.0;
shadowOffset = widget.shadowOffset ?? Offset.zero;
showBlur = widget.showDropBoxFilter;
/// On native mobile platforms, this parameter is ignored as hover events are not supported.
/// The widget reverts to standard barrier behavior (tap-to-dismiss) to prevent the
/// tooltip from becoming unresponsive.
var isNativeMobile = !kIsWeb &&
(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.android);
if (isNativeMobile) {
/// On native mobile platforms, this parameter is ignored as hover events are not supported.
/// The widget reverts to standard barrier behavior (tap-to-dismiss) to prevent the
/// tooltip from becoming unresponsive.
showBarrier = widget.showBarrier ?? true;
} else {
/// On Web and Desktop, if [hideOnHoverExit] is true, the barrier is
/// automatically disabled regardless of this value. This ensures that
/// the barrier does not obstruct the mouse cursor from triggering the exit event.
showBarrier = widget.hideOnHoverExit ? false : widget.showBarrier ?? true;
}
return MouseRegion(
hitTestBehavior: HitTestBehavior.translucent,
onEnter: (_) {
if (widget.showOnHover) {
if (!_superTooltipController!.isVisible) {
_superTooltipController!.showTooltip();
}
}
},
onExit: (_) {
if (widget.hideOnHoverExit) {
if (_superTooltipController!.isVisible) {
_superTooltipController!.hideTooltip();
}
}
},
child: CompositedTransformTarget(
link: _layerLink,
child: GestureDetector(
onTap: () {
if (widget.toggleOnTap && _superTooltipController!.isVisible) {
_superTooltipController!.hideTooltip();
} else {
if (widget.showOnTap) {
_superTooltipController!.showTooltip();
}
}
},
onLongPress: widget.onLongPress,
child: widget.child,
),
),
);
}
/// Called when the [_superTooltipController] notifies its listeners.
///
/// This method is used to show or hide the tooltip based on the event type.
/// If the event is [Event.show], the [_showTooltip] method is called. If the
/// event is [Event.hide], the [_hideTooltip] method is called.
void _onChangeNotifier() {
switch (_superTooltipController!.event) {
// Show the tooltip.
case Event.show:
_showTooltip();
break;
// Hide the tooltip.
case Event.hide:
_hideTooltip();
break;
}
}
/// Creates the overlay entries for the tooltip, barrier, and blur filter (if enabled).
///
/// The overlay entries are inserted into the [Overlay] using the [OverlayState.insertAll] method.
/// The order of insertion is: blur filter overlay entry, barrier overlay entry, and tooltip overlay entry.
void _createOverlayEntries() {
// Find the render box of the widget.
final renderBox = context.findRenderObject() as RenderBox;
// Find the overlay state.
final overlayState = Overlay.of(context);
// Find the overlay render box (if available).
RenderBox? overlay;
// ignore: unnecessary_null_comparison
if (overlayState != null) {
overlay = overlayState.context.findRenderObject() as RenderBox?;
}
// Calculate the size of the widget.
final size = renderBox.size;
// Calculate the target position relative to the global coordinate system.
final target = renderBox.localToGlobal(size.center(Offset.zero));
final animation = CurvedAnimation(
parent: _animationController,
curve: Curves.fastOutSlowIn,
);
final offsetToTarget = Offset(
-target.dx + size.width / 2,
-target.dy + size.height / 2,
);
final backgroundColor =
widget.backgroundColor ?? Theme.of(context).cardColor;
var constraints = widget.constraints;
var preferredDirection =
widget.popupDirectionBuilder?.call() ?? widget.popupDirection;
var left = widget.left;
var right = widget.right;
var top = widget.top;
var bottom = widget.bottom;
_resolvedDirection = preferredDirection;
// When [TooltipDirection.auto] is specified, the tooltip direction is
// dynamically resolved based on available space around the target widget.
if (preferredDirection == TooltipDirection.auto && overlay != null) {
final estimatedTooltipSize = Size(
constraints.maxWidth.isFinite
? constraints.maxWidth
: overlay.size.width * 0.8,
constraints.maxHeight.isFinite
? constraints.maxHeight
: overlay.size.height * 0.4,
);
final screen = overlay.size;
final spaceAbove = target.dy - widget.minimumOutsideMargin;
final spaceBelow =
screen.height - target.dy - widget.minimumOutsideMargin;
final spaceLeft = target.dx - widget.minimumOutsideMargin;
final spaceRight = screen.width - target.dx - widget.minimumOutsideMargin;
if (spaceBelow >= estimatedTooltipSize.height) {
preferredDirection = TooltipDirection.down;
} else if (spaceAbove >= estimatedTooltipSize.height) {
preferredDirection = TooltipDirection.up;
} else if (spaceRight >= estimatedTooltipSize.width) {
preferredDirection = TooltipDirection.right;
} else if (spaceLeft >= estimatedTooltipSize.width) {
preferredDirection = TooltipDirection.left;
} else {
final candidates = <TooltipDirection, double>{
TooltipDirection.down: spaceBelow,
TooltipDirection.up: spaceAbove,
TooltipDirection.right: spaceRight,
TooltipDirection.left: spaceLeft,
};
preferredDirection =
candidates.entries.reduce((a, b) => a.value > b.value ? a : b).key;
}
}
if (widget.snapsFarAwayVertically) {
constraints = constraints.copyWith(maxHeight: null);
left = right = 0.0;
if (overlay != null) {
if (target.dy > overlay.size.center(Offset.zero).dy) {
preferredDirection = TooltipDirection.up;
top = 0.0;
} else {
preferredDirection = TooltipDirection.down;
bottom = 0.0;
}
} else {
// overlay is null - set default values
preferredDirection = TooltipDirection.down;
bottom = 0.0;
}
} else if (widget.snapsFarAwayHorizontally) {
constraints = constraints.copyWith(maxHeight: null);
top = bottom = 0.0;
if (overlay != null) {
if (target.dx < overlay.size.center(Offset.zero).dx) {
preferredDirection = TooltipDirection.right;
right = 0.0;
} else {
preferredDirection = TooltipDirection.left;
left = 0.0;
}
} else {
// overlay is null - set default values
preferredDirection = TooltipDirection.left;
left = 0.0;
}
}
_resolvedDirection = preferredDirection;
_barrierEntry = showBarrier
? OverlayEntry(
builder: (context) => FadeTransition(
opacity: animation,
child: GestureDetector(
onTap: widget.hideTooltipOnBarrierTap
? _superTooltipController!.hideTooltip
: null,
child: Container(
key: SuperTooltip.barrierKey,
decoration: ShapeDecoration(
shape: ShapeOverlay(
clipAreaCornerRadius: widget.touchThroughAreaCornerRadius,
clipAreaShape: widget.touchThroughAreaShape,
clipRect: widget.touchThroughArea,
barrierColor: barrierColor,
overlayDimensions: widget.overlayDimensions,
),
),
),
),
),
)
: null;
blur = showBlur
? OverlayEntry(
builder: (BuildContext context) => FadeTransition(
opacity: animation,
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: widget.sigmaX,
sigmaY: widget.sigmaY,
),
child: Container(
width: double.infinity,
height: double.infinity,
),
),
),
)
: null;
_entry = OverlayEntry(
builder: (BuildContext context) => IgnorePointer(
ignoring: widget.clickThrough,
child: FadeTransition(
opacity: animation,
child: Center(
child: CompositedTransformFollower(
link: _layerLink,
showWhenUnlinked: false,
offset: offsetToTarget,
child: CustomSingleChildLayout(
delegate: ToolTipPositionDelegate(
preferredDirection: _resolvedDirection,
constraints: constraints,
top: top,
bottom: bottom,
left: left,
right: right,
target: target,
// verticalOffset: widget.verticalOffset,
overlay: overlay,
margin: widget.minimumOutsideMargin,
snapsFarAwayHorizontally: widget.snapsFarAwayHorizontally,
snapsFarAwayVertically: widget.snapsFarAwayVertically,
),
// TD: Text fields and such will need a material ancestor
// In order to function properly. Need to find more elegant way
// to add this.
child: Stack(
fit: StackFit.passthrough,
children: <Widget>[
Material(
color: Colors.transparent,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
if (widget.hideTooltipOnTap) {
_superTooltipController!.hideTooltip();
}
},
child: Container(
key: SuperTooltip.bubbleKey,
margin: SuperUtils.getTooltipMargin(
arrowLength: widget.arrowLength,
arrowTipDistance: widget.arrowTipDistance,
closeButtonSize: closeButtonSize,
preferredDirection: _resolvedDirection,
closeButtonType: closeButtonType,
showCloseButton: showCloseButton,
),
padding: SuperUtils.getTooltipPadding(
closeButtonSize: closeButtonSize,
closeButtonType: closeButtonType,
showCloseButton: showCloseButton,
),
decoration: widget.decorationBuilder != null
? widget.decorationBuilder!(target)
: ShapeDecoration(
color: backgroundColor,
shadows: hasShadow
? widget.boxShadows ??
<BoxShadow>[
BoxShadow(
blurRadius: shadowBlurRadius,
spreadRadius: shadowSpreadRadius,
color: shadowColor,
offset: shadowOffset,
),
]
: null,
shape: BubbleShape(
arrowBaseWidth: widget.arrowBaseWidth,
arrowTipDistance: widget.arrowTipDistance,
arrowTipRadius: widget.arrowTipRadius,
borderColor: widget.borderColor,
borderRadius: widget.borderRadius,
borderWidth: widget.borderWidth,
bottom: bottom,
left: left,
preferredDirection: preferredDirection,
right: right,
target: target,
top: top,
bubbleDimensions: widget.bubbleDimensions,
),
),
child: widget.content,
),
),
),
_buildCloseButton(),
],
),
),
),
),
),
),
);
// ignore: unnecessary_null_comparison
if (overlayState != null) {
// Insert the overlay entries for the tooltip, barrier, and blur filter
// (if enabled).
overlayState.insertAll([
// Insert the blur filter overlay entry if enabled.
if (showBlur) blur!,
// Insert the barrier overlay entry if enabled.
if (showBarrier) _barrierEntry!,
// Insert the tooltip overlay entry.
_entry!,
]);
}
}
/// Shows the tooltip.
///
/// This method starts the fade-in animation and adds the overlay entries for
/// the tooltip, barrier, and blur filter (if enabled).
///
/// The [onShow] callback is called before the animation starts. After the
/// animation completes, the [SuperTooltipController.complete] method is called
/// to complete the show operation.
///
/// If the tooltip is already visible, this method does nothing.
Future<void> _showTooltip() async {
// Call the onShow callback before the animation starts.
widget.onShow?.call();
// Already visible.
if (_entry != null) return;
// Create the overlay entries for the tooltip, barrier, and blur filter.
_createOverlayEntries();
// Start the fade-in animation and wait for it to complete.
await _animationController
.forward()
.whenComplete(_superTooltipController!.complete);
}
/// Removes the overlay entries for the tooltip, barrier, and blur filter.
///
/// This function removes the overlay entries for the tooltip, barrier, and
/// blur filter. It sets the [_entry], [_barrierEntry], and [blur] variables to
/// `null` after the removal.
void _removeEntries() {
// Remove the tooltip overlay entry.
_entry?.remove();
_entry = null;
// Remove the barrier overlay entry.
_barrierEntry?.remove();
_barrierEntry = null;
// Remove the blur filter overlay entry.
blur?.remove();
blur = null;
}
/// Hides the tooltip.
///
/// This method starts the fade-out animation and removes the overlay entries
/// for the tooltip, barrier, and blur filter (if enabled).
///
/// The [onHide] callback is called before the animation starts. After the
/// animation completes, the [SuperTooltipController.complete] method is called
/// to complete the hide operation.
///
/// Finally, the method removes the overlay entries for the tooltip, barrier, and
/// blur filter.
Future<void> _hideTooltip() async {
// Call the onHide callback before the animation starts.
widget.onHide?.call();
// Start the fade-out animation and wait for it to complete.
await _animationController
.reverse()
.whenComplete(_superTooltipController!.complete);
// Remove the overlay entries for the tooltip, barrier, and blur filter.
_removeEntries();
}
/// Builds the close button widget based on the tooltip's configuration and
/// the current [TooltipDirection].
///
/// The position of the close button is calculated based on the
/// [closeButtonType] and the [TooltipDirection]. The close button is positioned
/// within the tooltip's content area.
///
/// Returns the close button widget wrapped in a [Positioned] widget.
Widget _buildCloseButton() {
// Return an empty widget if close button is not enabled.
if (!showCloseButton) {
return const SizedBox.shrink();
}
// Calculate the position of the close button based on the tooltip direction.
double right;
double top;
switch (_resolvedDirection) {
//
// LEFT: -------------------------------------
case TooltipDirection.left:
right = widget.arrowLength + widget.arrowTipDistance + 3.0;
if (closeButtonType == CloseButtonType.inside) {
// If the close button is inside the tooltip, position it at the top.
top = 2.0;
} else if (closeButtonType == CloseButtonType.outside) {
// If the close button is outside the tooltip, position it at the top.
top = 0.0;
} else {
throw AssertionError(closeButtonType);
}
break;
// RIGHT/UP: ---------------------------------
case TooltipDirection.right:
case TooltipDirection.up:
right = 5.0;
if (closeButtonType == CloseButtonType.inside) {
// If the close button is inside the tooltip, position it at the top.
top = 2.0;
} else if (closeButtonType == CloseButtonType.outside) {
// If the close button is outside the tooltip, position it at the top.
top = 0.0;
} else {
throw AssertionError(closeButtonType);
}
break;
// DOWN: -------------------------------------
case TooltipDirection.down:
right = 2.0;
if (closeButtonType == CloseButtonType.inside) {
// If the close button is inside the tooltip, position it below the arrow.
top = widget.arrowLength + widget.arrowTipDistance + 2.0;
} else if (closeButtonType == CloseButtonType.outside) {
// If the close button is outside the tooltip, position it at the top.