text stringlengths 1 474 |
|---|
), |
); |
} |
} |
Route _createRoute() { |
return PageRouteBuilder( |
pageBuilder: (context, animation, secondaryAnimation) => const Page2(), |
transitionsBuilder: (context, animation, secondaryAnimation, child) { |
const begin = Offset(0.0, 1.0); |
const end = Offset.zero; |
const curve = Curves.ease; |
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve)); |
return SlideTransition( |
position: animation.drive(tween), |
child: child, |
); |
}, |
); |
} |
class Page2 extends StatelessWidget { |
const Page2({super.key}); |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar(), |
body: const Center( |
child: Text('Page 2'), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Animate a widget using a physics simulation |
Physics simulations can make app interactions feel realistic and interactive. |
For example, you might want to animate a widget to act as if it were attached to |
a spring or falling with gravity.This recipe demonstrates how to move a widget from a dragged point back to the |
center using a spring simulation.This recipe uses these steps:<topic_end> |
<topic_start> |
Step 1: Set up an animation controller |
Start with a stateful widget called DraggableCard: |
<code_start>import 'package:flutter/material.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, |
), |
), |
); |
} |
} |
class DraggableCard extends StatefulWidget { |
const DraggableCard({required this.child, super.key}); |
final Widget child; |
@override |
State<DraggableCard> createState() => _DraggableCardState(); |
} |
class _DraggableCardState extends State<DraggableCard> { |
@override |
void initState() { |
super.initState(); |
} |
@override |
void dispose() { |
super.dispose(); |
} |
@override |
Widget build(BuildContext context) { |
return Align( |
child: Card( |
child: widget.child, |
), |
); |
} |
}<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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.