0

问题描述

我正在尝试查看我AlertDialog在撰写中的预览,但我没有得到它,即使它没有显示任何错误,预览也不会呈现它。我确定问题仅出在这个可组合功能上,因为我可以正常看到其他功能的预览。

警告

我收到了这个警告,但我不太明白这是一个没有呈现我的对话框的问题。

在此处输入图像描述

在运行时正常工作

我的对话框在执行环境中正常出现,这让我相信这确实是预览功能的问题:

在此处输入图像描述

我的代码

编写相关版本

我正在使用build.gradle.kts文件,并引用我的撰写相关版本,如下所示:

object Compose {
    const val composeVersion = "1.0.5"
    const val composeCompilerVersion = "1.0.5"
    const val material = "androidx.compose.material:material:$composeVersion"
    const val ui = "androidx.compose.ui:ui:$composeVersion"
    const val uiTooling = "androidx.compose.ui:ui-tooling:$composeVersion"
    const val uiToolingPreview = "androidx.compose.ui:ui-tooling-preview:$composeVersion"
    const val runtime = "androidx.compose.runtime:runtime:$composeVersion"
    const val compiler = "androidx.compose.compiler:compiler:$composeCompilerVersion"

    private const val navigationVersion = "2.4.0-beta02"
    const val navigation = "androidx.navigation:navigation-compose:$navigationVersion"

    private const val hiltNavigationComposeVersion = "1.0.0-beta01"
    const val hiltNavigationCompose = "androidx.hilt:hilt-navigation-compose:$hiltNavigationComposeVersion"

    private const val activityComposeVersion = "1.4.0"
    const val activityCompose = "androidx.activity:activity-compose:$activityComposeVersion"

    private const val lifecycleVersion = "2.4.0"
    const val viewModelCompose = "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycleVersion"
}

呈现对话框的组件

@Composable
fun InvalidValuesDialog(
    @StringRes title: Int,
    @StringRes message: Int,
    @StringRes buttonText: Int,
    checkBoxIsChecked: Boolean,
    onCheckChange: () -> Unit,
    onDismissRequest: () -> Unit,
    buttonClick: () -> Unit,
) {
    val spaces = LocalSpacing.current
    AlertDialog(
        onDismissRequest = onDismissRequest,
        title = {
            Text(
                text = stringResource(id = title),
                fontSize = 20.sp
            )
        },
        text = {
            Column {
                Text(
                    text = stringResource(id = message),
                    fontSize = 18.sp
                )
                VerticalSpacer(spaces.small)
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Checkbox(checked = checkBoxIsChecked, onCheckedChange = { onCheckChange() })
                    HorizontalSpacer(spaces.extraSmall)
                    Text(
                        modifier = Modifier.clickable { onCheckChange() },
                        text = stringResource(id = R.string.keep_value_informed),
                        fontSize = 16.sp
                    )
                }
            }
        },
        buttons = {
            Button(
                modifier = Modifier.fillMaxWidth(),
                onClick = buttonClick
            ) {
                Text(text = stringResource(id = buttonText))
            }
        }
    )
}

预览功能

@Composable
@Preview
fun InvalidValuesDialogCheckedPreview() {
    CalorieTrackerTheme {
        InvalidValuesDialog(
            title = R.string.high_height,
            message = R.string.high_height_message,
            buttonText = R.string.next,
            checkBoxIsChecked = true,
            onCheckChange = {},
            onDismissRequest = {},
            buttonClick = {}
        )
    }
}

预览结果

在此处输入图像描述

我的尝试失败了

我认为对话框可能无法呈现,因为它不够大,无法在屏幕上显示。所以我决定把它放在一个Box最大屏幕尺寸的里面,但它仍然不起作用。

@Composable
@Preview(showBackground = true)
fun InvalidValuesDialogCheckedPreview() {
    CalorieTrackerTheme {
        Box(modifier = Modifier.fillMaxSize()) {
            InvalidValuesDialog(
                title = R.string.high_height,
                message = R.string.high_height_message,
                buttonText = R.string.next,
                checkBoxIsChecked = true,
                onCheckChange = {},
                onDismissRequest = {},
                buttonClick = {}
            )
        }
    }
}

尝试失败结果

白屏预览

4

1 回答 1

2

预览窗口不支持多窗口 我认为错误是有效的,并且在预览或预览交互模式下仍然无法看到Toast或snackbar。

顺便说一句,我认为当您使用androidx.compose.ui:ui-tooling时,您不再需要androidx.compose.ui:ui-tooling-preview

于 2022-02-02T21:20:27.527 回答