From 2fff488d60207dc6198e777a4e6caf08f8088bc3 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Wed, 19 Nov 2025 08:12:19 +0100 Subject: [PATCH] Fix train hopping animation status listeners (#178372) Fixes https://github.com/flutter/flutter/issues/178336 ### Description - Fixes `TrainHoppingAnimation` to notify status listeners when `status` changes - Adds tests ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../flutter/lib/src/animation/animations.dart | 2 +- .../test/animation/animations_test.dart | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/animation/animations.dart b/packages/flutter/lib/src/animation/animations.dart index 3b4d92f235b..924eb9c2e44 100644 --- a/packages/flutter/lib/src/animation/animations.dart +++ b/packages/flutter/lib/src/animation/animations.dart @@ -543,7 +543,7 @@ class TrainHoppingAnimation extends Animation void _statusChangeHandler(AnimationStatus status) { assert(_currentTrain != null); if (status != _lastStatus) { - notifyListeners(); + notifyStatusListeners(status); _lastStatus = status; } assert(_lastStatus != null); diff --git a/packages/flutter/test/animation/animations_test.dart b/packages/flutter/test/animation/animations_test.dart index f2d063b23b1..d6486c86c72 100644 --- a/packages/flutter/test/animation/animations_test.dart +++ b/packages/flutter/test/animation/animations_test.dart @@ -99,7 +99,9 @@ void main() { test('TrainHoppingAnimation', () { final AnimationController currentTrain = AnimationController(vsync: const TestVSync()); + addTearDown(currentTrain.dispose); final AnimationController nextTrain = AnimationController(vsync: const TestVSync()); + addTearDown(nextTrain.dispose); currentTrain.value = 0.5; nextTrain.value = 0.75; bool didSwitchTrains = false; @@ -133,6 +135,32 @@ void main() { ); }); + // Regression test for https://github.com/flutter/flutter/issues/178336. + test('TrainHoppingAnimation notifies status listeners', () { + final AnimationController controller = AnimationController( + duration: const Duration(milliseconds: 100), + vsync: const TestVSync(), + ); + addTearDown(controller.dispose); + + final TrainHoppingAnimation animation = TrainHoppingAnimation( + Tween(begin: 1.0, end: -1.0).animate(controller), + Tween(begin: -1.0, end: 1.0).animate(controller), + ); + addTearDown(animation.dispose); + + final List statusLog = []; + animation.addStatusListener(statusLog.add); + expect(statusLog, isEmpty); + + controller.forward(); + expect(statusLog, equals([AnimationStatus.forward])); + statusLog.clear(); + + controller.reverse(); + expect(statusLog, equals([AnimationStatus.dismissed])); + }); + test('AnimationMean control test', () { final AnimationController left = AnimationController(value: 0.5, vsync: const TestVSync()); final AnimationController right = AnimationController(vsync: const TestVSync());