Mayank Saini
10/27/2020, 8:36 AMMarquee
. Is this correct?
@Composable
fun MarqueeText(announcement: String, marqueeSpeed: MarqueeSpeed, modifier: Modifier = Modifier) {
val scrollState = rememberScrollState()
val scope = remember { CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) }
onDispose { scope.cancel() }
ScrollableRow(scrollState = scrollState, modifier = modifier, children = {
Row {
repeat(1000) {
Text(
text = announcement, maxLines = 1
)
}
}
val scrollBy = when (marqueeSpeed) {
MarqueeSpeed.SLOW -> 2f
MarqueeSpeed.MEDIUM -> 6f
MarqueeSpeed.FAST -> 9f
}
scrollState.smoothScrollBy(scrollBy)
scope.launch {
while (true) {
scrollState.smoothScrollBy(scrollBy)
delay(50)
}
}
})
}
Zach Klippenstein (he/him) [MOD]
10/27/2020, 4:26 PMremember { CoroutineScope… }
, use rememberCoroutineScope()
instead.
- In this case, you don’t even need that. Never trigger side effects directly in the composition either, use one of the *Effect
functions. In this case, you could use LaunchedTask
(which will eventually be renamed to LaunchedEffect
).
- There’s a better way to do looping animations, using animated values (see how cursor blinking is implemented).
- I think using ScrollableRow
here means you’ll have to fight the system to make it non-interactive - better to just use a row and “scroll” it by using modifiers only.Mayank Saini
10/28/2020, 10:40 AMZach Klippenstein (he/him) [MOD]
10/28/2020, 1:46 PMval childConstraints = constraints.copy(maxWidth = Constraints.Infinity)
Mayank Saini
10/30/2020, 2:03 AMMarqueeText
in the pipeline as a ready to use feature?Zach Klippenstein (he/him) [MOD]
11/02/2020, 4:56 PM