android - 处理 jetpack compose 中 modalBottomsheet 内容中的单击事件

标签 android android-jetpack-compose bottom-sheet

我有一个 ModalBottomSheet,我想在单击 Bottom Sheet 透明背景后更改 bool 值。我如何访问此 View 点击监听器?

我不想使用 BottomSheetScaffold。

fun ScheduleJobFromQueuedBottomSheet(
    viewModel: ProductViewModel,
    onClickCancel: () -> Unit,
    onQueuedScheduleRequestResultIsBack: () -> Unit,
) {
**var state by remember { mutableStateOf(false) }**

val bottomSheetState: ModalBottomSheetState =
        rememberModalBottomSheetState(ModalBottomSheetValue.Expanded, confirmStateChange = {
            it != ModalBottomSheetValue.HalfExpanded
        }, animationSpec = TweenSpec(durationMillis = 300, delay = 10))


    ModalBottomSheetLayout(
        sheetState = bottomSheetState,
        sheetShape = RoundedCornerShape(topStart = custom, topEnd = custom),
        scrimColor = ModalBottomSheetDefaults.scrimColor.copy(alpha = 0.40f),
        sheetContent = {
            ReviewBookingScheduledBottomSheet(modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight(),
                viewModel = viewModel,
                onClickCancel = {
                    onClickCancel.invoke()
                }, onScheduledRequestResultIsBack = {
                    onQueuedScheduleRequestResultIsBack.invoke()
                })
        }
    ) {

    }
}

我想通过单击 Bottom Sheet 背景(不是 Bottom Sheet 内容)来更改状态值

最佳答案

在 Compose 中,可观察性优于回调,因此没有可用的显式监听器。

我们可以通过观察隐藏时的bottomSheetState状态变化来观察透明背景点击:

fun ScheduleJobFromQueuedBottomSheet(
    viewModel: ProductViewModel,
    onClickCancel: () -> Unit,
    onQueuedScheduleRequestResultIsBack: () -> Unit,
) {
var state by remember { mutableStateOf(false) }

val bottomSheetState: ModalBottomSheetState =
        rememberModalBottomSheetState(ModalBottomSheetValue.Expanded, confirmStateChange = {
            it != ModalBottomSheetValue.HalfExpanded
        }, animationSpec = TweenSpec(durationMillis = 300, delay = 10))

LaunchedEffect(bottomSheetState) {
    snapshotFlow { bottomSheetState.currentValue }
        .filter { it == Hidden }
        .collect { state = true }
}


    ModalBottomSheetLayout(
        sheetState = bottomSheetState,
        sheetShape = RoundedCornerShape(topStart = custom, topEnd = custom),
        scrimColor = ModalBottomSheetDefaults.scrimColor.copy(alpha = 0.40f),
        sheetContent = {
            ReviewBookingScheduledBottomSheet(modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight(),
                viewModel = viewModel,
                onClickCancel = {
                    onClickCancel.invoke()
                }, onScheduledRequestResultIsBack = {
                    onQueuedScheduleRequestResultIsBack.invoke()
                })
        }
    ) {

    }
}

不幸的是,这无法区分 bottomSheetState 更改是由于点击透明背景还是由于状态的其他更改。如果您有一个用例需要观察透明背景点击而不是其他关闭操作,请在此处留下评论:https://buganizer.corp.google.com/issues/256755735

关于android - 处理 jetpack compose 中 modalBottomsheet 内容中的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71419682/

相关文章:

android - BottomSheet 中的 RecyclerView 未按预期工作

android - classes.jar' 已经包含条目 'META-INF/module_name_debug.kotlin_module' ,无法覆盖

android - 来自模态 BottomSheetFragment 的 BottomSheetCallback

android - 是否可以将 OpenMP 库与 Android NDK 一起使用?

android - Android 中的只读配置文件

java - 通过代码更改样式主题中的文本颜色

android - 如何制作一个文本,它会水平滚动其内容,而内容长于 android jetpack compose 中的大小?

android - 扩展时更改 ModalDrawer 的大小,Jetpack Compose

android - 如何允许 BottomSheetDialog 的外部触摸?

android - 谷歌资料库更新后, Bottom Sheet View 无法完全展开吗?