Switcher
Conditional rendering — picks one child to show, by matching its
id:
against the value of a variable.
Type:
multi-child
Switcher $pageId
Column id:email
Text "Sign in"
Column id:register
Text "Sign up"
How it works
Switcher
takes the first
positional
parameter (the value to compare) and looks among its children for the one whose
id:
matches that value. If nothing matches, the
first
child is shown (the fallback).
Switcher $variable
FirstChild ← shown if no id matched (default)
Element id:value1 ← shown if $variable == "value1"
Element id:value2 ← shown if $variable == "value2"
Parameters:
| Parameter | Type | Description |
|---|---|---|
| (positional 0) | any |
The value compared with the children's
id:
|
Examples
Switching pages
State @pageId:email
Switcher $pageId
// Sign-in page
Column id:email spacing:82 mainAxisAlignment:center
Text "Sign in"
TextFormField name:"email"
#decoration hintText:"example@email.com"
ElevatedButton onPressed:"form:submit"
Text "Get a code"
// Sign-up page
Column id:register spacing:82 mainAxisAlignment:center
Text "Sign up"
TextFormField name:"name"
#decoration hintText:"Name"
TextFormField name:"email"
#decoration hintText:"example@email.com"
ElevatedButton onPressed:"form:submit"
Text "Get a code"
Fallback (first child = default)
State @status:initial
Switcher $status
// Default — shown for status=initial or any unknown value
SizedBox
// Shown for status=loading
Text id:loading "Loading..."
// Shown for status=error
Text id:error "Something went wrong"
Switcher inside a list itemBuilder
Use a Switcher inside
itemBuilder
when the list rows are
structurally different
(different widgets at different positions):
CustomScrollView
SliverList.builder itemCount:7
@Macros id:itemBuilder index:0
Switcher $index
// Default — a regular card
$CategoryChip "Category"
// The first item is the filters button (structurally different)
DecoratedBox id:0
#decoration color:$primaryColor
#borderRadius circular 60
Center
Row spacing:12
Icon filter_list size:24
Text "Filters"
$CategoryChip id:1 "Movies"
$CategoryChip id:2 "Series"
Bad practices
1. A named parameter instead of the positional one
// BAD — value: is a named parameter, and Switcher reads only the positional one
Switcher value:$status
Text id:ok "OK"
// GOOD — positional parameter
Switcher $status
Text id:ok "OK"
2. Switcher for items of the same kind
If all the items are of one kind and differ only in data, you do not need a Switcher. Keep the data separately and take the fields by index.
// BAD — the template is duplicated for every item
@Macros id:itemBuilder index:0
Switcher $index
$Card id:0 title:"First" badge:"4K"
$Card id:1 title:"Second" badge:"HDR"
$Card id:2 title:"Third" badge:"New"
// GOOD — data separately, one template
// data/cards.json: [{"title":"First","badge":"4K"}, {"title":"Second","badge":"HDR"}, …]
JsonData src:"data/cards.json" data:@cards count:@count
CustomScrollView id:ready
SliverList.builder itemCount:$count
@Macros id:itemBuilder index:0
$Card title:{cards[index]['title']} badge:{cards[index]['badge']}
Adaptive layout with
$theme.sizeClass
Switcher
is the standard way to change the markup by screen size: it is driven by the ambient variable
$theme.sizeClass
(
compact
/
medium
/
expanded
, computed from the window width). Children have
id:
= the class name; no match → the first child.
// Navigation: a burger column on mobile, a row on desktop
Switcher $theme.sizeClass
Column id:compact
$MobileNav
Row id:expanded
$DesktopNav
The size-class thresholds are set in the Brand Kit (
breakpoints
, defaults
compact:0, medium:600, expanded:1240
). In the app the Switcher rebuilds when the class changes; on the site all branches are on the page, and
@media
switches which one is shown. For responsive
spacing
without changing the markup use per-class tokens:
spacing:$theme.spacing.<name>
.
Best practices
-
Positional parameter
:
Switcher $variable(notvalue:$variable) -
Children with
id:: every variant is a child withid:value -
First child = default
: if no
id:matched, the first child is rendered -
Only for structurally different items
: if the items are of one kind with different data, keep the data separately (
JsonData) and take the fields by index