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, theVisibility.maintainconstructor is equivalent to using an opacity widget with values of0.0or1.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: f64The 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§
Trait Implementations§
Source§impl RenderObjectWidget for Opacity
impl RenderObjectWidget for Opacity
Source§type RenderObject = RenderOpacity
type RenderObject = RenderOpacity
covariant RenderObject parameter type.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_render_object(
&self,
app: &mut App,
_context: BuildContext,
) -> AnyRenderObject
fn create_render_object( &self, app: &mut App, _context: BuildContext, ) -> AnyRenderObject
RenderObject class that this widget represents, using the
configuration described by this widget. Read moreSource§fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderOpacity>,
)
fn update_render_object( &self, app: &mut App, _context: BuildContext, render_object: RenderHandle<RenderOpacity>, )
create_render_object. Read moreSource§fn to_string_short(&self) -> String
fn to_string_short(&self) -> String
toStringShort, for a widget that names itself apart from its type, as
SizedBox.expand does; see Widget::to_string_short.Source§fn debug_visit_onstage_children(
&self,
app: &App,
children: &[AnyElement],
visitor: &mut dyn FnMut(AnyElement),
)
fn debug_visit_onstage_children( &self, app: &App, children: &[AnyElement], visitor: &mut dyn FnMut(AnyElement), )
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>,
)
fn did_unmount_render_object( &self, app: &mut App, render_object: RenderHandle<Self::RenderObject>, )
RenderObject will be of the same type as returned by this object’s
create_render_object.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
impl Move for Opacity
impl Unpin for Opacity
impl UnsafeUnpin for Opacitywhere
*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> 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<SingleChildRenderObjectKind> for Wwhere
W: SingleChildRenderObjectWidget,
<W as RenderObjectWidget>::RenderObject: RenderObjectWithChildMixin,
impl<W> IntoWidget<SingleChildRenderObjectKind> for Wwhere
W: SingleChildRenderObjectWidget,
<W as RenderObjectWidget>::RenderObject: RenderObjectWithChildMixin,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.