text
stringlengths
1
474
method defines a Tween that interpolates between the point the widget was
dragged to, to the point in the center.
<code_start>void _runAnimation() {
_animation = _controller.drive(
AlignmentTween(
begin: _dragAlignment,
end: Alignment.center,
),
);
_controller.reset();
_controller.forward();
}<code_end>
Next, update _dragAlignment when the AnimationController produces a
value:Next, make the Align widget use the _dragAlignment field:
<code_start>child: Align(
alignment: _dragAlignment,
child: Card(
child: widget.child,
),
),<code_end>
Finally, update the GestureDetector to manage the animation controller:<topic_end>
<topic_start>
Step 4: Calculate the velocity to simulate a springing motion
The last step is to do a little math, to calculate the velocity of the widget
after it’s finished being dragged. This is so that the widget realistically
continues at that speed before being snapped back. (The _runAnimation method
already sets the direction by setting the animation’s start and end alignment.)First, import the physics package:
<code_start>import 'package:flutter/physics.dart';<code_end>
The onPanEnd callback provides a DragEndDetails object. This object
provides the velocity of the pointer when it stopped contacting the screen. The
velocity is in pixels per second, but the Align widget doesn’t use pixels. It
uses coordinate values between [-1.0, -1.0] and [1.0, 1.0], where [0.0, 0.0]
represents the center. The size calculated in step 2 is used to convert pixels
to coordinate values in this range.Finally, AnimationController has an animateWith() method that can be given a
SpringSimulation:
<code_start>/// 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);
}<code_end>
Don’t forget to call _runAnimation() with the velocity and size:
<code_start>onPanEnd: (details) {
_runAnimation(details.velocity.pixelsPerSecond, size);
},<code_end>
info Note
Now that the animation controller uses a simulation it’s duration argument
is no longer required.<topic_end>
<topic_start>
Interactive Example
<code_start>import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
void main() {
runApp(const MaterialApp(home: PhysicsCardDragDemo()));
}
class PhysicsCardDragDemo extends StatelessWidget {
const PhysicsCardDragDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: const DraggableCard(
child: FlutterLogo(
size: 128,
),
),
);
}
}
/// A draggable card that moves back to [Alignment.center] when it's
/// released.
class DraggableCard extends StatefulWidget {
const DraggableCard({required this.child, super.key});
final Widget child;
@override
State<DraggableCard> createState() => _DraggableCardState();
}
class _DraggableCardState extends State<DraggableCard>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
/// The alignment of the card as it is dragged or being animated.
///
/// While the card is being dragged, this value is set to the values computed
/// in the GestureDetector onPanUpdate callback. If the animation is running,