valueCode
Returns a Kotlin code string representing the current value of this field.
This method is used by the Code tab in the Inspector pane to generate copy-pastable Kotlin code that reproduces the current preview state. The returned string should be valid Kotlin code that can be directly inserted into source files.
Built-in implementations
Most built-in field types provide appropriate implementations:
me.tbsten.compose.preview.lab.field.BooleanField:
"true"or"false"me.tbsten.compose.preview.lab.field.ColorField:
"Color.Red"or"Color(0xFFFF0000)"
Custom implementation
When creating a custom field, override this method to provide appropriate code generation:
class MyCustomField(label: String, initialValue: MyType) :
MutablePreviewLabField<MyType>(label, initialValue) {
override fun valueCode(): String {
return "MyType(param = ${value.param})"
}
@Composable
override fun Content() { /* ... */}
}Using withValueCode
For existing fields, use me.tbsten.compose.preview.lab.field.withValueCode to customize the code output without creating a new class:
val fontWeight = fieldValue {
SelectableField(
label = "Font Weight",
choices = listOf(FontWeight.Normal, FontWeight.Bold),
choiceLabel = { it.toString() },
).withValueCode { weight ->
when (weight) {
FontWeight.Normal -> "FontWeight.Normal"
FontWeight.Bold -> "FontWeight.Bold"
else -> "FontWeight(${weight.weight})"
}
}
}Return
A valid Kotlin code string representing the current value