I'm new to Compose framework and StateFlows. I want LinearProgressIndicator to show progress that will be emitted from downloadProgress StateFlow. How can I achieve that?
@Composable
fun DownloadProgressDialog(
downloadProgress: StateFlow<Float>,
onDismissRequest: () -> Unit = {},
) {
// how to pass downloadProgress to LinearProgressIndicator?
var progress by remember { mutableStateOf(0f) }
val animatedProgress = animateFloatAsState(
targetValue = progress,
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
).value
Dialog(onDismissRequest = { onDismissRequest() }) {
Surface {
Column {
LinearProgressIndicator(progress = animatedProgress)
}
}
}
}
You need a
StateorMutableStateto schedule recomposition.https://stackoverflow.com/a/70217911/5457853
And you can have a stateless Composable by changing DownloadProgressDialog input with Float as
StateFlow has an extension function to represent it as
StateI made a sample with ViewModel but you can ignore it if not needed and use how
StateFlowis converted toState