From cfff88b26a4cecba390bbe3a45ef16e3b459c636 Mon Sep 17 00:00:00 2001 From: Youssef Attia <16466991+youssef-attia@users.noreply.github.com> Date: Tue, 12 Jul 2022 13:42:04 -0700 Subject: [PATCH] Remerge "Fixed AnimatedSwitcher chain produced duplicates" after fixing issues with g3 This reverts commit 1d2fa285a4704678f3f813ad8c6464749292de04. --- .../lib/src/widgets/animated_switcher.dart | 3 ++- .../test/widgets/animated_switcher_test.dart | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/animated_switcher.dart b/packages/flutter/lib/src/widgets/animated_switcher.dart index 76be5631a0f8..35f6ae5f52a6 100644 --- a/packages/flutter/lib/src/widgets/animated_switcher.dart +++ b/packages/flutter/lib/src/widgets/animated_switcher.dart @@ -222,6 +222,7 @@ class AnimatedSwitcher extends StatefulWidget { /// This is an [AnimatedSwitcherTransitionBuilder] function. static Widget defaultTransitionBuilder(Widget child, Animation animation) { return FadeTransition( + key: ValueKey(child.key), opacity: animation, child: child, ); @@ -394,6 +395,6 @@ class _AnimatedSwitcherState extends State with TickerProvider @override Widget build(BuildContext context) { _rebuildOutgoingWidgetsIfNeeded(); - return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!); + return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!.where((Widget outgoing) => outgoing.key != _currentEntry?.transition.key).toSet().toList()); } } diff --git a/packages/flutter/test/widgets/animated_switcher_test.dart b/packages/flutter/test/widgets/animated_switcher_test.dart index 335ddd608333..0d914cb73ed9 100644 --- a/packages/flutter/test/widgets/animated_switcher_test.dart +++ b/packages/flutter/test/widgets/animated_switcher_test.dart @@ -415,6 +415,25 @@ void main() { ); } }); + + testWidgets('AnimatedSwitcher does not duplicate animations if the same child is entered twice.', (WidgetTester tester) async { + Future pumpChild(Widget child) async { + return tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: AnimatedSwitcher( + duration: const Duration(milliseconds: 1000), + child: child, + ), + ), + ); + } + await pumpChild(const Text('1', key: Key('1'))); + await pumpChild(const Text('2', key: Key('2'))); + await pumpChild(const Text('1', key: Key('1'))); + await tester.pump(const Duration(milliseconds: 1000)); + expect(find.text('1'), findsOneWidget); + }); } class StatefulTest extends StatefulWidget {