簡體   English   中英

預覽中不支持具有非默認參數的可組合函數,除非它們使用 @PreviewParameter 注釋

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

我有一個帶有 lambda 的Composable函數,用於獲取按鈕單擊操作。 我想預覽那個Composable函數。 但是在 @Composable 上面添加@Preview注釋后,這種 lambda 的可組合函數會@Composable

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

可組合功能看起來像

@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
            )
        }
 }

這個的應用看起來像

MyView(onViewButtonClick = {
                Log.d("ViewButtonClick","ViewButtonClick")
            }). 

如何使用 Lambda 查看此可組合函數的預覽?

要么為你的可組合提供一個默認的 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()
}

或者

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

正如問題在此處定義的那樣,不支持具有非默認參數的可組合函數 這意味着應該在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM