Skip to main content

AnimatedSwitcher

Struct AnimatedSwitcher 

Source
pub struct AnimatedSwitcher {
    pub key: Option<KeyRef>,
    pub child: Option<WidgetRef>,
    pub duration: Duration,
    pub reverse_duration: Option<Duration>,
    pub switch_in_curve: Rc<dyn Curve>,
    pub switch_out_curve: Rc<dyn Curve>,
    pub transition_builder: AnimatedSwitcherTransitionBuilder,
    pub layout_builder: AnimatedSwitcherLayoutBuilder,
}
Expand description

A widget that by default does a cross-fade between a new widget and the widget previously set on the AnimatedSwitcher as a child.

If they are swapped fast enough (i.e. before duration elapses), more than one previous child can exist and be transitioning out while the newest one is transitioning in.

If the “new” child is the same widget type and key as the “old” child, but with different parameters, then AnimatedSwitcher will not do a transition between them, since as far as the framework is concerned, they are the same widget and the existing widget can be updated with the new parameters. To force the transition to occur, set a key on each child widget that you wish to be considered unique (typically a ValueKey on the widget data that distinguishes this child from the others).

The same key can be used for a new child as was used for an already-outgoing child; the two will not be considered related. (For example, if a progress indicator with key A is first shown, then an image with key B, then another progress indicator with key A again, all in rapid succession, then the old progress indicator and the image will be fading out while a new progress indicator is fading in.)

The type of transition can be changed from a cross-fade to a custom transition by setting the transition_builder.

See also:

  • AnimatedCrossFade, which only fades between two children, but also interpolates their sizes, and is reversible.
  • AnimatedOpacity, which can be used to switch between nothingness and a given child by fading the child in and out.
  • FadeTransition, which AnimatedSwitcher uses to perform the transition.

Fields§

§key: Option<KeyRef>§child: Option<WidgetRef>

The current child widget to display. If there was a previous child, then that child will be faded out using the switch_out_curve, while the new child is faded in with the switch_in_curve, over the duration.

If there was no previous child, then this child will fade in using the switch_in_curve over the duration.

The child is considered to be “new” if it has a different type or key (see Widget.canUpdate).

To change the kind of transition used, see transition_builder.

§duration: Duration

The duration of the transition from the old child value to the new one.

This duration is applied to the given child when that property is set to a new child. The same duration is used when fading out, unless reverse_duration is set. Changing duration will not affect the durations of transitions already in progress.

§reverse_duration: Option<Duration>

The duration of the transition from the new child value to the old one.

This duration is applied to the given child when that property is set to a new child. Changing reverse_duration will not affect the durations of transitions already in progress.

If not set, then the value of duration is used by default.

§switch_in_curve: Rc<dyn Curve>

The animation curve to use when transitioning in a new child.

This curve is applied to the given child when that property is set to a new child. Changing switch_in_curve will not affect the curve of a transition already in progress.

The switch_out_curve is used when fading out, except that if child is changed while the current child is in the middle of fading in, switch_in_curve will be run in reverse from that point instead of jumping to the corresponding point on switch_out_curve.

§switch_out_curve: Rc<dyn Curve>

The animation curve to use when transitioning a previous child out.

This curve is applied to the child when the child is faded in (or when the widget is created, for the first child). Changing switch_out_curve will not affect the curves of already-visible widgets, it only affects the curves of future children.

If child is changed while the current child is in the middle of fading in, switch_in_curve will be run in reverse from that point instead of jumping to the corresponding point on switch_out_curve.

§transition_builder: AnimatedSwitcherTransitionBuilder

A function that wraps a new child with an animation that transitions the child in when the animation runs in the forward direction and out when the animation runs in the reverse direction. This is only called when a new child is set (not for each build), or when a new transition_builder is set. If a new transition_builder is set, then the transition is rebuilt for the current child and all previous children using the new transition_builder.

The default is AnimatedSwitcher::default_transition_builder.

The animation provided to the builder has the duration and switch_in_curve or switch_out_curve applied as provided when the corresponding child was first provided.

See also:

§layout_builder: AnimatedSwitcherLayoutBuilder

A function that wraps all of the children that are transitioning out, and the child that’s transitioning in, with a widget that lays all of them out. This is called every time this widget is built.

The default is AnimatedSwitcher::default_layout_builder.

See also:

Implementations§

Source§

impl AnimatedSwitcher

Source

pub fn new(duration: Duration) -> AnimatedSwitcher

