Skip to content

Commit 9cb7a15

Browse files
authored
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"
1 parent 712ce3b commit 9cb7a15

2 files changed

Lines changed: 15 additions & 38 deletions

File tree

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class AnimatedSwitcher extends StatefulWidget {
216216
/// This is an [AnimatedSwitcherTransitionBuilder] function.
217217
static Widget defaultTransitionBuilder(Widget child, Animation<double> animation) {
218218
return FadeTransition(
219+
key: ValueKey<Key?>(child.key),
219220
opacity: animation,
220221
child: child,
221222
);
@@ -337,10 +338,7 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider
337338
}) {
338339
final _ChildEntry entry = _ChildEntry(
339340
widgetChild: child,
340-
transition: KeyedSubtree(
341-
key: ValueKey<int>(_childNumber),
342-
child: builder(child, animation),
343-
),
341+
transition: KeyedSubtree.wrap(builder(child, animation), _childNumber),
344342
animation: animation,
345343
controller: controller,
346344
);
@@ -391,6 +389,6 @@ class _AnimatedSwitcherState extends State<AnimatedSwitcher> with TickerProvider
391389
@override
392390
Widget build(BuildContext context) {
393391
_rebuildOutgoingWidgetsIfNeeded();
394-
return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!);
392+
return widget.layoutBuilder(_currentEntry?.transition, _outgoingWidgets!.where((Widget outgoing) => outgoing.key != _currentEntry?.transition.key).toSet().toList());
395393
}
396394
}

packages/flutter/test/widgets/animated_switcher_test.dart

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -416,44 +416,23 @@ void main() {
416416
}
417417
});
418418

419-
testWidgets('AnimatedSwitcher can handle multiple children with the same key.', (WidgetTester tester) async {
420-
final UniqueKey containerA = UniqueKey();
421-
final UniqueKey containerB = UniqueKey();
422-
423-
// Pump an AnimatedSwitcher with a child container with the given key.
424-
Future<void> pump(Key key) async {
419+
testWidgets('AnimatedSwitcher does not duplicate animations if the same child is entered twice.', (WidgetTester tester) async {
420+
Future<void> pumpChild(Widget child) async {
425421
return tester.pumpWidget(
426-
AnimatedSwitcher(
427-
duration: const Duration(milliseconds: 1000),
428-
child: Container(key: key),
422+
Directionality(
423+
textDirection: TextDirection.ltr,
424+
child: AnimatedSwitcher(
425+
duration: const Duration(milliseconds: 1000),
426+
child: child,
427+
),
429428
),
430429
);
431430
}
432-
433-
// Pump four widgets with the two keys A and B in alternating order.
434-
await pump(containerA);
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')));
435434
await tester.pump(const Duration(milliseconds: 1000));
436-
await pump(containerB);
437-
await tester.pump(const Duration(milliseconds: 500));
438-
await pump(containerA);
439-
await tester.pump(const Duration(milliseconds: 250));
440-
await pump(containerB);
441-
await tester.pump(const Duration(milliseconds: 125));
442-
443-
// All four widgets should still be animating in (the one pumped last) or
444-
// out (the other ones), and thus have an associated FadeTransition each.
445-
expect(find.byType(FadeTransition), findsNWidgets(4));
446-
final Iterable<FadeTransition> transitions = tester.widgetList(
447-
find.byType(FadeTransition),
448-
);
449-
450-
// The exponentially decaying timing used in pumping the widgets should have
451-
// lined up all of the FadeTransitions' values to be the same.
452-
for (final FadeTransition transition in transitions) {
453-
expect(transition.opacity.value, moreOrLessEquals(0.125, epsilon: 0.001));
454-
}
455-
456-
await tester.pumpAndSettle();
435+
expect(find.text('1'), findsOneWidget);
457436
});
458437
}
459438

0 commit comments

Comments
 (0)