Jetpack compose 预览未显示在 Google Rally 示例应用程序的组件之一中

Jetpack compose Preview not showing in one of the components of the Google Rally Sample app

我正在尝试预览 RallyAlertDialog 可组合项,它是 Google 中 Rally 示例中使用的组件之一。我要预览的代码如下:

@Composable
fun RallyAlertDialog(
    onDismiss: () -> Unit,
    bodyText: String,
    buttonText: String
) {
    RallyDialogThemeOverlay {
        AlertDialog(
            onDismissRequest = onDismiss,
            text = { Text(bodyText) },
            buttons = {
                Column {
                    Divider(
                        Modifier.padding(horizontal = 12.dp),
                        color = MaterialTheme.colors.onSurface.copy(alpha = 0.2f)
                    )
                    TextButton(
                        onClick = onDismiss,
                        shape = RectangleShape,
                        contentPadding = PaddingValues(16.dp),
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text(buttonText)
                    }
                }
            }
        )
    }
}

可组合的 RallyDialogThemeOverlay(content: @Composable () -> Unit) 在另一个 kotlin 文件中定义,RallyTheme.kt,并且是自定义的 MaterialTheme。

我用于@Preview 的代码如下:

@Preview(showBackground = true)
@Composable
fun PreviewRallyAlertDialog() {
    val alertMessage = "Heads up, you've used up 90% of your Shopping budget for this month."
    val buttonText = "Dismiss".uppercase(Locale.getDefault())
    RallyAlertDialog(
        onDismiss = { /*TODO*/ },
        bodyText = alertMessage,
        buttonText = buttonText
    )
}

既然不能预览也没有报错,我哪里做错了?

谢谢

现在不支持对话框预览。 参见 https://issuetracker.google.com/issues/186502047

更新:自 Android Studio Chipmunk Canary 5 起支持。