When I tried to write text, on a buttonClick event, I ran into the problem, that I can't call a @Composable function inside another function because of the: @Composable invocations can only happen from the context of a @Composable function error. I tried to call the showResult function inside the Composable BeCalc function on a button click event. Any ideas? Thanks
@Composable fun showResult(result: Int?) { Text( buildAnnotatedString { if(result != null && result != 0){ append("Unnecessary Text") withStyle(style = SpanStyle(color = MaterialTheme.colors.primaryVariant)) //Einheiten-Anzahl in Farbe { append("$result") } append("Unnecessary Text") } }, modifier = Modifier.padding(top = 48.dp), fontSize = 20.sp, fontWeight = FontWeight.Bold ) } @Composable fun BeCalc(openDrawer: () -> Unit) { var valueCarbohydrate by remember { mutableStateOf(TextFieldValue("")) } var valueQuantity by remember { mutableStateOf(TextFieldValue("")) } var result: Int? = 0 Button( modifier = Modifier.padding(top = 24.dp), colors = ButtonDefaults.buttonColors(MaterialTheme.colors.primaryVariant), onClick = { val intValueQuantity = valueQuantity.text.toInt() val intValueCarbohydrate = valueCarbohydrate.text.toInt() if (intValueQuantity != null && intValueCarbohydrate != null) { result = (intValueCarbohydrate / intValueQuantity) showResult(result) } else { println("No value!") } })Button( modifier = Modifier.padding(top = 24.dp), colors = ButtonDefaults.buttonColors(MaterialTheme.colors.primaryVariant), onClick = { val intValueQuantity = valueQuantity.text.toInt() val intValueCarbohydrate = valueCarbohydrate.text.toInt() if (intValueQuantity != null && intValueCarbohydrate != null) { result = (intValueCarbohydrate / intValueQuantity) showResult(result) } else { println("No value!") } } ) }
showResultand by default it's false and you set it true inside onclick function and later in composable function level addif (showResult) { showResult(result) }