Skip to content

Commit

Permalink
adds safeSetState
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbarbosadrip committed Aug 3, 2022
1 parent 1f674c1 commit c6c0186
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
25 changes: 9 additions & 16 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -73,7 +73,7 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.1.4"
meta:
dependency: transitive
description:
Expand All @@ -87,7 +87,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -99,7 +99,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -134,27 +134,20 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
version: "0.4.9"
tutorial_coach_mark:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.2.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.2.3"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
version: "2.1.2"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
9 changes: 9 additions & 0 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ abstract class TutorialCoachMarkController {
void previous();
void skip();
}

extension StateExt on State {
void safeSetState(VoidCallback call) {
if (mounted) {
// ignore: invalid_use_of_protected_member
setState(call);
}
}
}
25 changes: 14 additions & 11 deletions lib/src/widgets/animated_focus_light.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ abstract class AnimatedFocusLightState extends State<AnimatedFocusLight>
}

void _finish() {
setState(() => _currentFocus = 0);
safeSetState(() => _currentFocus = 0);
widget.finish!();
}

Expand Down Expand Up @@ -237,7 +237,7 @@ class AnimatedStaticFocusLightState extends AnimatedFocusLightState {
targetTap: targetTap,
overlayTap: overlayTap,
);
setState(() => _goNext = goNext);
safeSetState(() => _goNext = goNext);
_controller.reverse();
}

Expand All @@ -262,7 +262,7 @@ class AnimatedStaticFocusLightState extends AnimatedFocusLightState {
return;
}

setState(() {
safeSetState(() {
_targetPosition = targetPosition;

_positioned = Offset(
Expand Down Expand Up @@ -405,7 +405,7 @@ class AnimatedPulseFocusLightState extends AnimatedFocusLightState {
return;
}

setState(() {
safeSetState(() {
_finishFocus = false;
_targetPosition = targetPosition;

Expand Down Expand Up @@ -439,10 +439,13 @@ class AnimatedPulseFocusLightState extends AnimatedFocusLightState {
targetTap: targetTap,
overlayTap: overlayTap,
);
setState(() {
_goNext = goNext;
_initReverse = true;
});
if (mounted) {
safeSetState(() {
_goNext = goNext;
_initReverse = true;
});
}

_controllerPulse.reverse(from: _controllerPulse.value);
}

Expand All @@ -460,14 +463,14 @@ class AnimatedPulseFocusLightState extends AnimatedFocusLightState {
@override
void _listener(AnimationStatus status) {
if (status == AnimationStatus.completed) {
setState(() => _finishFocus = true);
safeSetState(() => _finishFocus = true);

widget.focus?.call(_targetFocus);

_controllerPulse.forward();
}
if (status == AnimationStatus.dismissed) {
setState(() {
safeSetState(() {
_finishFocus = false;
_initReverse = false;
});
Expand All @@ -490,7 +493,7 @@ class AnimatedPulseFocusLightState extends AnimatedFocusLightState {

if (status == AnimationStatus.dismissed) {
if (_initReverse) {
setState(() => _finishFocus = false);
safeSetState(() => _finishFocus = false);
_controller.reverse();
} else if (_finishFocus) {
_controllerPulse.forward();
Expand Down
23 changes: 8 additions & 15 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -73,7 +73,7 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.1.4"
meta:
dependency: transitive
description:
Expand All @@ -87,7 +87,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -99,7 +99,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -134,20 +134,13 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.9"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
version: "2.1.2"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"

0 comments on commit c6c0186

Please sign in to comment.