Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit d30d18e

Browse files
authored
dispose of the animation controller for ScrollablePositionedList when needed (#423)
1 parent 01ba4e2 commit d30d18e

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

packages/scrollable_positioned_list/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.3.4
2+
* Disposed the animation controller when disposing the scrollable list.
3+
14
# 0.3.3
25
* Fix potential crash when reading from RenderBox.size.
36

packages/scrollable_positioned_list/lib/src/scrollable_positioned_list.dart

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ class _ScrollablePositionedListState extends State<ScrollablePositionedList>
270270

271271
bool _isTransitioning = false;
272272

273+
var _animationController;
274+
273275
@override
274276
void initState() {
275277
super.initState();
@@ -297,6 +299,7 @@ class _ScrollablePositionedListState extends State<ScrollablePositionedList>
297299
.removeListener(_updatePositions);
298300
secondary.itemPositionsNotifier.itemPositions
299301
.removeListener(_updatePositions);
302+
_animationController?.dispose();
300303
super.dispose();
301304
}
302305

@@ -484,7 +487,11 @@ class _ScrollablePositionedListState extends State<ScrollablePositionedList>
484487
startAnimationCallback = () {
485488
SchedulerBinding.instance.addPostFrameCallback((_) {
486489
startAnimationCallback = () {};
487-
490+
_animationController?.dispose();
491+
_animationController =
492+
AnimationController(vsync: this, duration: duration)..forward();
493+
opacity.parent = _opacityAnimation(opacityAnimationWeights)
494+
.animate(_animationController);
488495
opacity.parent = _opacityAnimation(opacityAnimationWeights).animate(
489496
AnimationController(vsync: this, duration: duration)..forward());
490497
secondary.scrollController.jumpTo(-direction *
@@ -527,17 +534,19 @@ class _ScrollablePositionedListState extends State<ScrollablePositionedList>
527534
}
528535
}
529536

530-
setState(() {
531-
if (opacity.value >= 0.5) {
532-
// Secondary [ListView] is more visible than the primary; make it the
533-
// new primary.
534-
var temp = primary;
535-
primary = secondary;
536-
secondary = temp;
537-
}
538-
_isTransitioning = false;
539-
opacity.parent = const AlwaysStoppedAnimation<double>(0);
540-
});
537+
if (mounted) {
538+
setState(() {
539+
if (opacity.value >= 0.5) {
540+
// Secondary [ListView] is more visible than the primary; make it the
541+
// new primary.
542+
var temp = primary;
543+
primary = secondary;
544+
secondary = temp;
545+
}
546+
_isTransitioning = false;
547+
opacity.parent = const AlwaysStoppedAnimation<double>(0);
548+
});
549+
}
541550
}
542551

543552
Animatable<double> _opacityAnimation(List<double> opacityAnimationWeights) {

packages/scrollable_positioned_list/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: scrollable_positioned_list
2-
version: 0.3.3
2+
version: 0.3.4
33
description: >
44
A list with helper methods to programmatically scroll to an item.
55
homepage: https://github.com/google/flutter.widgets/tree/master/packages/scrollable_positioned_list

0 commit comments

Comments
 (0)