diff --git a/lib/src/indicator/material_indicator.dart b/lib/src/indicator/material_indicator.dart index 645efb77..792fd8a7 100644 --- a/lib/src/indicator/material_indicator.dart +++ b/lib/src/indicator/material_indicator.dart @@ -64,6 +64,7 @@ class _MaterialClassicHeaderState AnimationController _scaleFactor; AnimationController _positionController; AnimationController _valueAni; + ScrollPosition _position; @override void initState() { @@ -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 = @@ -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 @@ -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 ?? diff --git a/lib/src/internals/indicator_wrap.dart b/lib/src/internals/indicator_wrap.dart index a7973f44..87b82119 100644 --- a/lib/src/internals/indicator_wrap.dart +++ b/lib/src/internals/indicator_wrap.dart @@ -142,7 +142,7 @@ abstract class RefreshIndicatorState ? (mode == RefreshStatus.twoLeveling || mode == RefreshStatus.twoLevelOpening || mode == RefreshStatus.twoLevelClosing - ? SmartRefresher.ofState(context).viewportExtent + ? refresherState.viewportExtent : widget.height) : 0.0) - _position?.pixels; @@ -241,14 +241,14 @@ abstract class RefreshIndicatorState 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(); @@ -287,7 +287,7 @@ abstract class RefreshIndicatorState 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(); @@ -302,10 +302,10 @@ abstract class RefreshIndicatorState }); } 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); @@ -342,7 +342,7 @@ abstract class RefreshIndicatorState refreshIndicatorLayoutExtent: mode == RefreshStatus.twoLeveling || mode == RefreshStatus.twoLevelOpening || mode == RefreshStatus.twoLevelClosing - ? SmartRefresher.ofState(context).viewportExtent + ? refresherState.viewportExtent : widget.height, refreshStyle: widget.refreshStyle); } @@ -578,6 +578,8 @@ abstract class LoadIndicatorState extends State mixin IndicatorStateMixin on State { SmartRefresher refresher; + SmartRefresherState refresherState; + RefreshConfiguration configuration; bool _floating = false; @@ -627,6 +629,7 @@ mixin IndicatorStateMixin on State { void _updateListener() { configuration = RefreshConfiguration.of(context); refresher = SmartRefresher.of(context); + refresherState = SmartRefresher.ofState(context); ValueNotifier newMode = V == RefreshStatus ? refresher.controller.headerMode : refresher.controller.footerMode;