Skip to content

Commit

Permalink
Fixed AnimatedSwitcher chain produced duplicates (#106962)
Browse files Browse the repository at this point in the history
* Fixed AnimatedSwitcher chain produced duplicates

* Fixed issue with global keys being copied

* trailing whitespace

* removed unused dependencies

* Fixed dependency loop
  • Loading branch information
youssef-attia authored Jul 7, 2022
1 parent 1573934 commit 0d3cc92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/flutter/lib/src/widgets/animated_switcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class AnimatedSwitcher extends StatefulWidget {
/// This is an [AnimatedSwitcherTransitionBuilder] function.
static Widget defaultTransitionBuilder(Widget child, Animation<double> animation) {
return FadeTransition(
key: ValueKey<Key?>(child.key),
opacity: animation,
child: child,
);
Expand Down Expand Up @@ -394,6 +395,6 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> 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());
}
}
19 changes: 19 additions & 0 deletions packages/flutter/test/widgets/animated_switcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,25 @@ void main() {
);
}
});

testWidgets('AnimatedSwitcher does not duplicate animations if the same child is entered twice.', (WidgetTester tester) async {
Future<void> 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 {
Expand Down

0 comments on commit 0d3cc92

Please sign in to comment.