【发布时间】:2019-07-27 13:30:32
【问题描述】:
我正在尝试基于一个动画来旋转一个小部件(这不是问题的一部分,因为它通过构造函数参数处理旋转本身),该动画在先前的旋转位置和通过插件函数获得的新位置之间进行插值。该函数的值(FlutterCompass.events.listen)会定期异步更新,并且每次都会重建 Tween 对象以表示小部件位置的更新。这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';
import 'package:flutter_compass/flutter_compass.dart';
import 'package:compass_test/compass.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin{
double _direction;
double _angle = 0.0;
Animation<double> _animation;
AnimationController _animationController;
Tween<double> _tween;
@override
void initState() {
super.initState();
_animationController =
AnimationController(duration: Duration(milliseconds: 400), vsync: this);
_tween = Tween<double>(begin: 0.0, end: 0.0);
_animation = _tween.animate(_animationController)
..addListener(() {
setState(() {
_angle =_animationController.value;
});
});
_direction = 0;
FlutterCompass.events.listen((double direction) {
print(_animationController.status);
if(_direction !=direction){
_tween = Tween<double>(
begin: _direction,
end: direction);
_animationController.reset();
_tween.animate(_animationController);
_animationController.forward();
}
_direction = direction;
});
}
@override
void dispose(){
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
children: <Widget>[
Center(
child: Container(
margin: EdgeInsets.only(top: 100),
child: Compass(height: 300, width: 300, angleToNorth: _angle)
)
)
],
)
),
);
}
}
但是,正如我在一些调试中看到的那样,_animationController.value 返回的值仅在 0.0 到 1.0 之间变化,这不是我认为应该发生的:我希望它们与之前的不同将 _direction 的值更改为新的 direction 值。我怎样才能做到这一点?
提前致谢
【问题讨论】:
标签: animation flutter flutter-animation