Single preview composable
A straightforward way is to combine multiple composables in a single preview, for example:
@Preview(showBackground = true)
@Composable
fun GreetingTextPreview() {
Column {
val names = remember {
arrayOf("John", "D'Artagnan")
}
for (name in names) {
GreetingText(name)
}
}
}
The result:
Multiple previews using PreviewParameter
It might not be convenient to combine multiple composables into a single preview if the composable is too large for example. An alternative way is to use a PreviewParameter, this way you'll get separate previews:
class NameProvider: PreviewParameterProvider<String> {
override val values: Sequence<String> = sequenceOf(
"John",
"Albert Einstein"
)
}
@Preview(showBackground = true)
@Composable
fun GreetingTextPreview(
@PreviewParameter(NameProvider::class) name: String
) {
GreetingText(name)
}
And the result:
But keep in mind that you're limited to have only one @PreviewParameter
per preview composable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…