Flutter 动画中的_animation.value是什么,为什么值是260.0而不是1.0?
lowerBound -动画起始值,在我的示例中为 0.0
upperBound -动画结束值,在我的示例中为 1.0
value -当前动画值,默认为lowerBound起始值,即在我的示例中为0.0 。一切都在这里匹配。
理论上,正如我所想,动画的当前值 应该在 0.0 和 0.1 之间。关于这个错误输出,我决定在正方形移动的过程中改变不透明度(opacity)。动画开始时透明度为 0.0,动画结束时透明度为 1.0。但是在所有动画中,透明度为零
_color = Color.fromRGBO(255, 255, 255, _animation.value);
我决定测量当前动画的值,当方块移动时,值就会显示出来。动画开始的时候是0.0,到最后是260.0,理论上应该是1.0
这段代码放在监听器 addListener()中,当动画值的当前值发生变化或者每隔TickerProvider对象的勾号,这里我不确定。<br /
..addListener(() {
setState(() {
//
_color = Color.fromRGBO(255, 255, 255, _animation.value);
_str = '${_animation.value}';
//идентично
//_str = '${_controller.value}';
});
})
接下来是完整的
main.dart代码
import 'dart:ui';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Name App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Name Page'),
),
body: MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState
extends State<MyHomePage>
with SingleTickerProviderStateMixin {
AnimationController _controller;
Tween _tween;
Animation<double> _animation;
double _width;
double _height;
Color _color;
String _str;
@override
void initState() {
super.initState();
_width = 100.0;
_height = 100.0;
_color = Colors.black38;
_str = '';
_controller = AnimationController(
vsync: this,
value: 0.0, //default
lowerBound: 0.0, //default
upperBound: 1.0, //default
duration: Duration(seconds: 1),
)..addListener(() {
setState(() {
//
_color = Color.fromRGBO(255, 255, 255, _animation.value);
_str = '${_animation.value}';
//идентично
//_str = '${_controller.value}';
});
});
_tween = Tween<double>(begin: 0.0, end: window.physicalSize.width / 2 - _width);
_animation = _tween.animate(_controller);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
void _f1() {
setState(() {
});
}
void _funForward() {
setState(() {
_controller
..forward();
});
}
void _funReverse() {
setState(() {
_controller
..reverse();
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Expanded(
flex: 1,
child: LayoutBuilder(
builder: (context, constraints) =>
Stack(
children: <Widget>[
Positioned(
// 1/2 высоты родительского - 1/2 высоты дочернего виджета
top: (constraints.maxHeight / 2) - (_height / 2),
left: _animation.value,
child: Container(
width: _width,
height: _height,
color: _color,
alignment: Alignment.center,
child: Text(
_str,
style: TextStyle(
fontSize: 20.0,
color: Colors.pink,
),
),
),
),
],
),
),
),
Container(
padding: const EdgeInsets.all(8.0),
color: Colors.orangeAccent,
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: RaisedButton(
onPressed: () { _funForward(); },
splashColor: Colors.blue.withOpacity(0.5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
child: Text(
'start'.toUpperCase(),
style: TextStyle(
color: Colors.deepPurple,
fontSize: 16.0,
),
),
),
),
SizedBox(width: 8.0,),
Expanded(
flex: 1,
child: RaisedButton(
onPressed: () { _funReverse(); },
splashColor: Colors.blue.withOpacity(0.5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
child: Text(
'back'.toUpperCase(),
style: TextStyle(
color: Colors.deepPurple,
fontSize: 16.0,
),
),
),
),
],
),
),
],
);
}
}

_animation.value这是动画的当前值。你不应该
animation通过以下方式重做tween:你
tween有:要解决所有问题,只需使用
controller而不是animation: