diff --git a/packages/flutter/lib/src/widgets/heroes.dart b/packages/flutter/lib/src/widgets/heroes.dart index ced275d76f..b5ca5b3414 100644 --- a/packages/flutter/lib/src/widgets/heroes.dart +++ b/packages/flutter/lib/src/widgets/heroes.dart @@ -62,12 +62,12 @@ class _HeroManifest { }); final GlobalKey key; final Widget config; - final Set sourceStates; + final Set<_HeroState> sourceStates; final Rect currentRect; final double currentTurns; } -abstract class HeroHandle { +abstract class _HeroHandle { bool get alwaysAnimate; _HeroManifest _takeChild(Animation currentAnimation); } @@ -94,9 +94,9 @@ class Hero extends StatefulWidget { /// animate to or from. final bool alwaysAnimate; - /// Return a hero tag to HeroState map of all of the heroes within the given subtree. - static Map of(BuildContext context) { - final Map result = {}; + /// Return a hero tag to _HeroState map of all of the heroes within the given subtree. + static Map _of(BuildContext context) { + final Map result = {}; void visitor(Element element) { if (element.widget is Hero) { StatefulElement hero = element; @@ -114,7 +114,7 @@ class Hero extends StatefulWidget { } return true; }); - HeroState heroState = hero.state; + _HeroState heroState = hero.state; result[tag] = heroState; } element.visitChildren(visitor); @@ -124,11 +124,10 @@ class Hero extends StatefulWidget { } @override - HeroState createState() => new HeroState(); + _HeroState createState() => new _HeroState(); } -class HeroState extends State implements HeroHandle { - +class _HeroState extends State implements _HeroHandle { GlobalKey _key = new GlobalKey(); Size _placeholderSize; VoidCallback _disposeCallback; @@ -154,7 +153,7 @@ class HeroState extends State implements HeroHandle { _HeroManifest result = new _HeroManifest( key: _key, // might be null, e.g. if the hero is returning to us config: config, - sourceStates: new HashSet.from([this]), + sourceStates: new HashSet<_HeroState>.from(<_HeroState>[this]), currentRect: heroArea, currentTurns: config.turns.toDouble() ); @@ -166,6 +165,7 @@ class HeroState extends State implements HeroHandle { void _setChild(GlobalKey value) { assert(_key == null); assert(_placeholderSize != null); + assert(value != null); assert(mounted); setState(() { _key = value; @@ -175,7 +175,7 @@ class HeroState extends State implements HeroHandle { void _resetChild() { if (mounted) - _setChild(null); + _setChild(new GlobalKey()); } @override @@ -196,11 +196,10 @@ class HeroState extends State implements HeroHandle { child: config.child ); } - } -class _HeroQuestState implements HeroHandle { +class _HeroQuestState implements _HeroHandle { _HeroQuestState({ this.tag, this.key, @@ -214,10 +213,8 @@ class _HeroQuestState implements HeroHandle { this.currentTurns }) { assert(tag != null); - - for (HeroState state in sourceStates) + for (_HeroState state in sourceStates) state._disposeCallback = () => sourceStates.remove(state); - if (targetState != null) targetState._disposeCallback = _handleTargetStateDispose; } @@ -225,11 +222,11 @@ class _HeroQuestState implements HeroHandle { final Object tag; final GlobalKey key; final Widget child; - final Set sourceStates; + final Set<_HeroState> sourceStates; final Rect animationArea; Rect targetRect; int targetTurns; - HeroState targetState; + _HeroState targetState; final RectTween currentRect; final Tween currentTurns; @@ -245,13 +242,11 @@ class _HeroQuestState implements HeroHandle { _HeroManifest _takeChild(Animation currentAnimation) { assert(!taken); _taken = true; - Set states = sourceStates; + Set<_HeroState> states = sourceStates; if (targetState != null) - states = states.union(new HashSet.from([targetState])); - - for (HeroState state in states) + states = states.union(new HashSet<_HeroState>.from(<_HeroState>[targetState])); + for (_HeroState state in states) state._disposeCallback = null; - return new _HeroManifest( key: key, config: child, @@ -287,10 +282,8 @@ class _HeroQuestState implements HeroHandle { @mustCallSuper void dispose() { overlayEntry = null; - - for (HeroState state in sourceStates) + for (_HeroState state in sourceStates) state._disposeCallback = null; - if (targetState != null) targetState._disposeCallback = null; } @@ -298,15 +291,15 @@ class _HeroQuestState implements HeroHandle { class _HeroMatch { const _HeroMatch(this.from, this.to, this.tag); - final HeroHandle from; - final HeroHandle to; + final _HeroHandle from; + final _HeroHandle to; final Object tag; } typedef RectTween CreateRectTween(Rect begin, Rect end); -class HeroParty { - HeroParty({ this.onQuestFinished, this.createRectTween }); +class _HeroParty { + _HeroParty({ this.onQuestFinished, this.createRectTween }); final VoidCallback onQuestFinished; final CreateRectTween createRectTween; @@ -314,8 +307,8 @@ class HeroParty { List<_HeroQuestState> _heroes = <_HeroQuestState>[]; bool get isEmpty => _heroes.isEmpty; - Map getHeroesToAnimate() { - Map result = new Map(); + Map getHeroesToAnimate() { + Map result = new Map(); for (_HeroQuestState hero in _heroes) result[hero.tag] = hero; assert(!result.containsKey(null)); @@ -333,7 +326,7 @@ class HeroParty { return new Tween(begin: begin, end: end); } - void animate(Map heroesFrom, Map heroesTo, Rect animationArea) { + void animate(Map heroesFrom, Map heroesTo, Rect animationArea) { assert(!heroesFrom.containsKey(null)); assert(!heroesTo.containsKey(null)); @@ -354,13 +347,13 @@ class HeroParty { (heroPair.to == null && !heroPair.from.alwaysAnimate)) continue; _HeroManifest from = heroPair.from?._takeChild(_currentAnimation); - assert(heroPair.to == null || heroPair.to is HeroState); + assert(heroPair.to == null || heroPair.to is _HeroState); _HeroManifest to = heroPair.to?._takeChild(_currentAnimation); assert(from != null || to != null); assert(to == null || to.sourceStates.length == 1); assert(to == null || to.currentTurns.floor() == to.currentTurns); - HeroState targetState = to != null ? to.sourceStates.elementAt(0) : null; - Set sourceStates = from?.sourceStates ?? new HashSet(); + _HeroState targetState = to != null ? to.sourceStates.elementAt(0) : null; + Set<_HeroState> sourceStates = from?.sourceStates ?? new HashSet<_HeroState>(); sourceStates.remove(targetState); Rect sourceRect = from?.currentRect ?? to.currentRect.center & Size.zero; Rect targetRect = to?.currentRect ?? from.currentRect.center & Size.zero; @@ -408,7 +401,7 @@ class HeroParty { for (_HeroQuestState hero in _heroes) { if (hero.targetState != null) hero.targetState._setChild(hero.key); - for (HeroState source in hero.sourceStates) + for (_HeroState source in hero.sourceStates) source._resetChild(); hero.dispose(); } @@ -425,13 +418,13 @@ class HeroParty { class HeroController extends NavigatorObserver { HeroController({ CreateRectTween createRectTween }) { - _party = new HeroParty( + _party = new _HeroParty( onQuestFinished: _handleQuestFinished, createRectTween: createRectTween ); } - HeroParty _party; + _HeroParty _party; Animation _animation; PageRoute _from; PageRoute _to; @@ -523,10 +516,10 @@ class HeroController extends NavigatorObserver { // The navigator was removed before this end-of-frame callback was called. return; } - Map heroesFrom = _party.isEmpty ? - Hero.of(_from.subtreeContext) : _party.getHeroesToAnimate(); + Map heroesFrom = _party.isEmpty ? + Hero._of(_from.subtreeContext) : _party.getHeroesToAnimate(); - Map heroesTo = Hero.of(_to.subtreeContext); + Map heroesTo = Hero._of(_to.subtreeContext); _to.offstage = false; Animation animation = _animation; // The route's animation. diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 3494a0c320..babbca051d 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -251,7 +251,7 @@ abstract class TransitionRoute extends OverlayRoute { @override void dispose() { - _controller.stop(); + _controller.dispose(); super.dispose(); }