Unverified Commit 9cb7a151 authored by Xilai Zhang's avatar Xilai Zhang Committed by GitHub

Revert "Use AnimatedSwitcher's _childNumber as Key in layoutBuilder's Stack (#121408)" (#121835)

[flutter roll] Revert "Use AnimatedSwitcher's _childNumber as Key in layoutBuilder's Stack"
parent 712ce3b5
...@@ -216,6 +216,7 @@ class AnimatedSwitcher extends StatefulWidget { ...@@ -216,6 +216,7 @@ class AnimatedSwitcher extends StatefulWidget {
/// This is an [AnimatedSwitcherTransitionBuilder] function. /// This is an [AnimatedSwitcherTransitionBuilder] function.
static Widget defaultTransitionBuilder(Widget child, Animation<double> animation) { static Widget defaultTransitionBuilder(Widget child, Animation<double> animation) {
return FadeTransition( return FadeTransition(
key: ValueKey<Key?>(child.key),
opacity: animation, opacity: animation,
child: child, child: child,
); );
...@@ -337,10 +338,7 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider ...@@ -337,10 +338,7 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider
}) { }) {
final _ChildEntry entry = _ChildEntry( final _ChildEntry entry = _ChildEntry(
widgetChild: child, widgetChild: child,
transition: KeyedSubtree( transition: KeyedSubtree.wrap(builder(child, animation), _childNumber),
key: ValueKey<int>(_childNumber),
child: builder(child, animation),
),
animation: animation, animation: animation,
controller: controller, controller: controller,
); );
...@@ -391,6 +389,6 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider ...@@ -391,6 +389,6 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_rebuildOutgoingWidgetsIfNeeded(); _rebuildOutgoingWidgetsIfNeeded();
return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!); return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!.where((Widget outgoing) => outgoing.key != _currentEntry?.transition.key).toSet().toList());
} }
} }
...@@ -416,44 +416,23 @@ void main() { ...@@ -416,44 +416,23 @@ void main() {
} }
}); });
testWidgets('AnimatedSwitcher can handle multiple children with the same key.', (WidgetTester tester) async { testWidgets('AnimatedSwitcher does not duplicate animations if the same child is entered twice.', (WidgetTester tester) async {
final UniqueKey containerA = UniqueKey(); Future<void> pumpChild(Widget child) async {
final UniqueKey containerB = UniqueKey();
// Pump an AnimatedSwitcher with a child container with the given key.
Future<void> pump(Key key) async {
return tester.pumpWidget( return tester.pumpWidget(
AnimatedSwitcher( Directionality(
duration: const Duration(milliseconds: 1000), textDirection: TextDirection.ltr,
child: Container(key: key), child: AnimatedSwitcher(
duration: const Duration(milliseconds: 1000),
child: child,
),
), ),
); );
} }
await pumpChild(const Text('1', key: Key('1')));
// Pump four widgets with the two keys A and B in alternating order. await pumpChild(const Text('2', key: Key('2')));
await pump(containerA); await pumpChild(const Text('1', key: Key('1')));
await tester.pump(const Duration(milliseconds: 1000)); await tester.pump(const Duration(milliseconds: 1000));
await pump(containerB); expect(find.text('1'), findsOneWidget);
await tester.pump(const Duration(milliseconds: 500));
await pump(containerA);
await tester.pump(const Duration(milliseconds: 250));
await pump(containerB);
await tester.pump(const Duration(milliseconds: 125));
// All four widgets should still be animating in (the one pumped last) or
// out (the other ones), and thus have an associated FadeTransition each.
expect(find.byType(FadeTransition), findsNWidgets(4));
final Iterable<FadeTransition> transitions = tester.widgetList(
find.byType(FadeTransition),
);
// The exponentially decaying timing used in pumping the widgets should have
// lined up all of the FadeTransitions' values to be the same.
for (final FadeTransition transition in transitions) {
expect(transition.opacity.value, moreOrLessEquals(0.125, epsilon: 0.001));
}
await tester.pumpAndSettle();
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment