Skip to content

Commit

Permalink
Merge pull request #66 from bensonarafat/features
Browse files Browse the repository at this point in the history
fix: Overlay.of(context) nullable
  • Loading branch information
bensonarafat authored Sep 3, 2023
2 parents 90f10f4 + 6b2c60f commit 7eba7aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This will add a line like this to your package's pubspec.yaml (and run an implic

```
dependencies:
super_tooltip: ^2.0.4
super_tooltip: ^2.0.5
```

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Expand Down
48 changes: 35 additions & 13 deletions lib/src/super_tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,14 @@ class _SuperTooltipState extends State<SuperTooltip>

void _createOverlayEntries() {
final renderBox = context.findRenderObject() as RenderBox;
final overlay =
Overlay.of(context).context.findRenderObject() as RenderBox?;

final overlayState = Overlay.of(context);
RenderBox? overlay;

// ignore: unnecessary_null_comparison
if (overlayState != null) {
overlay = overlayState.context.findRenderObject() as RenderBox?;
}

final size = renderBox.size;
final target = renderBox.localToGlobal(size.center(Offset.zero));
Expand All @@ -238,21 +244,33 @@ class _SuperTooltipState extends State<SuperTooltip>
constraints = constraints.copyWith(maxHeight: null);
left = right = 0.0;

if (target.dy > overlay!.size.center(Offset.zero).dy) {
preferredDirection = TooltipDirection.up;
top = 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 (target.dx < overlay!.size.center(Offset.zero).dx) {
preferredDirection = TooltipDirection.right;
right = 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;
}
Expand Down Expand Up @@ -380,11 +398,14 @@ class _SuperTooltipState extends State<SuperTooltip>
),
);

Overlay.of(context).insertAll([
if (showBlur) blur!,
if (showBarrier) _barrierEntry!,
_entry!,
]);
// ignore: unnecessary_null_comparison
if (overlayState != null) {
overlayState.insertAll([
if (showBlur) blur!,
if (showBarrier) _barrierEntry!,
_entry!,
]);
}
}

_showTooltip() async {
Expand All @@ -405,6 +426,7 @@ class _SuperTooltipState extends State<SuperTooltip>
_entry = null;
_barrierEntry?.remove();
_entry = null;
blur?.remove();
}

_hideTooltip() async {
Expand Down

0 comments on commit 7eba7aa

Please sign in to comment.