Creates an AnimatedSwitcher.

Source

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

Dart AnimatedSwitcher(key:).

Source

pub fn child<K>(self, child: impl IntoWidget<K>) -> AnimatedSwitcher

Dart AnimatedSwitcher(child:).

Source

pub fn reverse_duration(self, reverse_duration: Duration) -> AnimatedSwitcher

Dart AnimatedSwitcher(reverseDuration:).

Source

pub fn switch_in_curve(self, switch_in_curve: Rc<dyn Curve>) -> AnimatedSwitcher

Dart AnimatedSwitcher(switchInCurve:).

Source

pub fn switch_out_curve( self, switch_out_curve: Rc<dyn Curve>, ) -> AnimatedSwitcher

Dart AnimatedSwitcher(switchOutCurve:).

Source

pub fn transition_builder( self, transition_builder: AnimatedSwitcherTransitionBuilder, ) -> AnimatedSwitcher

Dart AnimatedSwitcher(transitionBuilder:).

Source

pub fn layout_builder( self, layout_builder: impl Fn(&mut App, Option<&WidgetRef>, &[WidgetRef]) -> WidgetRef + 'static, ) -> AnimatedSwitcher

Dart AnimatedSwitcher(layoutBuilder:).

Source

pub fn default_transition_builder( _app: &mut App, child: &WidgetRef, animation: AnimationRef<f64>, ) -> WidgetRef

The transition builder used as the default value of transition_builder.

The new child is given a FadeTransition which increases opacity as the animation goes from 0.0 to 1.0, and decreases when the animation is reversed.

This is an AnimatedSwitcherTransitionBuilder function.

Source

pub fn default_layout_builder( _app: &mut App, current_child: Option<&WidgetRef>, previous_children: &[WidgetRef], ) -> WidgetRef

The layout builder used as the default value of layout_builder.

The new child is placed in a Stack that sizes itself to match the largest of the child or a previous child. The children are centered on each other.

This is an AnimatedSwitcherLayoutBuilder function.

Trait Implementations§

Source§

impl Debug for AnimatedSwitcher

Source§

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

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

impl StatefulWidget for AnimatedSwitcher

Source§

type State = AnimatedSwitcherState

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) -> AnimatedSwitcherState

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 AnimatedSwitcher

§

impl !Send for AnimatedSwitcher

§

impl !Sync for AnimatedSwitcher

§

impl !UnwindSafe for AnimatedSwitcher

§

impl Forget for AnimatedSwitcher

§

impl Freeze for AnimatedSwitcher
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze, *const RcInner<dyn Curve> is !null: Freeze, PhantomData<RcInner<dyn Curve>>: Freeze, *const RcInner<dyn Fn(&mut App, &Rc<dyn Widget>, Retained<AnyAnimation<f64>>) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, &Rc<dyn Widget>, Retained<AnyAnimation<f64>>) -> Rc<dyn Widget>>>: Freeze, *const (): Freeze, NonZero<u32>: Freeze, *const RcInner<dyn Fn(&mut App, Option<&Rc<dyn Widget>>, &[Rc<dyn Widget>]) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, Option<&Rc<dyn Widget>>, &[Rc<dyn Widget>]) -> Rc<dyn Widget>>>: Freeze,

§

impl Move for AnimatedSwitcher

§

impl Unpin for AnimatedSwitcher
where Rc<dyn Key>: Unpin, Rc<dyn Widget>: Unpin, Rc<dyn Curve>: Unpin, Rc<dyn Fn(&mut App, &Rc<dyn Widget>, Retained<AnyAnimation<f64>>) -> Rc<dyn Widget>>: Unpin, *const (): Unpin, NonZero<u32>: Unpin, Rc<dyn Fn(&mut App, Option<&Rc<dyn Widget>>, &[Rc<dyn Widget>]) -> Rc<dyn Widget>>: Unpin,

§

impl UnsafeUnpin for AnimatedSwitcher
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Widget>>: UnsafeUnpin, *const RcInner<dyn Curve> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Curve>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, &Rc<dyn Widget>, Retained<AnyAnimation<f64>>) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, &Rc<dyn Widget>, Retained<AnyAnimation<f64>>) -> Rc<dyn Widget>>>: UnsafeUnpin, *const (): UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, Option<&Rc<dyn Widget>>, &[Rc<dyn Widget>]) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, Option<&Rc<dyn Widget>>, &[Rc<dyn Widget>]) -> Rc<dyn Widget>>>: 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>