简体   繁体   中英

Composable functions with non-default parameters are not supported in Preview unless they are annotated with @PreviewParameter

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 = {})
}

As the issue defines here Composable functions with non-default parameters are not supported . This means that there should be a default value defined in the Composable function or if you are using a custom Model then use PreviewParameter .

@Preview(showBackground = true)
@Composable
fun Greeting(name: String = "Bharat") {
Surface(modifier = Modifier
    .fillMaxHeight()
    .fillMaxWidth()) {
    Text(text = "Hello $name!")
  }
}


@Preview   
@Composable
fun UserProfilePreview(
@PreviewParameter(UserPreviewParameterProvider::class) user: User) 
    {
       UserProfile(user)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM