text
stringlengths
1
372
),
);
}
class page1 extends StatelessWidget {
const page1({super.key});
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(),
body: center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(_createRoute());
},
child: const Text('Go!'),
),
),
);
}
}
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,