I am building a radio app. Like in Spotify, there is a bar with the current title and artist, the text should be in one line and in a given width. How can I let the text move from right to left and back?
When using a self-made animation, I want to have a fixed speed of the moving text, so I need the time and the width of the text widget.
Is there a package/built-in option to do this?
Or do I have to use a self-made animation? If so, how can I get the text widget width?
Controller and animation:
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();
build method
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,
),
),
);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…