gpt4 book ai didi

dart - 如何 : CurvedAnimation in "Zero to One" Example

转载 作者:IT王子 更新时间:2023-10-29 07:10:25 28 4
gpt4 key购买 nike

有人可以举例说明如何将 CurvedAnimation 实现到 Zero to One 中吗? ,还是通常与 AnimationController 一起使用?

最佳答案

无论您在何处使用动画,都可以使用 CurvedAnimation 并将原始动画作为父级。在这种情况下,您可以将 tween.animate(animation) 替换为

tween.animate(
new CurvedAnimation(
parent: animation,
curve: Curves.ease,
),
),

这是一个完整的示例,显示了使用各种曲线将条形动画设置为相同的值。

screenshot

import 'dart:math';

import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

import 'dart:ui' show lerpDouble;

class Bar {
Bar(this.height);

final double height;

static Bar lerp(Bar begin, Bar end, double t) {
return new Bar(lerpDouble(begin.height, end.height, t));
}
}

class BarTween extends Tween<Bar> {
BarTween(Bar begin, Bar end) : super(begin: begin, end: end);

@override
Bar lerp(double t) => Bar.lerp(begin, end, t);
}

class BarChartPainter extends CustomPainter {
static const barWidth = 10.0;

BarChartPainter(Animation<Bar> animation)
: animation = animation,
super(repaint: animation);

final Animation<Bar> animation;

@override
void paint(Canvas canvas, Size size) {
final bar = animation.value;
final paint = new Paint()
..color = Colors.blue[400]
..style = PaintingStyle.fill;
canvas.drawRect(
new Rect.fromLTWH(
(size.width - barWidth) / 2.0,
size.height - bar.height,
barWidth,
bar.height,
),
paint,
);
}

@override
bool shouldRepaint(BarChartPainter old) => false;
}

void main() {
runApp(new MaterialApp(home: new ChartPage()));
}

class ChartPage extends StatefulWidget {
@override
ChartPageState createState() => new ChartPageState();
}

class ChartPageState extends State<ChartPage> with TickerProviderStateMixin {
final random = new Random();
AnimationController animation;
BarTween tween;

@override
void initState() {
super.initState();
animation = new AnimationController(
duration: const Duration(milliseconds: 900),
vsync: this,
);
tween = new BarTween(new Bar(0.0), new Bar(50.0));
animation.forward();
}

@override
void dispose() {
animation.dispose();
super.dispose();
}

void changeData() {
setState(() {
tween = new BarTween(
tween.evaluate(animation),
new Bar(600.0 * random.nextDouble()),
);
animation.forward(from: 0.0);
});
}

final List<Curve> curves = [
Curves.bounceIn,
Curves.bounceInOut,
Curves.bounceOut,
Curves.decelerate,
Curves.ease,
Curves.easeIn,
Curves.easeInOut,
Curves.easeOut,
Curves.elasticIn,
Curves.elasticInOut,
Curves.elasticOut,
Curves.fastOutSlowIn,
];

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: new List.generate(curves.length, (int index) {
return new CustomPaint(
painter: new BarChartPainter(
tween.animate(
new CurvedAnimation(
parent: animation, curve: curves[index]),
),
),
);
}),
),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.refresh),
onPressed: changeData,
),
);
}
}

关于dart - 如何 : CurvedAnimation in "Zero to One" Example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404132/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com