Skip to main content

Visibility

Struct Visibility 

Source
pub struct Visibility {
    pub key: Option<KeyRef>,
    pub child: WidgetRef,
    pub replacement: WidgetRef,
    pub visible: bool,
    pub maintain_state: bool,
    pub maintain_animation: bool,
    pub maintain_size: bool,
    pub maintain_semantics: bool,
    pub maintain_interactivity: bool,
    pub maintain_focusability: bool,
}
Expand description

Whether to show or hide a child.

By default, the visible property controls whether the child is included in the subtree or not; when it is not visible, the replacement child (typically a zero-sized box) is included instead.

A variety of flags can be used to tweak exactly how the child is hidden. (Changing the flags dynamically is discouraged, as it can cause the child subtree to be rebuilt, with any state in the subtree being discarded. Typically, only the visible flag is changed dynamically.)

These widgets provide some of the facets of this one:

  • Opacity, which can stop its child from being painted.
  • Offstage, which can stop its child from being laid out or painted.
  • TickerMode, which can stop its child from being animated.
  • ExcludeSemantics, which can hide the child from accessibility tools.
  • IgnorePointer, which can disable touch interactions with the child.

Using this widget is not necessary to hide children. The simplest way to hide a child is just to not include it, or, if a child must be given (e.g. because the parent is a stateless widget) then to use SizedBox::shrink instead of the child that would otherwise be included.

See also:

  • AnimatedSwitcher, which can fade from one child to the next as the subtree changes.
  • AnimatedCrossFade, which can fade between two specific children.
  • SliverVisibility, the sliver equivalent of this widget.

Fields§

§key: Option<KeyRef>§child: WidgetRef

The widget to show or hide, as controlled by visible.

§replacement: WidgetRef

The widget to use when the child is not visible, assuming that none of the maintain flags (in particular, maintain_state) are set.

The normal behavior is to replace the widget with a zero by zero box (SizedBox::shrink).

See also:

  • AnimatedCrossFade, which can animate between two children.
§visible: bool

Switches between showing the child or hiding it.

The maintain flags should be set to the same values regardless of the state of the visible property, otherwise they will not operate correctly (specifically, the state will be lost regardless of the state of maintain_state whenever any of the maintain flags are changed, since doing so will result in a subtree shape change).

Unless maintain_state is set, the child subtree will be disposed (removed from the tree) while hidden.

§maintain_state: bool

Whether to maintain the State objects of the child subtree when it is not visible.

Keeping the state of the subtree is potentially expensive (because it means all the objects are still in memory; their resources are not released). It should only be maintained if it cannot be recreated on demand. One example of when the state would be maintained is if the child subtree contains a Navigator, since that widget maintains elaborate state that cannot be recreated on the fly.

If this property is true, an Offstage widget is used to hide the child instead of replacing it with replacement.

If this property is false, then maintain_animation must also be false.

If this property is false, then maintain_focusability must also be false.

Dynamically changing this value may cause the current state of the subtree to be lost (and a new instance of the subtree, with new State objects, to be immediately created if visible is true).

§maintain_animation: bool

Whether to maintain animations within the child subtree when it is not visible.

To set this, maintain_state must also be set.

Keeping animations active when the widget is not visible is even more expensive than only maintaining the state.

One example when this might be useful is if the subtree is animating its layout in time with an AnimationController, and the result of that layout is being used to influence some other logic. If this flag is false, then any AnimationControllers hosted inside the child subtree will be muted while the visible flag is false.

If this property is true, no TickerMode widget is used.

If this property is false, then maintain_size must also be false.

Dynamically changing this value may cause the current state of the subtree to be lost (and a new instance of the subtree, with new State objects, to be immediately created if visible is true).

§maintain_size: bool

Whether to maintain space for where the widget would have been.

To set this, maintain_animation and maintain_state must also be set.

Maintaining the size when the widget is not visible is not notably more expensive than just keeping animations running without maintaining the size, and may in some circumstances be slightly cheaper if the subtree is simple and the visible property is frequently toggled, since it avoids triggering a layout change when the visible property is toggled. If the child subtree is not trivial then it is significantly cheaper to not even keep the state (see maintain_state).

If this property is false, Offstage is used.

If this property is false, then maintain_semantics and maintain_interactivity must also be false.

