为什么 Animation Tween 总是 运行 从 0.0 到 1.0?
Why is Animation Tween always running from 0.0 to 1.0?
我想用
在flutter中制作动画
late AnimationController controller1;
late Animation<double> animation1;
@override
void initState() {
super.initState();
controller1 = AnimationController(
duration: const Duration(milliseconds: 700),
vsync: this,
);
animation1 = Tween(begin: 50.0, end: 0.0).animate(controller1)
..addListener(() {
setState(() {});
});
controller1.forward();
}
无论我输入哪个值作为“开始”和“结束” -
controller1.value
总是从 0.0 到 1.0。我这里有什么错误?
谢谢!
您应该使用 animation1.value
而不是控制器值。
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 350),
);
animation = Tween<double>(begin:50, end:0.0).animate(controller);
animation.addListener((){
print(animation.value);
});
controller.forward();
}
输出
50 50
41.285714285714285
36.714285714285715
31.57142857142857
24.857142857142854
20.14285714285714
15.428571428571423
10.428571428571423
5.714285714285708
0.9999999999999929 0
我认为您想使用 animation1.value
,而不是 controller1.value,您可以使用以下代码验证它:) :
@override
void initState() {
super.initState();
controller =
AnimationController(duration: const Duration(seconds: 2), vsync: this);
animation = Tween<double>(begin: 50.0, end: 0.0).animate(controller)
..addListener(() {
// #enddocregion addListener
setState(() {
print("Animation value: ${animation.value}");
//TODO: you can uncomment this print to see the value of the controller
//print("Controller value: ${controller.value}");
});
});
controller.forward();
}
查看官方教程了解更多信息:)
顺便说一下,由于 controller.value
从 0.0 变为 1.0,您可以将其视为一种完成百分比 :)
我设法使用 SlideTransition
和 FadeTransition
对其进行了排序。
我想我们应该只将 Transition
小部件用于...转换?而 Positioned
和 Opacity
之类的东西用于更多静态小部件?不确定...
它的样子:https://youtu.be/hj7PkjXrgfg
无论如何,这是替换代码,如果有人正在寻找参考:
class WeatherCloudWidget extends StatefulWidget {
final double sunSize;
final CloudProperties properties;
WeatherCloudWidget({Key key, this.properties, this.sunSize})
: super(key: key);
@override
State<StatefulWidget> createState() => _WeatherCloudWidget();
}
class _WeatherCloudWidget extends State<WeatherCloudWidget>
with TickerProviderStateMixin {
AnimationController controller;
Animation<Offset> position;
Animation<double> opacity;
final alphaTween = new Tween(begin: 0.0, end: 1.0);
@override
initState() {
super.initState();
_startAnimation();
}
@override
Widget build(BuildContext context) {
// screen width and height
final screenWidth = MediaQuery.of(context).size.width;
final screenHeight = MediaQuery.of(context).size.height;
final properties = widget.properties;
var vertical = (screenHeight * 0.5) +
(widget.sunSize * properties.verticalOffset * -1);
var horizontal =
(screenWidth * 0.5) + (widget.sunSize * properties.tweenEnd);
return Positioned(
left: horizontal,
top: vertical,
child: SlideTransition(
position: position,
child: FadeTransition(
opacity: opacity,
child: Image.asset(
properties.path,
width: properties.getScaledWidth(widget.sunSize),
height: properties.getScaledHeight(widget.sunSize),
),
)),
);
}
@override
dispose() {
controller.dispose();
super.dispose();
}
void _startAnimation() {
controller = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
position = new Tween<Offset>(
begin: new Offset(widget.properties.tweenStart, 0.0),
end: new Offset(0.0, 0.0),
).animate(new CurvedAnimation(parent: controller, curve: Curves.decelerate));
opacity = alphaTween.animate(controller);
controller.forward();
}
}
我想用
在flutter中制作动画 late AnimationController controller1;
late Animation<double> animation1;
@override
void initState() {
super.initState();
controller1 = AnimationController(
duration: const Duration(milliseconds: 700),
vsync: this,
);
animation1 = Tween(begin: 50.0, end: 0.0).animate(controller1)
..addListener(() {
setState(() {});
});
controller1.forward();
}
无论我输入哪个值作为“开始”和“结束” -
controller1.value
总是从 0.0 到 1.0。我这里有什么错误?
谢谢!
您应该使用 animation1.value
而不是控制器值。
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 350),
);
animation = Tween<double>(begin:50, end:0.0).animate(controller);
animation.addListener((){
print(animation.value);
});
controller.forward();
}
输出
50 50 41.285714285714285 36.714285714285715 31.57142857142857 24.857142857142854 20.14285714285714 15.428571428571423 10.428571428571423 5.714285714285708 0.9999999999999929 0
我认为您想使用 animation1.value
,而不是 controller1.value,您可以使用以下代码验证它:) :
@override
void initState() {
super.initState();
controller =
AnimationController(duration: const Duration(seconds: 2), vsync: this);
animation = Tween<double>(begin: 50.0, end: 0.0).animate(controller)
..addListener(() {
// #enddocregion addListener
setState(() {
print("Animation value: ${animation.value}");
//TODO: you can uncomment this print to see the value of the controller
//print("Controller value: ${controller.value}");
});
});
controller.forward();
}
查看官方教程了解更多信息:)
顺便说一下,由于 controller.value
从 0.0 变为 1.0,您可以将其视为一种完成百分比 :)
我设法使用 SlideTransition
和 FadeTransition
对其进行了排序。
我想我们应该只将 Transition
小部件用于...转换?而 Positioned
和 Opacity
之类的东西用于更多静态小部件?不确定...
它的样子:https://youtu.be/hj7PkjXrgfg
无论如何,这是替换代码,如果有人正在寻找参考:
class WeatherCloudWidget extends StatefulWidget {
final double sunSize;
final CloudProperties properties;
WeatherCloudWidget({Key key, this.properties, this.sunSize})
: super(key: key);
@override
State<StatefulWidget> createState() => _WeatherCloudWidget();
}
class _WeatherCloudWidget extends State<WeatherCloudWidget>
with TickerProviderStateMixin {
AnimationController controller;
Animation<Offset> position;
Animation<double> opacity;
final alphaTween = new Tween(begin: 0.0, end: 1.0);
@override
initState() {
super.initState();
_startAnimation();
}
@override
Widget build(BuildContext context) {
// screen width and height
final screenWidth = MediaQuery.of(context).size.width;
final screenHeight = MediaQuery.of(context).size.height;
final properties = widget.properties;
var vertical = (screenHeight * 0.5) +
(widget.sunSize * properties.verticalOffset * -1);
var horizontal =
(screenWidth * 0.5) + (widget.sunSize * properties.tweenEnd);
return Positioned(
left: horizontal,
top: vertical,
child: SlideTransition(
position: position,
child: FadeTransition(
opacity: opacity,
child: Image.asset(
properties.path,
width: properties.getScaledWidth(widget.sunSize),
height: properties.getScaledHeight(widget.sunSize),
),
)),
);
}
@override
dispose() {
controller.dispose();
super.dispose();
}
void _startAnimation() {
controller = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
position = new Tween<Offset>(
begin: new Offset(widget.properties.tweenStart, 0.0),
end: new Offset(0.0, 0.0),
).animate(new CurvedAnimation(parent: controller, curve: Curves.decelerate));
opacity = alphaTween.animate(controller);
controller.forward();
}
}