Skip to content

Commit 387b927

Browse files
authored
Merge pull request #153 from bensonarafat/develop2
Develop2
2 parents 5f7237a + c14f507 commit 387b927

File tree

5 files changed

+61
-44
lines changed

5 files changed

+61
-44
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ Create a callback function to dismiss
215215
}
216216
```
217217

218+
## MouseRegion on Desktop & Web
219+
Based on issue [#80](https://github.com/bensonarafat/super_tooltip/issues/80) Supertooltip now support MouseEnter and Exit for Desktop for Web
220+
### How to use
221+
```dart
222+
SuperTooltip(
223+
// ... other parameters
224+
showOnHover: true, // Tooltip appears on hover
225+
hideOnHoverExit: true, // Tooltip disappears when mouse leaves
226+
content: Text("Hello World"),
227+
child: Icon(Icons.info),
228+
);
229+
230+
```
231+
<b>Demo</b>
232+
<p>
233+
<img src="https://private-user-images.githubusercontent.com/74812392/518725925-f425c7b1-fd4b-48ee-9a1b-079fe357f269.gif?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjU2MjY0MzEsIm5iZiI6MTc2NTYyNjEzMSwicGF0aCI6Ii83NDgxMjM5Mi81MTg3MjU5MjUtZjQyNWM3YjEtZmQ0Yi00OGVlLTlhMWItMDc5ZmUzNTdmMjY5LmdpZj9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTEyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUxMjEzVDExNDIxMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRlMjU1OWI5ODAwMjJhY2I4MjkyNzIzMGQwYzA4YjBkYTc5YjZkNDU3YTFhZDA0YzY3NDM1ZjE4ZjdhNWYyZTgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.GWaIPrIgvwidohapOkdY3TMQsqOgowtxT33Y31zVtlw" width="200"/>
234+
</p>
235+
Thanks [@akhil-ge0rge](https://github.com/akhil-ge0rge)
236+
218237
## Example app
219238

220239
Find the example app [here](https://github.com/bensonarafat/super_tooltip/tree/master/example).

android/local.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
sdk.dir=/Users/bensonarafat/Library/Android/sdk
2-
flutter.sdk=/Users/bensonarafat/development/flutter
2+
flutter.sdk=/Users/bensonarafat/development/flutter

example/analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
# No lint rules for example

lib/src/tooltip_position_delegate.dart

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ class ToolTipPositionDelegate extends SingleChildLayoutDelegate {
3434

3535
@override
3636
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
37-
// TD: when margin is EdgeInsets, look into
38-
// constraints.deflate(margin);
39-
var newConstraints = this.constraints;
37+
// We use the INCOMING constraints (screen size) to calculate available space.
38+
// We do NOT start with this.constraints (user prefs) because that leads to negative math.
39+
var availableConstraints = constraints;
4040

4141
switch (preferredDirection) {
4242
case TooltipDirection.up:
4343
case TooltipDirection.down:
44-
newConstraints = SuperUtils.verticalConstraints(
45-
constraints: newConstraints,
44+
availableConstraints = SuperUtils.verticalConstraints(
45+
constraints: availableConstraints,
4646
margin: margin,
4747
bottom: bottom,
4848
isUp: preferredDirection == TooltipDirection.up,
@@ -54,8 +54,8 @@ class ToolTipPositionDelegate extends SingleChildLayoutDelegate {
5454
break;
5555
case TooltipDirection.right:
5656
case TooltipDirection.left:
57-
newConstraints = SuperUtils.horizontalConstraints(
58-
constraints: newConstraints,
57+
availableConstraints = SuperUtils.horizontalConstraints(
58+
constraints: availableConstraints,
5959
margin: margin,
6060
bottom: bottom,
6161
isRight: preferredDirection == TooltipDirection.right,
@@ -67,40 +67,31 @@ class ToolTipPositionDelegate extends SingleChildLayoutDelegate {
6767
break;
6868
}
6969

70-
// TD: This scenerio should likely be avoided in the initial functions
71-
// Ensure constraints are valid - no negiative values
72-
final validatedConstraints = newConstraints.copyWith(
73-
minHeight: math.max(
74-
0,
75-
newConstraints.minHeight > newConstraints.maxHeight
76-
? newConstraints.maxHeight
77-
: newConstraints.minHeight),
78-
minWidth: math.max(
79-
0,
80-
newConstraints.minWidth > newConstraints.maxWidth
81-
? newConstraints.maxWidth
82-
: newConstraints.minWidth),
83-
maxHeight: math.max(0, newConstraints.maxHeight),
84-
maxWidth: math.max(0, newConstraints.maxWidth),
70+
// Now we merge the calculated "Available Space" with the User's "Desired Constraints".
71+
// We take the smaller of the two max widths/heights to ensure we fit in both.
72+
// We respect the user's min sizes unless they exceed available space.
73+
74+
double finalMaxWidth =
75+
math.min(availableConstraints.maxWidth, this.constraints.maxWidth);
76+
double finalMaxHeight =
77+
math.min(availableConstraints.maxHeight, this.constraints.maxHeight);
78+
79+
// Ensure final max is not negative
80+
finalMaxWidth = math.max(0.0, finalMaxWidth);
81+
finalMaxHeight = math.max(0.0, finalMaxHeight);
82+
83+
final validatedConstraints = BoxConstraints(
84+
minWidth: math.min(this.constraints.minWidth, finalMaxWidth),
85+
maxWidth: finalMaxWidth,
86+
minHeight: math.min(this.constraints.minHeight, finalMaxHeight),
87+
maxHeight: finalMaxHeight,
8588
);
8689

8790
return validatedConstraints;
8891
}
8992

9093
@override
9194
Offset getPositionForChild(Size size, Size childSize) {
92-
// TD: If there isn't enough space for the child on the preferredDirection
93-
// use the opposite dirrection
94-
//
95-
// See:
96-
// return positionDependentBox(
97-
// size: size,
98-
// childSize: childSize,
99-
// target: target,
100-
// verticalOffset: verticalOffset,
101-
// preferBelow: preferBelow,
102-
// );
103-
10495
switch (preferredDirection) {
10596
case TooltipDirection.up:
10697
case TooltipDirection.down:

lib/src/utils.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,25 @@ class SuperUtils {
153153

154154
if (isRight) {
155155
if (right != null) {
156-
minWidth = maxWidth = maxWidth - right - target.dx;
156+
// Calculate available width based on margin
157+
maxWidth = maxWidth - right - target.dx;
158+
// Don't force minWidth to be equal to maxWidth (allows shrinking)
157159
} else {
158-
maxWidth = min(maxWidth, maxWidth - target.dx) - margin;
160+
maxWidth = (maxWidth - target.dx) - margin;
159161
}
160162
} else {
161163
if (left != null) {
162-
minWidth = maxWidth = target.dx - left;
164+
// Calculate available width based on margin
165+
maxWidth = target.dx - left;
163166
} else {
164167
maxWidth = min(maxWidth, target.dx) - margin;
165168
}
166169
}
167170

171+
// Robustness check to prevent negative constraints
172+
if (maxWidth < 0) maxWidth = 0;
173+
if (minWidth > maxWidth) minWidth = maxWidth;
174+
168175
return constraints.copyWith(
169176
maxHeight: maxHeight,
170177
minWidth: minWidth,
@@ -190,7 +197,6 @@ class SuperUtils {
190197
maxWidth = maxWidth - (left + right);
191198
} else if ((left != null && right == null) ||
192199
(left == null && right != null)) {
193-
// make sure that the sum of left, right + maxwidth isn't bigger than the screen width.
194200
final sideDelta = (left ?? 0.0) + (right ?? 0.0) + margin;
195201

196202
if (maxWidth > maxWidth - sideDelta) {
@@ -204,20 +210,22 @@ class SuperUtils {
204210

205211
if (isUp) {
206212
if (top != null) {
207-
minHeight = maxHeight = target.dy - top;
213+
maxHeight = target.dy - top;
208214
} else {
209215
maxHeight = min(maxHeight, target.dy) - margin;
210-
// TD: clamp minheight
211216
}
212217
} else {
213218
if (bottom != null) {
214-
minHeight = maxHeight = maxHeight - bottom - target.dy;
219+
maxHeight = maxHeight - bottom - target.dy;
215220
} else {
216221
maxHeight = min(maxHeight, maxHeight - target.dy) - margin;
217-
// TD: clamp minheight
218222
}
219223
}
220224

225+
// Robustness check
226+
if (maxHeight < 0) maxHeight = 0;
227+
if (minHeight > maxHeight) minHeight = maxHeight;
228+
221229
return constraints.copyWith(
222230
minHeight: minHeight,
223231
maxHeight: maxHeight,

0 commit comments

Comments
 (0)