[英]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.我有一个带有 lambda 的Composable
函数,用于获取按钮单击操作。 I want to preview that Composable
function.我想预览那个Composable
函数。 But Composable function with this kind of lambda getting error after adding @Preview
annotation above @Composable
但是在 @Composable 上面添加@Preview
注释后,这种 lambda 的可组合函数会@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 ?如何使用 Lambda 查看此可组合函数的预览?
Either provide a default lambda to your composable, or you implement an empty lambda in your Preview要么为你的可组合提供一个默认的 lambda,要么在你的 Preview 中实现一个空的 lambda
@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 .这意味着应该在 Composable 函数中定义一个默认值,或者如果您使用的是自定义模型,则使用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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.