text stringlengths 1 372 |
|---|
_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, |
/// 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) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.