Skip to content

Commit

Permalink
fix crash "Looking up a deactivated widget's ancestor is unsafe"
Browse files Browse the repository at this point in the history
  • Loading branch information
peng8350 committed Apr 10, 2021
1 parent c14c826 commit de451dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 12 additions & 1 deletion lib/src/indicator/material_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class _MaterialClassicHeaderState
AnimationController _scaleFactor;
AnimationController _positionController;
AnimationController _valueAni;
ScrollPosition _position;

@override
void initState() {
Expand All @@ -76,7 +77,7 @@ class _MaterialClassicHeaderState
duration: Duration(milliseconds: 500));
_valueAni.addListener(() {
// frequently setState will decline the performance
if (mounted && Scrollable.of(context).position.pixels <= 0)
if (mounted && _position.pixels <= 0)
setState(() {});
});
_positionController =
Expand All @@ -92,6 +93,15 @@ class _MaterialClassicHeaderState
super.initState();
}

@override
void didUpdateWidget(covariant MaterialClassicHeader oldWidget) {
// TODO: implement didUpdateWidget
_position = Scrollable.of(context).position;
super.didUpdateWidget(oldWidget);
}



@override
Widget buildContent(BuildContext context, RefreshStatus mode) {
// TODO: implement buildContent
Expand Down Expand Up @@ -150,6 +160,7 @@ class _MaterialClassicHeaderState
@override
void didChangeDependencies() {
final ThemeData theme = Theme.of(context);
_position = Scrollable.of(context).position;
_valueColor = _positionController.drive(
ColorTween(
begin: (widget.color ??
Expand Down
17 changes: 10 additions & 7 deletions lib/src/internals/indicator_wrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ abstract class RefreshIndicatorState<T extends RefreshIndicator>
? (mode == RefreshStatus.twoLeveling ||
mode == RefreshStatus.twoLevelOpening ||
mode == RefreshStatus.twoLevelClosing
? SmartRefresher.ofState(context).viewportExtent
? refresherState.viewportExtent
: widget.height)
: 0.0) -
_position?.pixels;
Expand Down Expand Up @@ -241,14 +241,14 @@ abstract class RefreshIndicatorState<T extends RefreshIndicator>
resetValue();

if (mode == RefreshStatus.idle)
SmartRefresher.ofState(context).setCanDrag(true);
refresherState.setCanDrag(true);
}
if (mode == RefreshStatus.completed || mode == RefreshStatus.failed) {
endRefresh().then((_) {
if (!mounted) return;
floating = false;
if (mode == RefreshStatus.completed || mode == RefreshStatus.failed) {
SmartRefresher.ofState(context)
refresherState
.setCanDrag(configuration.enableScrollWhenRefreshCompleted);
}
update();
Expand Down Expand Up @@ -287,7 +287,7 @@ abstract class RefreshIndicatorState<T extends RefreshIndicator>
if (refresher.onRefresh != null) refresher.onRefresh();
} else if (mode == RefreshStatus.twoLevelOpening) {
floating = true;
SmartRefresher.ofState(context).setCanDrag(false);
refresherState.setCanDrag(false);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
activity.resetActivity();
Expand All @@ -302,10 +302,10 @@ abstract class RefreshIndicatorState<T extends RefreshIndicator>
});
} else if (mode == RefreshStatus.twoLevelClosing) {
floating = false;
SmartRefresher.ofState(context).setCanDrag(false);
refresherState.setCanDrag(false);
update();
} else if (mode == RefreshStatus.twoLeveling) {
SmartRefresher.ofState(context)
refresherState
.setCanDrag(configuration.enableScrollWhenTwoLevel);
}
onModeChange(mode);
Expand Down Expand Up @@ -342,7 +342,7 @@ abstract class RefreshIndicatorState<T extends RefreshIndicator>
refreshIndicatorLayoutExtent: mode == RefreshStatus.twoLeveling ||
mode == RefreshStatus.twoLevelOpening ||
mode == RefreshStatus.twoLevelClosing
? SmartRefresher.ofState(context).viewportExtent
? refresherState.viewportExtent
: widget.height,
refreshStyle: widget.refreshStyle);
}
Expand Down Expand Up @@ -578,6 +578,8 @@ abstract class LoadIndicatorState<T extends LoadIndicator> extends State<T>
mixin IndicatorStateMixin<T extends StatefulWidget, V> on State<T> {
SmartRefresher refresher;

SmartRefresherState refresherState;

RefreshConfiguration configuration;

bool _floating = false;
Expand Down Expand Up @@ -627,6 +629,7 @@ mixin IndicatorStateMixin<T extends StatefulWidget, V> on State<T> {
void _updateListener() {
configuration = RefreshConfiguration.of(context);
refresher = SmartRefresher.of(context);
refresherState = SmartRefresher.ofState(context);
ValueNotifier<V> newMode = V == RefreshStatus
? refresher.controller.headerMode
: refresher.controller.footerMode;
Expand Down

0 comments on commit de451dc

Please sign in to comment.