Yes, you can dynamically change the secondary label based on the checked state by using Compose's conditional logic inside the lambda for the secondaryLabel. Here's an example of how you might do that:
@Composable
fun YourComposable() {
val contentResolver = LocalContext.current.contentResolver
var autoBrightness by remember {
mutableStateOf(
Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE
) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
)
}
VerisureTheme {
Scaffold {
ScalingLazyColumn(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(top = 12.dp, bottom = 16.dp),
) {
item {
ToggleChip(
modifier = Modifier.fillMaxWidth(),
label = {
Text("Brightness Mode")
},
secondaryLabel = {
// Change secondary label based on the state
if (autoBrightness) {
Text("Automatic")
} else {
Text("Manual")
}
},
onCheckedChange = { isChecked ->
val newMode = if (isChecked) {
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
} else {
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
}
Settings.System.putInt(
contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
newMode
)
autoBrightness = isChecked
},
checked = autoBrightness,
toggleControl = {
Switch(checked = autoBrightness)
},
)
}
}
}
}
}
Explanation:
- The
secondaryLabel lambda now contains an if condition. It checks the value of autoBrightness and displays either "Automatic" or "Manual" accordingly.
- When the user toggles the chip, the
autoBrightness state is updated, causing the composable to recompose and update the secondary label.
With this approach, the label automatically reflects the current brightness mode.