我有一个ModalBottomSheet,在单击底部工作表透明的背景后,我想要更改一个布尔值。如何访问此视图,单击侦听器?
我不想使用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()
})
}
) {
}
}我希望通过单击底部工作表背景(而不是底部工作表内容)来更改状态值。
发布于 2022-03-10 07:23:55
添加一个布尔mutableState变量,并在底部工作表状态更改时更改此值。
var isBottomSheetDismissed = remember { mutableStateOf(false) }
val bottomSheetState: ModalBottomSheetState =
rememberModalBottomSheetState(ModalBottomSheetValue.Expanded, confirmStateChange = {
isBottomSheetDismissed.value = it==ModalBottomSheetValue.Hidden
it != ModalBottomSheetValue.HalfExpanded
}, animationSpec = TweenSpec(durationMillis = 300, delay = 10))发布于 2022-11-30 09:02:09
在撰写中,可观察性优于回调,因此没有明确的侦听器可用。
我们可以通过观察隐藏时的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
https://stackoverflow.com/questions/71419682
复制相似问题