Skip to main content

Opacity

Struct Opacity 

Source
pub struct Opacity {
    pub key: Option<KeyRef>,
    pub opacity: f64,
    pub child: Option<WidgetRef>,
}
Expand description

A widget that makes its child partially transparent.

This class paints its child into an intermediate buffer and then blends the child back into the scene partially transparent.

For values of opacity other than 0.0 and 1.0, this class is relatively expensive because it requires painting the child into an intermediate buffer. For the value 0.0, the child is not painted at all. For the value 1.0, the child is painted immediately without an intermediate buffer.

The presence of the intermediate buffer which has a transparent background by default may cause some child widgets to behave differently. For example a BackdropFilter child will only be able to apply its filter to the content between this widget and the backdrop child and may require adjusting the BackdropFilter.blendMode property to produce the desired results.

This example shows some Text when the visible member field is true, and hides it when it is false:

Opacity {
  opacity: if visible { 1.0 } else { 0.0 },
  child: Some(Text("Now you see me, now you don't!")),
  ..Default::default()
}

This is more efficient than adding and removing the child widget from the tree on demand.

§Performance considerations for opacity animation

Animating an Opacity widget directly causes the widget (and possibly its subtree) to rebuild each frame, which is not very efficient. Consider using one of these alternative widgets instead:

  • AnimatedOpacity, which uses an animation internally to efficiently animate opacity.
  • FadeTransition, which uses a provided animation to efficiently animate opacity.

§Transparent image

If only a single Image or Color needs to be composited with an opacity between 0.0 and 1.0, it’s much faster to directly use them without Opacity widgets.

For example, Container(color: Color.fromRGBO(255, 0, 0, 0.5)) is much faster than Opacity(opacity: 0.5, child: Container(color: Colors.red)).

Directly drawing an Image or Color with opacity is faster than using Opacity on top of them because Opacity could apply the opacity to a group of widgets and therefore a costly offscreen buffer will be used. Drawing content into the offscreen buffer may also trigger render target switches and such switching is particularly slow in older GPUs.

§Hit testing

Setting the opacity to zero does not prevent hit testing from being applied to the descendants of the Opacity widget. This can be confusing for the user, who may not see anything, and may believe the area of the interface where the Opacity is hiding a widget to be non-interactive.

With certain widgets, such as Flow, that compute their positions only when they are painted, this can actually lead to bugs (from unexpected geometry to exceptions), because those widgets are not painted by the Opacity widget at all when the opacity is zero.

To avoid such problems, it is generally a good idea to use an IgnorePointer widget when setting the opacity to zero. This prevents interactions with any children in the subtree.

See also:

  • Visibility, which can hide a child more efficiently (albeit less subtly, because it is either visible or hidden, rather than allowing fractional opacity values). Specifically, the Visibility.maintain constructor is equivalent to using an opacity widget with values of 0.0 or 1.0.
  • ShaderMask, which can apply more elaborate effects to its child.
  • Transform, which applies an arbitrary transform to its child widget at paint time.
  • SliverOpacity, the sliver version of this widget.

Flutter’s alwaysIncludeSemantics waits on accessibility.

Fields§

§key: Option<KeyRef>§opacity: f64

The fraction to scale the child’s alpha value.

An opacity of one is fully opaque. An opacity of zero is fully transparent (i.e., invisible).

Values one and zero are painted with a fast path. Other values require painting the child into an intermediate buffer, which is expensive.

Must be between zero and one, inclusive.

§child: Option<WidgetRef>

Implementations§

Source§

impl Opacity

Source

pub fn new(opacity: f64) -> Opacity

Creates a Opacity; Dart’s optional named arguments are the setters.

Source

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

Dart Opacity(key:).

Source

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

Dart Opacity(child:).

Trait Implementations§

Source§

impl Debug for Opacity

Source§

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

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

impl RenderObjectWidget for Opacity

Source§

type RenderObject = RenderOpacity

The render object this widget creates; Dart’s covariant RenderObject parameter type.
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_render_object( &self, app: &mut App, _context: BuildContext, ) -> AnyRenderObject

Creates an instance of the RenderObject class that this widget represents, using the configuration described by this widget. Read more
Source§

fn update_render_object( &self, app: &mut App, _context: BuildContext, render_object: RenderHandle<RenderOpacity>, )

Copies the configuration described by this widget to the given render object, which will be of the same type as returned by this object’s create_render_object. Read more
Source§

fn to_string_short(&self) -> String

Dart’s toStringShort, for a widget that names itself apart from its type, as SizedBox.expand does; see Widget::to_string_short.
Source§

fn debug_check_constructor(&self)

Source§

fn debug_visit_onstage_children( &self, app: &App, children: &[AnyElement], visitor: &mut dyn FnMut(AnyElement), )

Dart’s debugVisitOnstageChildren of the element this widget makes, for a widget whose Dart element subclass overrides it only to hide children (_OffstageElement, _IndexedStackElement): calls visitor for each of children, in order, that is onstage. Waft’s generic single- and multi-child elements stand in for those subclasses and ask their widget here; an element of a widget’s own overrides Element::debug_visit_onstage_children instead.
Source§

fn did_unmount_render_object( &self, app: &mut App, render_object: RenderHandle<Self::RenderObject>, )

A render object previously associated with this widget has been removed from the tree. The given RenderObject will be of the same type as returned by this object’s create_render_object.
Source§

impl SingleChildRenderObjectWidget for Opacity

Source§

fn child(&self) -> Option<&WidgetRef>

The widget below this widget in the tree.
Source§

fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement
where Self: Sized,

Creates the element that manages this widget’s child. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Opacity

§

impl !Send for Opacity

§

impl !Sync for Opacity

§

impl !UnwindSafe for Opacity

§

impl Forget for Opacity

§

impl Freeze for Opacity
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,

§

impl Move for Opacity

§

impl Unpin for Opacity
where Rc<dyn Key>: Unpin, Rc<dyn Widget>: Unpin,

§

impl UnsafeUnpin for Opacity
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,

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<SingleChildRenderObjectKind> for W

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>