Dynamically changing this value may cause the current state of the subtree to be lost (and a new instance of the subtree, with new State objects, to be immediately created if visible is true).

See also:

  • AnimatedOpacity and FadeTransition, which apply animations to the opacity for a more subtle effect.
§maintain_semantics: bool

Whether to maintain the semantics for the widget when it is hidden (e.g. for accessibility).

To set this, maintain_size must also be set.

By default, with maintain_semantics set to false, the child is not visible to accessibility tools when it is hidden from the user. If this flag is set to true, then accessibility tools will report the widget as if it was present.

Nothing reads this flag while accessibility is deferred (see PORTING.md).

§maintain_interactivity: bool

Whether to allow the widget to be interactive when hidden.

To set this, maintain_size must also be set.

By default, with maintain_interactivity set to false, touch events cannot reach the child when it is hidden from the user. If this flag is set to true, then touch events will nonetheless be passed through.

§maintain_focusability: bool

Whether to allow the widget to receive focus when hidden. Only in effect if visible is false.

To set this to true, maintain_state must also be set to true.

By default, with maintain_focusability set to false, focus events cannot reach the child when this widget is not visible because an ExcludeFocus widget is used to exclude the child subtree from the focus tree. If this flag is set to true, then focus events will reach the child subtree.

Implementations§

Source§

impl Visibility

Source

pub fn new<K>(child: impl IntoWidget<K>) -> Visibility

Control whether the given child is visible; Dart’s optional named arguments are the setters.

The maintain_semantics and maintain_interactivity flags can only be set if maintain_size is set.

The maintain_size flag can only be set if maintain_animation is set.

The maintain_animation flag can only be set if maintain_state is set.

Source

pub fn maintain<K>(child: impl IntoWidget<K>) -> Visibility

Control whether the given child is visible.

This is equivalent to Visibility::new with all “maintain” fields set to true. This constructor should be used in place of an Opacity widget that only takes on values of 0.0 or 1.0, as it avoids extra compositing when fully opaque.

Source

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

Dart Visibility(key:).

Source

pub fn replacement<K>(self, replacement: impl IntoWidget<K>) -> Visibility

Dart Visibility(replacement:).

Source

pub fn visible(self, visible: bool) -> Visibility

Dart Visibility(visible:).

Source

pub fn maintain_state(self, maintain_state: bool) -> Visibility

Dart Visibility(maintainState:).

Source

pub fn maintain_animation(self, maintain_animation: bool) -> Visibility

Dart Visibility(maintainAnimation:).

Source

pub fn maintain_size(self, maintain_size: bool) -> Visibility

Dart Visibility(maintainSize:).

Source

pub fn maintain_semantics(self, maintain_semantics: bool) -> Visibility

Dart Visibility(maintainSemantics:).

Source

pub fn maintain_interactivity(self, maintain_interactivity: bool) -> Visibility

Dart Visibility(maintainInteractivity:).

Source

pub fn maintain_focusability(self, maintain_focusability: bool) -> Visibility

Dart Visibility(maintainFocusability:).

Source

pub fn of(app: &mut App, context: BuildContext) -> bool

Tells the visibility state of an element in the tree based off its ancestor Visibility elements.

If there’s one or more Visibility widgets in the ancestor tree, this will return true if and only if all of those widgets have visible set to true. If there is no Visibility widget in the ancestor tree of the specified build context, this will return true.

This will register a dependency from the specified context on any Visibility elements in the ancestor tree, such that if any of their visibilities changes, the specified context will be rebuilt.

Trait Implementations§

Source§

impl Debug for Visibility

Source§

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

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

impl StatelessWidget for Visibility

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 key(&self) -> Option<&KeyRef>

Source§

fn debug_check_constructor(&self)

Dart’s constructor asserts that relate two or more arguments. Read more
Source§

fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef

Describes the part of the user interface represented by this widget. Read more
Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Dart’s this is PreferredSizeWidget: a widget that implements PreferredSizeWidget answers with itself, which is what a parent asking an erased child for its preferred size reads.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Visibility

§

impl !Send for Visibility

§

impl !Sync for Visibility

§

impl !UnwindSafe for Visibility

§

impl Forget for Visibility

§

impl Freeze for Visibility
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 Visibility

§

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

§

impl UnsafeUnpin for Visibility
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<StatelessKind> for W
where W: StatelessWidget,

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>