text
stringlengths
1
474
/// this value is set to the value of the [_animation].
Alignment _dragAlignment = Alignment.center;
late Animation<Alignment> _animation;
/// Calculates and runs a [SpringSimulation].
void _runAnimation(Offset pixelsPerSecond, Size size) {
_animation = _controller.drive(
AlignmentTween(
begin: _dragAlignment,
end: Alignment.center,
),
);
// Calculate the velocity relative to the unit interval, [0,1],
// used by the animation controller.
final unitsPerSecondX = pixelsPerSecond.dx / size.width;
final unitsPerSecondY = pixelsPerSecond.dy / size.height;
final unitsPerSecond = Offset(unitsPerSecondX, unitsPerSecondY);
final unitVelocity = unitsPerSecond.distance;
const spring = SpringDescription(
mass: 30,
stiffness: 1,
damping: 1,
);
final simulation = SpringSimulation(spring, 0, 1, -unitVelocity);
_controller.animateWith(simulation);
}
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this);
_controller.addListener(() {
setState(() {
_dragAlignment = _animation.value;
});
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return GestureDetector(
onPanDown: (details) {
_controller.stop();
},
onPanUpdate: (details) {
setState(() {
_dragAlignment += Alignment(
details.delta.dx / (size.width / 2),
details.delta.dy / (size.height / 2),
);
});
},
onPanEnd: (details) {
_runAnimation(details.velocity.pixelsPerSecond, size);
},
child: Align(
alignment: _dragAlignment,
child: Card(
child: widget.child,
),
),
);
}
}<code_end>
<topic_end>
<topic_start>Staggered animations
<topic_end>
<topic_start>What you'll learn
Terminology:
If the concept of tweens or tweening is new to you, see the
Animations in Flutter tutorial.Staggered animations are a straightforward concept: visual changes
happen as a series of operations, rather than all at once.
The animation might be purely sequential, with one change occurring after
the next, or it might partially or completely overlap. It might also
have gaps, where no changes occur.This guide shows how to build a staggered animation in Flutter.<topic_end>
<topic_start>Examples
This guide explains the basic_staggered_animation example.
You can also refer to a more complex example,
staggered_pic_selection.The following video demonstrates the animation performed by
basic_staggered_animation:In the video, you see the following animation of a single widget,
which begins as a bordered blue square with slightly rounded corners.
The square runs through changes in the following order:After running forward, the animation runs in reverse.New to Flutter?
This page assumes you know how to create a layout using Flutter’s
widgets. For more information, see Building Layouts in Flutter.<topic_end>
<topic_start>
Basic structure of a staggered animation
<topic_end>
<topic_start>What's the point?
The following diagram shows the Intervals used in the
basic_staggered_animation example.
You might notice the following characteristics:To set up the animation:When the controlling animation’s value changes,
the new animation’s value changes, triggering the UI to update.The following code creates a tween for the width property.
It builds a CurvedAnimation,
specifying an eased curve. See Curves for
other available pre-defined animation curves.The begin and end values don’t have to be doubles.
The following code builds the tween for the borderRadius property
(which controls the roundness of the square’s corners),