@Composable
fun LinearDeterminateIndicator() {
var currentProgress by remember { mutableFloatStateOf(0f) }
var loading by remember { mutableStateOf(false) }
val scope = rememberCoroutineScope() // Create a coroutine scope
// Animate between progress updates so the indicator fills smoothly.
val animatedProgress by animateFloatAsState(
targetValue = currentProgress,
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
)
if (loading) {
LinearProgressIndicator(
progress = { animatedProgress },
modifier = Modifier.fillMaxWidth(),
)
} else {
Button(onClick = {
loading = true
scope.launch {
loadProgress { progress ->
currentProgress = progress
}
loading = false // Reset loading when the coroutine finishes
}
}) {
Text("Start loading")
}
}
}
/** Iterate the progress value */
suspend fun loadProgress(updateProgress: (Float) -> Unit) {
for (i in 1..100) {
updateProgress(i.toFloat() / 100)
delay(100)
}
}