Transitions and Hero
FadeTransition
,
RotationTransition
,
ScaleTransition
,
SlideTransition
— opacity, rotation, scale and offset of the child. Despite the name,
they do not animate by themselves
: the value is applied at once, as with
Opacity
or
Transform
. Motion comes when the value is driven by
`Animated`
— it changes a variable smoothly, and the transition reads it.
Hero
is something else: an element shared by two screens that “flies” from one place to the other during the transition between them.
@shown:false
Animated of:{shown ? 1 : 0} value:@k duration:400 curve:easeOutCubic
SlideTransition x:0 y:{(1 - k) * 0.3}
FadeTransition opacity:$k
Text "I slide in from below"
FadeTransition
The child's opacity.
Type:
single-child
FadeTransition opacity:0.5
Text "Semi-transparent"
| Parameter | Type | Default | Description |
|---|---|---|---|
opacity
|
double | 1.0 | From 0 (invisible) to 1 |
RotationTransition
Rotation of the child in fractions of a turn:
0.25
is a quarter,
1
is a full turn.
Type:
single-child
RotationTransition turns:0.125
Icon star color:0xFFF59E0B
| Parameter | Type | Default | Description |
|---|---|---|---|
turns
|
double | 0 | Rotation in turns, clockwise |
alignment
|
alignment |
center
|
The point to rotate around:
center
,
topLeft
, …
|
ScaleTransition
The child's scale. Its place in the layout does not change — the child is only drawn larger or smaller.
Type:
single-child
ScaleTransition scale:1.2 alignment:center
Text "Larger"
| Parameter | Type | Default | Description |
|---|---|---|---|
scale
|
double | 1.0 |
Factor:
1
— as is,
0.5
— half the size
|
alignment
|
alignment |
center
|
The point to scale from |
SlideTransition
Offset of the child in fractions of
its own
size:
x:1
— its full width to the right,
y:-0.5
— half its height up. Its place in the layout does not change.
Type:
single-child
SlideTransition x:0.5 y:0
Text "Shifted by half its width"
| Parameter | Type | Default | Description |
|---|---|---|---|
x
|
double | 0 | Horizontal offset, in child widths |
y
|
double | 0 | Vertical offset, in child heights |
Smooth appearance
Animated
drives
k
from 0 to 1 on every press, and the transitions read
k
: the block slides in from below, fades in and grows a little.
@open:false
Column spacing:12
FilledButton onPressed:{open = !open}
Text "Show / hide"
Animated of:{open ? 1 : 0} value:@k duration:400 curve:easeOutCubic
SlideTransition x:0 y:{(1 - k) * 0.5}
FadeTransition opacity:$k
ScaleTransition scale:{0.8 + k * 0.2}
ColoredBox color:0xFF4F46E5
SizedBox width:200 height:72
Center
Text "Hello"
#style color:0xFFFFFFFF
Hero
An element shared by two screens. If both the old and the new screen have a
Hero
with the same
tag
, during the transition between them this element flies from its old place to the new one — for example, an avatar from a list becomes a large photo in the profile.
Type:
single-child
Hero tag:"avatar-42"
CircleAvatar radius:24 backgroundColor:0xFF4F46E5
Text "AB"
#style color:0xFFFFFFFF
| Parameter | Type | Default | Description |
|---|---|---|---|
| (positional 0) | string | — |
The tag — same as
tag:
:
Hero "avatar-42"
|
tag
|
string |
""
|
The pair's shared tag. Tags on one screen must be different |
// Profile screen: the same tag, a different size
Hero "avatar-42"
CircleAvatar radius:64 backgroundColor:0xFF4F46E5
Text "AB"
#style color:0xFFFFFFFF
Differences between the app and the site
-
FadeTransition,ScaleTransition,SlideTransitionare drawn the same way on the site and follow the state (includingAnimated). -
ScaleTransitionandRotationTransitionon the site do not readalignment: scale and rotation are always from the centre. On the siteRotationTransitionalso shrinks in width to its child. -
RotationTransitionwithturnsthat depends on state (turns:{…}) does not rotate on the site at all. A constant number (turns:0.125) works. For a rotation that changes, use Transform on the site. -
Heroon the site draws only its child: there is no flight between pages.
Bad practices
Expecting animation from
*Transition
// BAD — the value jumps: FadeTransition does not animate by itself
@shown:false
FadeTransition opacity:{shown ? 1 : 0}
Text "Hello"
// GOOD — smoothness comes from Animated (or AnimatedOpacity)
@shown:false
AnimatedOpacity opacity:{shown ? 1 : 0} duration:300ms curve:easeOut
Text "Hello"
See also
- Animated and Ticker — what drives a value over time
-
Implicit animations
—
AnimatedOpacity,AnimatedContainerand others - Transform — rotation, scale, offset, 3D
- Opacity — opacity without animation