我正在尝试旋转一个小部件(它不是问题的一部分,因为它通过构造参数自行处理旋转),基于插值动画来旋转,该动画在先前的旋转位置和使用插件函数获得的新位置之间进行插值。该函数的值 (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
值。我该如何实现?提前感谢。