_DefaultTabControllerState should dispose all created TabContoller instances. (#136608)

This commit is contained in:
Kostia Sokolovskyi 2023-10-19 19:38:19 +02:00 committed by GitHub
parent 0abb017cfd
commit 9fa9fd365c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 165 additions and 154 deletions

View file

@ -132,8 +132,11 @@ class TabController extends ChangeNotifier {
}) : _index = index, }) : _index = index,
_previousIndex = previousIndex, _previousIndex = previousIndex,
_animationController = animationController, _animationController = animationController,
_animationDuration = animationDuration; _animationDuration = animationDuration {
if (kFlutterMemoryAllocationsEnabled) {
ChangeNotifier.maybeDispatchObjectCreation(this);
}
}
/// Creates a new [TabController] with `index`, `previousIndex`, `length`, and /// Creates a new [TabController] with `index`, `previousIndex`, `length`, and
/// `animationDuration` if they are non-null. /// `animationDuration` if they are non-null.
@ -142,7 +145,10 @@ class TabController extends ChangeNotifier {
/// ///
/// When [DefaultTabController.length] is updated, this method is called to /// When [DefaultTabController.length] is updated, this method is called to
/// create a new [TabController] without creating a new [AnimationController]. /// create a new [TabController] without creating a new [AnimationController].
TabController _copyWith({ ///
/// This instance of [TabController] will be disposed and must not be used
/// anymore.
TabController _copyWithAndDispose({
required int? index, required int? index,
required int? length, required int? length,
required int? previousIndex, required int? previousIndex,
@ -151,13 +157,20 @@ class TabController extends ChangeNotifier {
if (index != null) { if (index != null) {
_animationController!.value = index.toDouble(); _animationController!.value = index.toDouble();
} }
return TabController._( final TabController newController = TabController._(
index: index ?? _index, index: index ?? _index,
length: length ?? this.length, length: length ?? this.length,
animationController: _animationController, animationController: _animationController,
previousIndex: previousIndex ?? _previousIndex, previousIndex: previousIndex ?? _previousIndex,
animationDuration: animationDuration ?? _animationDuration, animationDuration: animationDuration ?? _animationDuration,
); );
// Nulling _animationController to not dispose it. It will be disposed by
// the newly created instance of the TabController.
_animationController = null;
dispose();
return newController;
} }
/// An animation whose value represents the current position of the [TabBar]'s /// An animation whose value represents the current position of the [TabBar]'s
@ -485,7 +498,7 @@ class _DefaultTabControllerState extends State<DefaultTabController> with Single
newIndex = math.max(0, widget.length - 1); newIndex = math.max(0, widget.length - 1);
previousIndex = _controller.index; previousIndex = _controller.index;
} }
_controller = _controller._copyWith( _controller = _controller._copyWithAndDispose(
length: widget.length, length: widget.length,
animationDuration: widget.animationDuration, animationDuration: widget.animationDuration,
index: newIndex, index: newIndex,
@ -494,7 +507,7 @@ class _DefaultTabControllerState extends State<DefaultTabController> with Single
} }
if (oldWidget.animationDuration != widget.animationDuration) { if (oldWidget.animationDuration != widget.animationDuration) {
_controller = _controller._copyWith( _controller = _controller._copyWithAndDispose(
length: widget.length, length: widget.length,
animationDuration: widget.animationDuration, animationDuration: widget.animationDuration,
index: _controller.index, index: _controller.index,

File diff suppressed because it is too large Load diff