Skip to content

Commit cfff88b

Browse files
Remerge "Fixed AnimatedSwitcher chain produced duplicates" after fixing issues with g3
This reverts commit 1d2fa28.
1 parent e6f69ad commit cfff88b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/flutter/lib/src/widgets/animated_switcher.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class AnimatedSwitcher extends StatefulWidget {
222222
/// This is an [AnimatedSwitcherTransitionBuilder] function.
223223
static Widget defaultTransitionBuilder(Widget child, Animation<double> animation) {
224224
return FadeTransition(
225+
key: ValueKey<Key?>(child.key),
225226
opacity: animation,
226227
child: child,
227228
);
@@ -394,6 +395,6 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider
394395
@override
395396
Widget build(BuildContext context) {
396397
_rebuildOutgoingWidgetsIfNeeded();
397-
return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!);
398+
return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!.where((Widget outgoing) => outgoing.key != _currentEntry?.transition.key).toSet().toList());
398399
}
399400
}

packages/flutter/test/widgets/animated_switcher_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,25 @@ void main() {
415415
);
416416
}
417417
});
418+
419+
testWidgets('AnimatedSwitcher does not duplicate animations if the same child is entered twice.', (WidgetTester tester) async {
420+
Future<void> pumpChild(Widget child) async {
421+
return tester.pumpWidget(
422+
Directionality(
423+
textDirection: TextDirection.ltr,
424+
child: AnimatedSwitcher(
425+
duration: const Duration(milliseconds: 1000),
426+
child: child,
427+
),
428+
),
429+
);
430+
}
431+
await pumpChild(const Text('1', key: Key('1')));
432+
await pumpChild(const Text('2', key: Key('2')));
433+
await pumpChild(const Text('1', key: Key('1')));
434+
await tester.pump(const Duration(milliseconds: 1000));
435+
expect(find.text('1'), findsOneWidget);
436+
});
418437
}
419438

420439
class StatefulTest extends StatefulWidget {

0 commit comments

Comments
 (0)