Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null safety & mounted fix #405

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/src/layout_overlays.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ class _OverlayBuilderState extends State<OverlayBuilder> {
super.initState();

if (widget.showOverlay) {
ambiguate(WidgetsBinding.instance)
?.addPostFrameCallback((_) => showOverlay());
ambiguate(WidgetsBinding.instance)?.addPostFrameCallback(
(_) => showOverlay(),
);
}
}

Expand Down Expand Up @@ -163,7 +164,7 @@ class _OverlayBuilderState extends State<OverlayBuilder> {
_overlayEntry = OverlayEntry(
builder: widget.overlayBuilder!,
);
addToOverlay(_overlayEntry!);
if (mounted) addToOverlay(_overlayEntry!);
} else {
// Rebuild overlay.
buildOverlay();
Expand Down Expand Up @@ -192,7 +193,7 @@ class _OverlayBuilderState extends State<OverlayBuilder> {
if (isShowingOverlay() && !widget.showOverlay) {
hideOverlay();
} else if (!isShowingOverlay() && widget.showOverlay) {
showOverlay();
if (mounted) showOverlay();
}
}

Expand Down
86 changes: 51 additions & 35 deletions lib/src/showcase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,18 @@ class _ShowcaseState extends State<Showcase> {
Timer? timer;
GetPosition? position;

ShowCaseWidgetState get showCaseWidgetState => ShowCaseWidget.of(context);
ShowCaseWidgetState? get showCaseWidgetState {
try {
return ShowCaseWidget.of(context);
} catch (_) {
return null;
}
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
_enableShowcase = showCaseWidgetState.enableShowcase;
_enableShowcase = showCaseWidgetState?.enableShowcase ?? false;

if (_enableShowcase) {
position ??= GetPosition(
Expand All @@ -385,28 +391,34 @@ class _ShowcaseState extends State<Showcase> {
_showShowCase = activeStep == widget.key;
});

if (activeStep == widget.key) {
if (showCaseWidgetState.enableAutoScroll) {
if (activeStep == widget.key && mounted) {
if (showCaseWidgetState?.enableAutoScroll ?? false) {
_scrollIntoView();
}

if (showCaseWidgetState.autoPlay) {
if (showCaseWidgetState?.autoPlay ?? false) {
timer = Timer(
Duration(seconds: showCaseWidgetState.autoPlayDelay.inSeconds),
_nextIfAny);
Duration(
seconds: showCaseWidgetState?.autoPlayDelay.inSeconds ?? 0,
),
_nextIfAny,
);
}
}
}

void _scrollIntoView() {
ambiguate(WidgetsBinding.instance)?.addPostFrameCallback((timeStamp) async {
setState(() => _isScrollRunning = true);
await Scrollable.ensureVisible(
widget.key.currentContext!,
duration: showCaseWidgetState.widget.scrollDuration,
alignment: 0.5,
);
setState(() => _isScrollRunning = false);
if (mounted) {
setState(() => _isScrollRunning = true);
if (showCaseWidgetState == null) return;
await Scrollable.ensureVisible(
widget.key.currentContext!,
duration: showCaseWidgetState?.widget.scrollDuration ?? Duration.zero,
alignment: 0.5,
);
if (mounted) setState(() => _isScrollRunning = false);
}
});
}

Expand All @@ -432,40 +444,42 @@ class _ShowcaseState extends State<Showcase> {
}

Future<void> _nextIfAny() async {
if (timer != null && timer!.isActive) {
if (showCaseWidgetState.enableAutoPlayLock) {
return;
if (mounted) {
if (timer != null && timer!.isActive) {
if (showCaseWidgetState?.enableAutoPlayLock ?? false) {
return;
}
timer!.cancel();
} else if (timer != null && !timer!.isActive) {
timer = null;
}
timer!.cancel();
} else if (timer != null && !timer!.isActive) {
timer = null;
await _reverseAnimateTooltip();
if (mounted) showCaseWidgetState?.completed(widget.key);
}
await _reverseAnimateTooltip();
showCaseWidgetState.completed(widget.key);
}

Future<void> _getOnTargetTap() async {
if (widget.disposeOnTap == true) {
if (widget.disposeOnTap == true && mounted) {
await _reverseAnimateTooltip();
showCaseWidgetState.dismiss();
showCaseWidgetState?.dismiss();
widget.onTargetClick!();
} else {
} else if (mounted) {
(widget.onTargetClick ?? _nextIfAny).call();
}
}

Future<void> _getOnTooltipTap() async {
if (widget.disposeOnTap == true) {
if (widget.disposeOnTap == true && mounted) {
await _reverseAnimateTooltip();
showCaseWidgetState.dismiss();
showCaseWidgetState?.dismiss();
}
widget.onToolTipClick?.call();
if (mounted) widget.onToolTipClick?.call();
}

/// Reverse animates the provided tooltip or
/// the custom container widget.
Future<void> _reverseAnimateTooltip() async {
setState(() => _isTooltipDismissed = true);
if (mounted) setState(() => _isTooltipDismissed = true);
await Future<dynamic>.delayed(widget.scaleAnimationDuration);
_isTooltipDismissed = false;
}
Expand All @@ -477,21 +491,21 @@ class _ShowcaseState extends State<Showcase> {
Size screenSize,
) {
var blur = 0.0;
if (_showShowCase) {
blur = widget.blurValue ?? showCaseWidgetState.blurValue;
if (_showShowCase && mounted) {
blur = widget.blurValue ?? showCaseWidgetState?.blurValue ?? 0.0;
}

// Set blur to 0 if application is running on web and
// provided blur is less than 0.
blur = kIsWeb && blur < 0 ? 0 : blur;

if (!_showShowCase) return const Offstage();
if (!_showShowCase || !mounted) return const Offstage();

return Stack(
children: [
GestureDetector(
onTap: () {
if (!showCaseWidgetState.disableBarrierInteraction) {
if (!(showCaseWidgetState?.disableBarrierInteraction ?? false)) {
_nextIfAny();
}
widget.onBarrierClick?.call();
Expand Down Expand Up @@ -559,9 +573,11 @@ class _ShowcaseState extends State<Showcase> {
onTooltipTap: _getOnTooltipTap,
tooltipPadding: widget.tooltipPadding,
disableMovingAnimation: widget.disableMovingAnimation ??
showCaseWidgetState.disableMovingAnimation,
showCaseWidgetState?.disableMovingAnimation ??
false,
disableScaleAnimation: widget.disableScaleAnimation ??
showCaseWidgetState.disableScaleAnimation,
showCaseWidgetState?.disableScaleAnimation ??
false,
movingAnimationDuration: widget.movingAnimationDuration,
tooltipBorderRadius: widget.tooltipBorderRadius,
scaleAnimationDuration: widget.scaleAnimationDuration,
Expand Down
Loading