Colton Idle
06/26/2023, 2:38 PM@Composable
fun MySettingDialog(visible: Boolean, seedValue: String, saveEvent: () -> Unit){
var tempValue by remember { mutableStateOf(seedValue) }
if (visible){
//DialogWithTextField...
// use tempValue in textField, and onTextUpdate, update tempValue
}
}
this works fine the first time... but messes up every subsequent time because if the user types something in... it will be remembered, and then when you comes back to the dialog it will have the only remembered value even though the user didn't hit save.
It seems like an easy way to do this is for the seedValue to replace tempValue in a LaunchedEffect, but maybe kinda seems hacky. Am I missing something simple here?Colton Idle
06/26/2023, 2:39 PM@Composable
fun MySettingDialog(visible: Boolean, seedValue: String, saveEvent: () -> Unit){
var tempValue by remember { mutableStateOf("") }
LaunchedEffect(visible) {
if (visible) tempValue = seedValue
}
if (visible){
//DialogWithTextField...
// use tempValue in textField, and onTextUpdate, update tempValue
}
}
Albert Chang
06/26/2023, 2:55 PMvisible
out of the composable.Francesc
06/26/2023, 3:49 PMonValueUpdated
lambda. Also, the `visible `flag should not be part of the dialog, you should conditionally show or hide the dialog, instead of the dialog receiving a isVisible
flagColton Idle
06/27/2023, 3:09 AMColton Idle
06/27/2023, 3:10 AMAlbert Chang
06/27/2023, 3:19 AMvar tempValue by remember { mutableStateOf("") }
inside the if clause, but as a rule of thumb a composable function should always emit something and if you want to show something conditionally you should use an if clause on the call site of it.Colton Idle
06/28/2023, 4:09 AMFrancis Mariano
06/29/2023, 5:13 PMColton Idle
08/06/2023, 8:17 PMif (showModal){
var tempValue by remember { mutableStateOf("") }
LaunchedEffect(Unit) { tempValue = appStateHolder.settingX.first() }
SettingXDialog(
closeEvent = dismiss,
settingXChangeEvent = settingxChangeEvent,
settingX = tempValue,
textChange = { tempValue = it })
}