I have one Composable
function with lambda which is used to get Button Click action. I want to preview that Composable
function. But Composable function with this kind of lambda getting error after adding @Preview
annotation above @Composable
Composable functions with non-default parameters are not supported in Preview unless they are annotated with @PreviewParameter.
The composable function looks like
@Composable
fun MyView(onViewButtonClick: () -> Unit) {
Button(
enabled = isEnabled, colors = ButtonDefaults.buttonColors(
backgroundColor = greenColor
),
shape = Shapes.large, onClick = (onViewButtonClick),
modifier = Modifier
.fillMaxWidth()
.padding(15.dp, 40.dp, 15.dp, 15.dp)
) {
Text(
text = stringResource(id = R.string.send_otp),
color = Color.White,
fontSize = 20.sp
)
}
}
The application of this looks like
MyView(onViewButtonClick = {
Log.d("ViewButtonClick","ViewButtonClick")
}).
How to see preview of this composable function with Lambda ?
Either provide a default lambda to your composable, or you implement an empty lambda in your Preview
@Composable
fun MyView(onViewButtonClick: () -> Unit = {}) {
Button(
enabled = isEnabled, colors = ButtonDefaults.buttonColors(
backgroundColor = greenColor
),
shape = Shapes.large, onClick = (onViewButtonClick),
modifier = Modifier
.fillMaxWidth()
.padding(15.dp, 40.dp, 15.dp, 15.dp)
) {
Text(
text = stringResource(id = R.string.send_otp),
color = Color.White,
fontSize = 20.sp
)
}
}
@Preview
@Composable
fun MyViewPreview() {
MyView()
}
Or
@Composable
fun MyView(onViewButtonClick: () -> Unit) {
Button(
enabled = isEnabled, colors = ButtonDefaults.buttonColors(
backgroundColor = greenColor
),
shape = Shapes.large, onClick = (onViewButtonClick),
modifier = Modifier
.fillMaxWidth()
.padding(15.dp, 40.dp, 15.dp, 15.dp)
) {
Text(
text = stringResource(id = R.string.send_otp),
color = Color.White,
fontSize = 20.sp
)
}
}
@Preview
@Composable
fun MyViewPreview() {
MyView(onViewButtonClick = {})
}