forked from SimformSolutionsPvtLtd/showcaseview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip_wrapper.dart
More file actions
314 lines (301 loc) · 11.7 KB
/
Copy pathtooltip_wrapper.dart
File metadata and controls
314 lines (301 loc) · 11.7 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
/*
* Copyright (c) 2021 Simform Solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
part of 'tooltip.dart';
class ToolTipWrapper extends StatefulWidget {
/// A tooltip widget that is displayed alongside a target widget during a
/// showcase.
///
/// The tooltip can display:
/// - Title and description text with customizable styling.
/// - A custom container widget instead of the default tooltip layout.
/// - Action buttons (inside or outside the tooltip).
/// - An optional arrow pointing to the target widget.
///
/// It supports various animations:
/// - Scale animation when the tooltip appears/disappears.
/// - Moving/bouncing animation while the tooltip is displayed.
///
/// The tooltip automatically handles positioning constraints to ensure it
/// stays within screen boundaries and maintains proper spacing from the
/// target widget.
const ToolTipWrapper({
required this.title,
required this.description,
required this.titleTextStyle,
required this.descTextStyle,
required this.container,
required this.tooltipBackgroundColor,
required this.textColor,
required this.showArrow,
required this.onTooltipTap,
required this.movingAnimationDuration,
required this.titleTextAlign,
required this.descriptionTextAlign,
required this.titleAlignment,
required this.descriptionAlignment,
required this.tooltipActionConfig,
required this.tooltipActions,
required this.targetPadding,
required this.disableMovingAnimation,
required this.disableScaleAnimation,
required this.tooltipBorderRadius,
required this.scaleAnimationDuration,
required this.scaleAnimationCurve,
required this.toolTipMargin,
required this.showcaseController,
required this.tooltipPadding,
required this.toolTipSlideEndDistance,
required this.targetTooltipGap,
this.semanticEnable = false,
this.scaleAnimationAlignment,
this.tooltipPosition,
this.titlePadding,
this.descriptionPadding,
this.titleTextDirection,
this.descriptionTextDirection,
super.key,
});
final String? title;
final TextAlign titleTextAlign;
final String? description;
final TextAlign descriptionTextAlign;
final AlignmentGeometry titleAlignment;
final AlignmentGeometry descriptionAlignment;
final TextStyle? titleTextStyle;
final TextStyle? descTextStyle;
final Widget? container;
final Color tooltipBackgroundColor;
final Color textColor;
final bool showArrow;
final VoidCallback? onTooltipTap;
final EdgeInsets tooltipPadding;
final Duration movingAnimationDuration;
final bool disableMovingAnimation;
final bool disableScaleAnimation;
final BorderRadius? tooltipBorderRadius;
final Duration scaleAnimationDuration;
final Curve scaleAnimationCurve;
final Alignment? scaleAnimationAlignment;
final TooltipPosition? tooltipPosition;
final EdgeInsets? titlePadding;
final EdgeInsets? descriptionPadding;
final TextDirection? titleTextDirection;
final TextDirection? descriptionTextDirection;
final double toolTipSlideEndDistance;
final double toolTipMargin;
final TooltipActionConfig tooltipActionConfig;
final List<Widget> tooltipActions;
final EdgeInsets targetPadding;
final ShowcaseController showcaseController;
final double targetTooltipGap;
final bool semanticEnable;
@override
State<ToolTipWrapper> createState() => _ToolTipWrapperState();
}
class _ToolTipWrapperState extends State<ToolTipWrapper>
with TickerProviderStateMixin {
late final AnimationController _movingAnimationController =
AnimationController(
duration: widget.movingAnimationDuration,
vsync: this,
);
late final Animation<double> _movingAnimation = CurvedAnimation(
parent: _movingAnimationController,
curve: Curves.easeInOut,
);
late final AnimationController _scaleAnimationController =
AnimationController(
duration: widget.scaleAnimationDuration,
vsync: this,
lowerBound: widget.disableScaleAnimation ? 1 : 0,
);
late final Animation<double> _scaleAnimation = CurvedAnimation(
parent: _scaleAnimationController,
curve: widget.scaleAnimationCurve,
);
@override
void initState() {
super.initState();
if (widget.disableScaleAnimation) {
movingAnimationListener();
} else {
_scaleAnimationController
..addStatusListener((scaleAnimationStatus) {
if (scaleAnimationStatus != AnimationStatus.completed) return;
movingAnimationListener();
})
..forward();
}
if (!widget.disableMovingAnimation) _movingAnimationController.forward();
widget.showcaseController.reverseAnimationCallback =
widget.disableScaleAnimation ? null : _scaleAnimationController.reverse;
}
@override
Widget build(BuildContext context) {
assert(
widget.container != null ||
(widget.title != null || widget.description != null),
'Provide either a custom container or a title/description for the '
'tooltip. Both cannot be null.',
);
// Calculate the target position and size
final box = widget.showcaseController.position?.renderBox;
// This is a workaround to avoid the error when the widget is not mounted
// but won't happen in general cases
if (box == null || !box.attached) return const SizedBox.shrink();
final targetPosition = box.localToGlobal(Offset.zero);
final targetSize = box.size;
final defaultToolTipWidget = widget.container != null
? MouseRegion(
opaque: false,
cursor: widget.onTooltipTap == null
? MouseCursor.defer
: SystemMouseCursors.click,
child: GestureDetector(
onTap: widget.onTooltipTap,
child: RepaintBoundary(
child: widget.container ?? const SizedBox.shrink(),
),
),
)
: MouseRegion(
opaque: false,
cursor: widget.onTooltipTap == null
? MouseCursor.defer
: SystemMouseCursors.click,
child: GestureDetector(
onTap: widget.onTooltipTap,
child: RepaintBoundary(
child: Container(
padding: widget.tooltipPadding,
decoration: BoxDecoration(
color: widget.tooltipBackgroundColor,
borderRadius: widget.tooltipBorderRadius ??
const BorderRadius.all(Radius.circular(8)),
),
child: ToolTipContent(
title: widget.title,
description: widget.description,
titleTextAlign: widget.titleTextAlign,
descriptionTextAlign: widget.descriptionTextAlign,
titleAlignment: widget.titleAlignment,
descriptionAlignment: widget.descriptionAlignment,
textColor: widget.textColor,
titleTextStyle: widget.titleTextStyle,
descTextStyle: widget.descTextStyle,
titlePadding: widget.titlePadding,
descriptionPadding: widget.descriptionPadding,
titleTextDirection: widget.titleTextDirection,
descriptionTextDirection: widget.descriptionTextDirection,
tooltipActionConfig: widget.tooltipActionConfig,
tooltipActions: widget.tooltipActions,
),
),
),
),
);
return Material(
type: MaterialType.transparency,
child: _AnimatedTooltipMultiLayout(
scaleController: _scaleAnimationController,
moveController: _movingAnimationController,
scaleAnimation: _scaleAnimation,
moveAnimation: _movingAnimation,
targetPosition: targetPosition,
targetSize: targetSize,
position: widget.tooltipPosition,
screenSize: widget.showcaseController.rootWidgetSize ??
MediaQuery.sizeOf(context),
hasArrow: widget.showArrow,
targetPadding: widget.targetPadding,
scaleAlignment: widget.scaleAnimationAlignment,
hasSecondBox: widget.tooltipActions.isNotEmpty &&
(widget.tooltipActionConfig.position.isOutside ||
widget.container != null),
toolTipSlideEndDistance: widget.toolTipSlideEndDistance,
gapBetweenContentAndAction:
widget.tooltipActionConfig.gapBetweenContentAndAction,
screenEdgePadding: widget.toolTipMargin,
showcaseOffset: widget.showcaseController.rootRenderObject
?.localToGlobal(Offset.zero) ??
Offset.zero,
targetTooltipGap: widget.targetTooltipGap,
children: [
// We have to use UniqueKey here to avoid the issue with the
// _TooltipLayoutId being reused and causing layout issues
// See: documentation of [MultiChildRenderObjectWidget] for more
// details and to reproduce issue navigate to details screen in
// example app with route transition
_TooltipLayoutId(
id: TooltipLayoutSlot.tooltipBox,
key: UniqueKey(),
child: !widget.semanticEnable
? defaultToolTipWidget
: Semantics(
liveRegion: true,
child: defaultToolTipWidget,
),
),
if (widget.tooltipActions.isNotEmpty &&
(widget.tooltipActionConfig.position.isOutside ||
widget.container != null))
_TooltipLayoutId(
id: TooltipLayoutSlot.actionBox,
key: UniqueKey(),
child: ActionWidget(
tooltipActionConfig: widget.tooltipActionConfig,
children: widget.tooltipActions,
),
),
if (widget.showArrow)
_TooltipLayoutId(
id: TooltipLayoutSlot.arrow,
key: UniqueKey(),
child: ShowcaseArrow(
strokeColor: widget.tooltipBackgroundColor,
),
),
],
),
);
}
@override
void dispose() {
_movingAnimationController.dispose();
_scaleAnimationController.dispose();
super.dispose();
}
void movingAnimationListener() {
// We have added check at the call of the this function but still this
// will be our last defence
if (widget.disableMovingAnimation) return;
_movingAnimationController.addStatusListener((status) {
if (status == AnimationStatus.completed) {
_movingAnimationController.reverse();
}
if (_movingAnimationController.isDismissed &&
!widget.disableMovingAnimation) {
_movingAnimationController.forward();
}
});
}
}