我正在构建一个广播应用程序。就像在 Spotify 中一样,有一个带有当前标题和艺术家的栏,文本应该在一行中并具有给定的宽度。如何让文本从右向左和向后移动?
使用自制动画时,我希望移动文本的速度是固定的,因此需要文本小部件的时间和宽度。
是否有包/内置选项来执行此操作?还是我必须使用自制的动画?如果是这样,我怎样才能获得文本小部件宽度?
控制器和动画:
AnimationController(duration: Duration(seconds: 10), vsync: this);
animation = Tween<double>(begin: 0, end: 1)
.animate(CurvedAnimation(parent: _controller, curve: Curves.linear));
animation.addListener(() {
setState(() {});
});
_controller.repeat();
Run Code Online (Sandbox Code Playgroud)
构建方法
double value =
-300 * (animation.value <= 0.5 ? animation.value : 1 - animation.value);
return Container(
child: SizedBox(
width: widget.width,
height: 24,
child: Transform.translate(
offset: Offset(value, 0),
child: widget.text,
),
),
);
Run Code Online (Sandbox Code Playgroud)