Visibility
Three ways to hide part of the screen. They differ in what happens to the hidden part: whether it takes up space and whether its state stays alive (typed text, scroll position, animation).
| Element | Space of the hidden part | State of the hidden part |
|---|---|---|
| Visibility |
none (or kept with
maintainSize
)
|
lost (or kept with
maintainState
)
|
| Offstage | none | kept, animations stopped |
| IndexedStack | a box the size of the largest child | kept for all, animations of hidden ones stopped |
If the element is simply not needed, an
if:
condition on the element itself is simpler: a hidden
Visibility
remains a child, and the parent's
spacing
still leaves a gap for it.
Visibility
Shows or hides its child. By default a hidden child is removed entirely: it takes no space and loses its state.
Type:
single-child
@open:false
@note:""
Column
TextButton onPressed:{open = !open}
Text "Comment"
Visibility visible:$open maintainState:true
TextField value:$note onChanged:(value){note = value}
| Parameter | Type | Default | Description |
|---|---|---|---|
visible
|
bool | true | Whether to show the child |
maintainState
|
bool | false | Keep the hidden child alive: typed text and scroll position are preserved |
maintainAnimation
|
bool | false |
Do not stop the hidden child's animations. Requires
maintainState
|
maintainSize
|
bool | false |
Keep the hidden child's space (empty). Requires
maintainAnimation
and
maintainState
|
maintainInteractivity
|
bool | false |
Accept taps on the hidden child. Requires
maintainSize
|
maintainSemantics
|
bool | false |
Keep the hidden child visible to the screen reader. Requires
maintainSize
|
The
maintain*
flags stack in steps, as in Flutter: each next one only makes sense together with the previous ones.
@ready:false
Visibility visible:$ready maintainSize:true maintainAnimation:true maintainState:true
Text "The space under the text is taken while it is hidden"
Offstage
Lays out its child but does not draw it and does not let taps through to it. The child's state is alive, but it is
muted
: animations are stopped, a
Video
inside is paused.
Type:
single-child
Offstage offstage:true
Text "Hidden"
| Parameter | Type | Default | Description |
|---|---|---|---|
offstage
|
bool | true | Whether to hide the child. Note: by default it hides |
IndexedStack
Shows one child out of several and
keeps the rest alive
. That is the difference from
Visibility
: a tab you return to still has its typed text and scroll position. Hidden children are alive but muted — animations are stopped, a
Video
is paused.
Type:
multi-child
@tab:0
Column spacing:12
Row spacing:8
TextButton onPressed:{tab = 0}
Text "First"
TextButton onPressed:{tab = 1}
Text "Second"
IndexedStack index:$tab
Text "Contents of the first tab"
Text "Contents of the second tab"
| Parameter | Type | Default | Description |
|---|---|---|---|
index
|
int | 0 | Index of the shown child, from zero. An index outside the list shows nothing (no error) |
alignment
|
alignment | topStart | How the children sit inside the box |
sizing
|
string | loose |
Size of the children:
loose
(their own),
expand
(the whole box),
passthrough
(the parent's constraints as they are)
|
clipBehavior
|
string | hardEdge |
Whether to clip what goes past the box:
none
,
hardEdge
,
antiAlias
,
antiAliasWithSaveLayer
|
The box size follows the largest child, not the shown one: switching tabs does not move what is below.
Differences between the app and the site
-
Visibilitywith a variable invisibletoggles live on the site (display). With a constantfalsethe hidden part does not get into the page.maintainSizeand the othermaintain*flags are not read on the site: the hidden part leaves no space. -
Offstageon the site is decided once, when the page is built : a variable inoffstageis taken at its initial value and does not toggle visibility afterwards. To toggle on the site, useVisibilityorIndexedStack. -
IndexedStackon the site: all children lie in one grid cell, the shown one is opaque, the rest are transparent and do not accept taps; switching happens live on theindexvariable.alignmentandsizingare not read (children sit at the start).clipBehavior:nonegivesoverflow: visible.indexmust be a plain variable ($tab): an expression inindexdoes not switch on the site.