You're absolutely right! My apologies for that oversight. The error message "Composable invocations can only happen from the context of a @Composable function" means you're trying to directly pass an ImageVector where a Composable lambda (@Composable () -> Unit) is expected.
The decreaseIcon and increaseIcon parameters of InlineSlider expect a lambda that contains the Icon composable.
Here's the corrected way to use the icons:
kotlin
import android.content.Context
import android.provider.Settings
import android.widget.Toast
import androidx.compose.runtime.*
import androidx.wear.compose.material.*
import androidx.compose.material.icons.Icons // Make sure this is imported
import androidx.compose.material.icons.filled.Add // And these
import androidx.compose.material.icons.filled.Remove // And these
import kotlin.math.roundToInt
@Composable
fun SettingsScreen(
context: Context,
// ... other parameters ...
) {
val values = listOf(5_000, 15_000, 30_000)
var currentTimeout by remember { mutableStateOf<Int?>(null) }
val hasPermission = remember { Settings.System.canWrite(context) }
LaunchedEffect(Unit) {
if (hasPermission) {
currentTimeout = try {
Settings.System.getInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
} catch (_: Settings.SettingNotFoundException) {
// Fallback if the setting is not found, or a default for first run
5_000
}
}
}
item {
if (hasPermission && currentTimeout != null) {
val index = values.indexOf(currentTimeout).coerceAtLeast(0)
InlineSlider(
value = index.toFloat(),
onValueChange = { floatIndex ->
val newIndex = floatIndex.roundToInt()
val newValue = values[newIndex]
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
newValue
)
currentTimeout = newValue
Toast.makeText(
context,
"Timeout set to ${newValue / 1_000} s",
Toast.LENGTH_SHORT
).show()
},
steps = values.size - 2, // 1 step between 3 values
valueRange = 0f..(values.size - 1).toFloat(),
// CORRECTED: Wrap the Icon composable in a lambda {}
decreaseIcon = { Icon(Icons.Default.Remove, "Decrease") },
increaseIcon = { Icon(Icons.Default.Add, "Increase") },
enabled = true
)
} else {
// Grayed-out slider when permission is missing
InlineSlider(
value = 0f,
onValueChange = {},
steps = 0,
valueRange = 0f..0f,
decreaseIcon = { Icon(Icons.Default.Remove, null) },
increaseIcon = { Icon(Icons.Default.Add, null) },
enabled = false
)
// Optional: A button to ask for permission if needed
if (!hasPermission) {
Spacer(modifier = Modifier.height(4.dp)) // Add some spacing
Chip(
onClick = {
// This should trigger the permission request.
// You might need an explicit callback for this,
// e.g., onGrantSettingsPermissionButtonClicked()
// For demonstration, you can directly open settings here:
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
intent.data = Uri.parse("package:" + context.packageName)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
},
label = { Text("Grant 'Modify system settings' permission") }
)
}
}
}
}
Reason for the fix:
The decreaseIcon and increaseIcon parameters in InlineSlider are defined as Composable lambdas:
decreaseIcon: @Composable () -> UnitincreaseIcon: @Composable () -> Unit
This means they expect a block of Composable UI elements (in this case, just an Icon) inside curly braces {}. You were trying to pass InlineSliderDefaults.Increase (which is an ImageVector) directly.
By changing increaseIcon = Icon(InlineSliderDefaults.Increase, "Increase") to increaseIcon = { Icon(Icons.Default.Add, "Increase") }, you are now providing the expected Composable lambda. I've also swapped InlineSliderDefaults.Increase/Decrease for Icons.Default.Add/Remove which are more commonly used and explicit. Make sure to import androidx.compose.material.icons.Icons, androidx.compose.material.icons.filled.Add, and androidx.compose.material.icons.filled.Remove.
I've also added a conditional Chip that can appear if permission is not granted, offering the user a way to go to settings and grant it. You'd likely want to integrate this with your onPermissionButtonPressed callback.