From 64300123faa692c1ac3d8feeba171a6e079a741e Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Fri, 13 Sep 2019 19:46:58 -0700 Subject: [PATCH] Restore offstage and ticker mode after hero pop and the from hero is null (#40306) --- packages/flutter/lib/src/widgets/heroes.dart | 10 +++- .../flutter/test/widgets/heroes_test.dart | 50 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/heroes.dart b/packages/flutter/lib/src/widgets/heroes.dart index 475e290da4..9d49c02643 100644 --- a/packages/flutter/lib/src/widgets/heroes.dart +++ b/packages/flutter/lib/src/widgets/heroes.dart @@ -359,7 +359,8 @@ class _HeroState extends State { } // When `keepPlaceholder` is true, the placeholder will continue to be shown - // after the flight ends. + // after the flight ends. Otherwise the child of the Hero will become visible + // and its TickerMode will be re-enabled. void endFlight({ bool keepPlaceholder = false }) { if (!keepPlaceholder) { ensurePlaceholderIsHidden(); @@ -833,6 +834,13 @@ class HeroController extends NavigatorObserver { _flights[tag].abort(); } } + + // If the from hero is gone, the flight won't start and the to hero needs to + // be put on stage again. + for (Object tag in toHeroes.keys) { + if (fromHeroes[tag] == null) + toHeroes[tag].ensurePlaceholderIsHidden(); + } } void _handleFlightEnded(_HeroFlight flight) { diff --git a/packages/flutter/test/widgets/heroes_test.dart b/packages/flutter/test/widgets/heroes_test.dart index 6237dd878a..49cd5860cf 100644 --- a/packages/flutter/test/widgets/heroes_test.dart +++ b/packages/flutter/test/widgets/heroes_test.dart @@ -2256,6 +2256,56 @@ Future main() async { ); }); + // Regression test for https://github.com/flutter/flutter/issues/40239. + testWidgets( + 'In a pop transition, when fromHero is null, the to hero should eventually become visible', + (WidgetTester tester) async { + final GlobalKey navigatorKey = GlobalKey(); + StateSetter setState; + bool shouldDisplayHero = true; + await tester.pumpWidget( + CupertinoApp( + navigatorKey: navigatorKey, + home: Hero( + tag: navigatorKey, + child: const Placeholder(), + ), + ), + ); + + final CupertinoPageRoute route2 = CupertinoPageRoute( + builder: (BuildContext context) { + return CupertinoPageScaffold( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setter) { + setState = setter; + return shouldDisplayHero + ? Hero(tag: navigatorKey, child: const Text('text')) + : const SizedBox(); + }, + ), + ); + }, + ); + + navigatorKey.currentState.push(route2); + await tester.pumpAndSettle(); + + expect(find.text('text'), findsOneWidget); + expect(find.byType(Placeholder), findsNothing); + + setState(() { shouldDisplayHero = false; }); + await tester.pumpAndSettle(); + + expect(find.text('text'), findsNothing); + + navigatorKey.currentState.pop(); + await tester.pumpAndSettle(); + + expect(find.byType(Placeholder), findsOneWidget); + }, + ); + testWidgets('popped hero uses fastOutSlowIn curve', (WidgetTester tester) async { final Key container1 = UniqueKey(); final Key container2 = UniqueKey();