text
stringlengths
1
474
‘FractionalTranslation’ widget.
Setting the dy argument to 1 represents a vertical translation one
full height of the page.The transitionsBuilder callback has an animation parameter. It’s an
Animation<double> that produces values between 0 and 1. Convert the
Animation into an Animation using a Tween:
<code_start>transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
final tween = Tween(begin: begin, end: end);
final offsetAnimation = animation.drive(tween);
return child;
},<code_end>
<topic_end>
<topic_start>
3. Use an AnimatedWidget
Flutter has a set of widgets extending AnimatedWidget
that rebuild themselves when the value of the animation changes. For instance,
SlideTransition takes an Animation<Offset> and translates its child (using a
FractionalTranslation widget) whenever the value of the animation changes.AnimatedWidget Return a SlideTransition
with the Animation<Offset> and the child widget:
<code_start>transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
final tween = Tween(begin: begin, end: end);
final offsetAnimation = animation.drive(tween);
return SlideTransition(
position: offsetAnimation,
child: child,
);
},<code_end>
<topic_end>
<topic_start>
4. Use a CurveTween
Flutter provides a selection of easing curves that
adjust the rate of the animation over time.
The Curves class
provides a predefined set of commonly used curves.
For example, Curves.easeOut
makes the animation start quickly and end slowly.To use a Curve, create a new CurveTween
and pass it a Curve:
<code_start>var curve = Curves.ease;
var curveTween = CurveTween(curve: curve);<code_end>
This new Tween still produces values from 0 to 1. In the next step, it will be
combined the Tween<Offset> from step 2.<topic_end>
<topic_start>
5. Combine the two Tweens
To combine the tweens,
use chain():
<code_start>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));<code_end>
Then use this tween by passing it to animation.drive(). This creates a new
Animation<Offset> that can be given to the SlideTransition widget:
<code_start>return SlideTransition(
position: animation.drive(tween),
child: child,
);<code_end>
This new Tween (or Animatable) produces Offset values by first evaluating the
CurveTween, then evaluating the Tween<Offset>. When the animation runs, the
values are computed in this order:Another way to create an Animation<Offset> with an easing curve is to use a
CurvedAnimation:
<code_start>transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
const curve = Curves.ease;
final tween = Tween(begin: begin, end: end);
final curvedAnimation = CurvedAnimation(
parent: animation,
curve: curve,
);
return SlideTransition(
position: tween.animate(curvedAnimation),
child: child,
);
}<code_end>
<topic_end>
<topic_start>
Interactive example
<code_start>import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Page1(),
),
);
}
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!'),
),