text stringlengths 1 372 |
|---|
), |
); |
} |
} |
<code_end> |
make the _DraggableCardState class extend from |
SingleTickerProviderStateMixin. |
then construct an AnimationController in |
initState and set vsync to this. |
info note |
extending SingleTickerProviderStateMixin allows the state object to be a |
TickerProvider for the AnimationController. for more information, see the |
documentation for TickerProvider. |
<topic_end> |
<topic_start> |
step 2: move the widget using gestures |
make the widget move when it’s dragged, and add an alignment field to the |
_DraggableCardState class: |
add a GestureDetector that handles the onPanDown, onPanUpdate, and |
onPanEnd callbacks. to adjust the alignment, use a MediaQuery to get the |
size of the widget, and divide by 2. (this converts units of “pixels dragged” to |
coordinates that align uses.) then, set the align widget’s alignment to |
_dragAlignment: |
<topic_end> |
<topic_start> |
step 3: animate the widget |
when the widget is released, it should spring back to the center. |
add an Animation<Alignment> field and an _runAnimation method. this |
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) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.