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>See Widget::key.
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: WidgetRefThe 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: boolWhether 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
impl Hero
Sourcepub fn new<K>(tag: Rc<dyn AnyEq>, child: impl IntoWidget<K>) -> Hero
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().
Sourcepub fn create_rect_tween(self, create_rect_tween: CreateRectTween) -> Hero
pub fn create_rect_tween(self, create_rect_tween: CreateRectTween) -> Hero
Dart Hero(createRectTween:).
Sourcepub fn flight_shuttle_builder(
self,
flight_shuttle_builder: HeroFlightShuttleBuilder,
) -> Hero
pub fn flight_shuttle_builder( self, flight_shuttle_builder: HeroFlightShuttleBuilder, ) -> Hero
Dart Hero(flightShuttleBuilder:).
Sourcepub fn placeholder_builder(
self,
placeholder_builder: HeroPlaceholderBuilder,
) -> Hero
pub fn placeholder_builder( self, placeholder_builder: HeroPlaceholderBuilder, ) -> Hero
Dart Hero(placeholderBuilder:).
Sourcepub fn transition_on_user_gestures(
self,
transition_on_user_gestures: bool,
) -> Hero
pub fn transition_on_user_gestures( self, transition_on_user_gestures: bool, ) -> Hero
Dart Hero(transitionOnUserGestures:).
Sourcepub fn reverse_curve(self, reverse_curve: Rc<dyn Curve>) -> Hero
pub fn reverse_curve(self, reverse_curve: Rc<dyn Curve>) -> Hero
Dart Hero(reverseCurve:).
Trait Implementations§
Source§impl StatefulWidget for Hero
impl StatefulWidget for Hero
Source§fn debug_fill_properties(
&self,
_app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )
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
fn create_state(&self) -> HeroState
Source§fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
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 Herowhere
*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 Herowhere
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 Herowhere
*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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.