@Composable
fun DeterminateCircularProgressIndicatorExample() {
var currentProgress by remember { mutableStateOf(0.0f) }
var loading by remember { mutableStateOf(false) }
val animatedProgress by animateFloatAsState(
targetValue = currentProgress,
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
label = ""
)
val scope = rememberCoroutineScope()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
CircularProgressIndicator(progress = animatedProgress)
Spacer(Modifier.requiredHeight(30.dp))
Button(
onClick = {
loading = true
scope.launch {
loadProgress { progress ->
currentProgress = progress
}
loading = false
}
},
enabled = !loading
) {
Text("Start loading")
}
}
}
suspend fun loadProgress(updateProcess: (Float) -> Unit) {
for (current in 1..100) {
updateProcess( current.toFloat() / 100 )
delay(50) //<-- Operación de carga.
}
}