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