Skip to main content

Hero

Struct Hero 

Source
pub struct Hero {
    pub key: Option<KeyRef>,
    pub tag: Rc<dyn AnyEq>,
    pub create_rect_tween: Option<CreateRectTween>,
    pub child: WidgetRef,
    pub flight_shuttle_builder: Option<HeroFlightShuttleBuilder>,
    pub placeholder_builder: Option<HeroPlaceholderBuilder>,
    pub transition_on_user_gestures: bool,
    pub curve: Rc<dyn Curve>,
    pub reverse_curve: Option<Rc<dyn Curve>>,
}
Expand description

A widget that marks its child as being a candidate for hero animations.

When a PageRoute is pushed or popped with the Navigator, the entire screen’s content is replaced. An old route disappears and a new route appears. If there’s a common visual feature on both routes then it can be helpful for orienting the user for the feature to physically move from one page to the other during the routes’ transition. Such an animation is called a hero animation. The hero widgets “fly” in the navigator’s overlay during the transition and while they’re in-flight they’re, by default, not shown in their original locations in the old and new routes.

To label a widget as such a feature, wrap it in a Hero widget. When navigation happens, the Hero widgets on each route are identified by the HeroController. For each pair of Hero widgets that have the same tag, a hero animation is triggered.

If a Hero is already in flight when navigation occurs, its flight animation will be redirected to its new destination. The widget shown in-flight during the transition is, by default, the destination route’s Hero’s child.

For a hero animation to trigger, the hero has to exist on the very first frame of the new page’s animation.

Routes must not contain more than one Hero for each tag.

§Discussion

Heroes and the Navigator’s Overlay Stack must be axis-aligned for all this to work. The top left and bottom right coordinates of each animated hero will be converted to global coordinates and then from there converted to that Stack’s coordinate space, and the entire hero subtree will, for the duration of the animation, be lifted out of its original place, and positioned on that stack. If the Hero isn’t axis aligned, this is going to fail in a rather ugly fashion. Don’t rotate your heroes!

To make the animations look good, it’s critical that the widget tree for the hero in both locations be essentially identical. The widget of the target is, by default, used to do the transition: when going from route A to route B, route B’s hero’s widget is placed over route A’s hero’s widget. Additionally, if the Hero subtree changes appearance based on an InheritedWidget (such as MediaQuery or Theme), then the hero animation may have discontinuity at the start or the end of the animation because route A and route B provides different such InheritedWidgets. Consider providing a custom flight_shuttle_builder to ensure smooth transitions. The default flight shuttle builder interpolates MediaQuery’s paddings. If your Hero widget uses custom InheritedWidgets and displays a discontinuity in the animation, try to provide custom in-flight transition using flight_shuttle_builder.

By default, both route A and route B’s heroes are hidden while the transitioning widget is animating in-flight above the 2 routes. placeholder_builder can be used to show a custom widget in their place instead once the transition has taken flight.

During the transition, the transition widget is animated to route B’s hero’s position, and then the widget is inserted into route B. When going back from B to A, route A’s hero’s widget is, by default, placed over where route B’s hero’s widget was, and then the animation goes the other way.

§Nested Navigators

If either or both routes contain nested Navigators, only Heroes contained in the top-most routes (as defined by Route::is_current) of those nested Navigators are considered for animation. Just like in the non-nested case the top-most routes containing these Heroes in the nested Navigators have to be PageRoutes.

Fields§

§key: Option<KeyRef>§tag: Rc<dyn AnyEq>

The identifier for this particular hero. If the tag of this hero matches the tag of a hero on a PageRoute that we’re navigating to or from, then a hero animation will be triggered.

§create_rect_tween: Option<CreateRectTween>

Defines how the destination hero’s bounds change as it flies from the starting route to the destination route.

A hero flight begins with the destination hero’s child aligned with the starting hero’s child. The tween returned by this callback is used to compute the hero’s bounds as the flight animation’s value goes from 0.0 to 1.0.

If this property is None, the default, then the value of HeroController::create_rect_tween is used.

§child: WidgetRef

The widget subtree that will “fly” from one route to another during a Navigator push or pop transition.

The appearance of this subtree should be similar to the appearance of the subtrees of any other heroes in the application with the same tag. Changes in scale and aspect ratio work well in hero animations, changes in layout or composition do not.

§flight_shuttle_builder: Option<HeroFlightShuttleBuilder>

Optional override to supply a widget that’s shown during the hero’s flight.

This in-flight widget can depend on the route transition’s animation as well as the incoming and outgoing routes’ Hero descendants’ widgets and layout.

When both the source and destination Heroes provide a flight_shuttle_builder, the destination’s takes precedence.

If none is provided, the destination route’s hero child is shown in-flight by default.

§Limitations

If a widget built by flight_shuttle_builder takes part in a Navigator push transition, that widget or its descendants must not have any GlobalKey that is used in the source hero’s descendant widgets. That is because both subtrees will be included in the widget tree during the hero flight animation, and GlobalKeys must be unique across the entire widget tree.

If the said GlobalKey is essential to your application, consider providing a custom placeholder_builder for the source hero, to avoid the GlobalKey collision, such as a builder that builds an empty SizedBox, keeping the hero child’s original size.

§placeholder_builder: Option<HeroPlaceholderBuilder>

Placeholder widget left in place as the hero’s child once the flight takes off.

