Ejemplo n.º 1
0
        internal static Dictionary <object, _HeroState> _allHeroesFor(
            BuildContext context,
            bool isUserGestureTransition,
            NavigatorState navigator)
        {
            D.assert(context != null);
            D.assert(navigator != null);

            Dictionary <object, _HeroState> result = new Dictionary <object, _HeroState> {
            };

            void inviteHero(StatefulElement hero, object tag)
            {
                D.assert(() => {
                    if (result.ContainsKey(tag))
                    {
                        throw new UIWidgetsError(
                            new List <DiagnosticsNode>()
                        {
                            new ErrorSummary("There are multiple heroes that share the same tag within a subtree."),
                            new ErrorDescription(
                                "Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), " +
                                "each Hero must have a unique non-null tag.\n" +
                                $"In this case, multiple heroes had the following tag: {tag}\n"
                                ),
                            new DiagnosticsProperty <StatefulElement>("Here is the subtree for one of the offending heroes", hero, linePrefix: "# ", style: DiagnosticsTreeStyle.dense),
                        });
                    }
                    return(true);
                });
                Hero       heroWidget = hero.widget as Hero;
                _HeroState heroState  = hero.state as _HeroState;

                if (!isUserGestureTransition || heroWidget.transitionOnUserGestures)
                {
                    result[tag] = heroState;
                }
                else
                {
                    heroState.ensurePlaceholderIsHidden();
                }
            }

            void visitor(Element element)
            {
                Widget widget = element.widget;

                if (widget is Hero)
                {
                    StatefulElement hero = element as StatefulElement;
                    object          tag  = ((Hero)widget).tag;
                    D.assert(tag != null);
                    if (Navigator.of(hero) == navigator)
                    {
                        inviteHero(hero, tag);
                    }
                    else
                    {
                        ModalRoute heroRoute = ModalRoute.of(hero);
                        if (heroRoute != null && heroRoute is PageRoute && heroRoute.isCurrent)
                        {
                            inviteHero(hero, tag);
                        }
                    }
                }

                element.visitChildren(visitor);
            }

            context.visitChildElements(visitor);
            return(result);
        }
Ejemplo n.º 2
0
        void _startHeroTransition(
            PageRoute from,
            PageRoute to,
            Animation <float> animation,
            HeroFlightDirection flightType,
            bool isUserGestureTransition
            )
        {
            if (navigator == null || from.subtreeContext == null || to.subtreeContext == null)
            {
                to.offstage = false; // in case we set this in _maybeStartHeroTransition
                return;
            }

            Rect navigatorRect = HeroUtils._boundingBoxFor(navigator.context);//_globalBoundingBoxFor(navigator.context);

            Dictionary <object, _HeroState> fromHeroes =
                Hero._allHeroesFor(from.subtreeContext, isUserGestureTransition, navigator);
            Dictionary <object, _HeroState> toHeroes =
                Hero._allHeroesFor(to.subtreeContext, isUserGestureTransition, navigator);

            to.offstage = false;

            foreach (object tag in fromHeroes.Keys)
            {
                if (toHeroes.ContainsKey(tag))
                {
                    HeroFlightShuttleBuilder fromShuttleBuilder = fromHeroes[tag].widget.flightShuttleBuilder;
                    HeroFlightShuttleBuilder toShuttleBuilder   = toHeroes[tag].widget.flightShuttleBuilder;
                    bool isDiverted = _flights.ContainsKey(tag);
                    _HeroFlightManifest manifest = new _HeroFlightManifest(
                        type: flightType,
                        overlay: navigator.overlay,
                        navigatorRect: navigatorRect,
                        fromRoute: from,
                        toRoute: to,
                        fromHero: fromHeroes[tag],
                        toHero: toHeroes[tag],
                        createRectTween: createRectTween,
                        shuttleBuilder:
                        toShuttleBuilder ?? fromShuttleBuilder ?? _defaultHeroFlightShuttleBuilder,
                        isUserGestureTransition: isUserGestureTransition,
                        isDiverted: isDiverted
                        );

                    //if (_flights.TryGetValue(tag, out var result)) {
                    if (isDiverted)
                    {
                        _flights[tag].divert(manifest);
                    }
                    else
                    {
                        _flights[tag] = new _HeroFlight(_handleFlightEnded);
                        _flights[tag].start(manifest);
                    }
                }
                else if (_flights.TryGetValue(tag, out var result))
                {
                    result.abort();
                }
            }
            foreach (object tag in toHeroes.Keys)
            {
                _HeroState heroState = null;
                if (!fromHeroes.TryGetValue(tag, out heroState))
                {
                    toHeroes[tag].ensurePlaceholderIsHidden();
                }
            }
        }