Skip to main content

ListenableBuilder

Struct ListenableBuilder 

Source
pub struct ListenableBuilder {
    pub key: Option<KeyRef>,
    pub listenable: Rc<dyn Listenable>,
    pub builder: TransitionBuilder,
    pub child: Option<WidgetRef>,
}
Expand description

A general-purpose widget for building a widget subtree when a Listenable changes.

ListenableBuilder is useful for more complex widgets that wish to listen to changes in other objects as part of a larger build function. To use ListenableBuilder, construct the widget and pass it a builder function.

Any subtype of Listenable (such as a ChangeNotifier, ValueNotifier, or Animation) can be used with a ListenableBuilder to rebuild only certain parts of a widget when the Listenable notifies its listeners. Although they have identical implementations, if an Animation is being listened to, consider using an AnimatedBuilder instead for better readability.

§Performance optimizations

If the builder function contains a subtree that does not depend on the listenable, it is more efficient to build that subtree once instead of rebuilding it on every change of the listenable.

Performance is optimized by building the subtree once and passing it as the child; the builder receives it as its third argument.

Fields§

§key: Option<KeyRef>§listenable: Rc<dyn Listenable>

The Listenable supplied to the constructor.

Also accessible through animation for an AnimatedBuilder.

§builder: TransitionBuilder

Called every time the listenable notifies about a change.

The child given to the builder should typically be part of the returned widget tree.

§child: Option<WidgetRef>

The child widget to pass to the builder.

If a builder callback’s return value contains a subtree that does not depend on the listenable, it’s more efficient to build that subtree once instead of rebuilding it on every change of the listenable.

If the pre-built subtree is passed as the child parameter, the ListenableBuilder will pass it back to the builder function so that it can be incorporated into the build.

Using this pre-built child is entirely optional, but can improve performance significantly in some cases and is therefore a good practice.

Implementations§

Source§

impl ListenableBuilder

Source

pub fn new( listenable: Rc<dyn Listenable>, builder: impl Fn(&mut App, BuildContext, Option<&WidgetRef>) -> WidgetRef + 'static, ) -> ListenableBuilder

Creates a builder that responds to changes in listenable.

Source

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

Dart ListenableBuilder(key:).

Source

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

Dart ListenableBuilder(child:).

Source

pub fn animation(&self) -> Rc<dyn Listenable>

The Listenable supplied to the constructor (Dart’s AnimatedBuilder.animation).

Trait Implementations§

Source§

impl AnimatedWidget for ListenableBuilder

Source§

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

Source§

fn listenable(&self) -> Rc<dyn Listenable>

The Listenable to which this widget is listening. Read more
Source§

fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef

Override this method to build widgets that depend on the state of the listenable (e.g., the current value of the animation).
Source§

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

Dart’s AnimatedWidget.debugFillProperties, which a subclass’s own body calls where Dart writes super.
Source§

impl Debug for ListenableBuilder

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for ListenableBuilder

§

impl !Send for ListenableBuilder

§

impl !Sync for ListenableBuilder

§

impl !UnwindSafe for ListenableBuilder

§

impl Forget for ListenableBuilder

§

impl Freeze for ListenableBuilder
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn Listenable> is !null: Freeze, PhantomData<RcInner<dyn Listenable>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze,

§

impl Move for ListenableBuilder

§

impl Unpin for ListenableBuilder
where Rc<dyn Key>: Unpin, Rc<dyn Listenable>: Unpin, Rc<dyn Fn(&mut App, AnyElement, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>: Unpin, Rc<dyn Widget>: Unpin,

§

impl UnsafeUnpin for ListenableBuilder
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Listenable> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Listenable>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<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<AnimatedKind> for W
where W: AnimatedWidget,

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>