By default the placeholder widget is an empty SizedBox keeping the hero child’s original size, unless this hero is a source hero of a Navigator push transition, in which case child will be a descendant of the placeholder and will be kept Offstage during the hero’s flight.

§transition_on_user_gestures: bool

Whether to perform the hero transition if the PageRoute transition was triggered by a user gesture, such as a back swipe on iOS.

If Heroes with the same tag on both the from and the to routes have transition_on_user_gestures set to true, a back swipe gesture will trigger the same hero animation as a programmatically triggered push or pop.

The route being popped to or the bottom route must also have PageRoute::maintain_state set to true for a gesture triggered hero transition to work.

Defaults to false.

§curve: Rc<dyn Curve>

The curve to use in the forward direction.

Defaults to Curves::fast_out_slow_in().

§reverse_curve: Option<Rc<dyn Curve>>

The curve to use in the reverse direction.

If this property is None, Hero::curve.flipped() is used.

Implementations§

Source§

impl Hero

Source

pub fn new<K>(tag: Rc<dyn AnyEq>, child: impl IntoWidget<K>) -> Hero

Create a hero; Dart’s optional named arguments are the setters.

The child parameter and all of its descendants must not be Heroes.

Dart’s defaults: transition_on_user_gestures false, curve Curves::fast_out_slow_in().

Source

pub fn key(self, key: KeyRef) -> Hero

Dart Hero(key:).

Source

pub fn create_rect_tween(self, create_rect_tween: CreateRectTween) -> Hero

Dart Hero(createRectTween:).

Source

pub fn flight_shuttle_builder( self, flight_shuttle_builder: HeroFlightShuttleBuilder, ) -> Hero

Dart Hero(flightShuttleBuilder:).

Source

pub fn placeholder_builder( self, placeholder_builder: HeroPlaceholderBuilder, ) -> Hero

Dart Hero(placeholderBuilder:).

Source

pub fn transition_on_user_gestures( self, transition_on_user_gestures: bool, ) -> Hero

Dart Hero(transitionOnUserGestures:).

Source

pub fn curve(self, curve: Rc<dyn Curve>) -> Hero

Dart Hero(curve:).

Source

pub fn reverse_curve(self, reverse_curve: Rc<dyn Curve>) -> Hero

Dart Hero(reverseCurve:).

Trait Implementations§

Source§

impl Debug for Hero

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StatefulWidget for Hero

Source§

type State = HeroState

The state this widget creates.
Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn create_state(&self) -> HeroState

Creates the mutable state for this widget at a given location in the tree. Read more
Source§

fn debug_check_constructor(&self)

Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Source§

fn type_name(&self) -> &'static str

See Widget::type_name. A stateful form another widget kind becomes (an animated or status transition widget) names the widget it wraps.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Hero

§

impl !Send for Hero

§

impl !Sync for Hero

§

impl !UnwindSafe for Hero

§

impl Forget for Hero

§

impl Freeze for Hero
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn AnyEq> is !null: Freeze, PhantomData<RcInner<dyn AnyEq>>: Freeze, *const RcInner<dyn Fn(&mut App, Option<Rect>, Option<Rect>) -> Rc<dyn RectTweenObject>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, Option<Rect>, Option<Rect>) -> Rc<dyn RectTweenObject>>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, Retained<AnyAnimation<f64>>, HeroFlightDirection, AnyElement, AnyElement) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, Retained<AnyAnimation<f64>>, HeroFlightDirection, AnyElement, AnyElement) -> Rc<dyn Widget>>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, Size, Rc<dyn Widget>) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, Size, Rc<dyn Widget>) -> Rc<dyn Widget>>>: Freeze, *const RcInner<dyn Curve> is !null: Freeze, PhantomData<RcInner<dyn Curve>>: Freeze,

§

impl Move for Hero

§

impl Unpin for Hero
where Rc<dyn Key>: Unpin, Rc<dyn AnyEq>: Unpin, Rc<dyn Fn(&mut App, Option<Rect>, Option<Rect>) -> Rc<dyn RectTweenObject>>: Unpin, Rc<dyn Widget>: Unpin, Rc<dyn Fn(&mut App, AnyElement, Retained<AnyAnimation<f64>>, HeroFlightDirection, AnyElement, AnyElement) -> Rc<dyn Widget>>: Unpin, Rc<dyn Fn(&mut App, AnyElement, Size, Rc<dyn Widget>) -> Rc<dyn Widget>>: Unpin, Rc<dyn Curve>: Unpin,

§

impl UnsafeUnpin for Hero
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn AnyEq> is !null: UnsafeUnpin, PhantomData<RcInner<dyn AnyEq>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, Option<Rect>, Option<Rect>) -> Rc<dyn RectTweenObject>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, Option<Rect>, Option<Rect>) -> Rc<dyn RectTweenObject>>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Widget>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, Retained<AnyAnimation<f64>>, HeroFlightDirection, AnyElement, AnyElement) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, Retained<AnyAnimation<f64>>, HeroFlightDirection, AnyElement, AnyElement) -> Rc<dyn Widget>>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, Size, Rc<dyn Widget>) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, Size, Rc<dyn Widget>) -> Rc<dyn Widget>>>: UnsafeUnpin, *const RcInner<dyn Curve> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Curve>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<W> IntoWidget<StatefulKind> for W
where W: StatefulWidget,

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>