Remerge "Fixed AnimatedSwitcher chain produced duplicates" after fixing issues with g3

This reverts commit 1d2fa285a4704678f3f813ad8c6464749292de04.
This commit is contained in:
Youssef Attia 2022-07-12 13:42:04 -07:00 committed by GitHub
parent e6f69add99
commit cfff88b26a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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,
);
@ -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());
}
}

View File

@ -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 {