[英]How can I make a swipeable composable only consume clicks and drags directly on content?
BottomSheetState.Gone
过渡到BottomSheetState.Half
的 FAB。GoogleMap
Composable
。maxHeight
参数是父Composable
的全高(以像素为单位)。@ExperimentalMaterialApi
@Composable
fun AnchoredBottomSheet(
state: BottomSheetState,
maxHeight: Float,
content: @Composable () -> Unit,
) {
val swipeableState = rememberSwipeableState(initialValue = state)
val anchors = mapOf(
BottomSheetState.Gone.toAnchor(maxHeight),
BottomSheetState.Half.toAnchor(maxHeight),
BottomSheetState.Full.toAnchor(maxHeight),
)
Column(
modifier = Modifier
.height(maxHeight.dp)
.swipeable(
enabled = swipeableState.currentValue != BottomSheetState.Gone,
state = swipeableState,
orientation = Orientation.Vertical,
anchors = anchors,
)
.offset {
IntOffset(
0,
swipeableState.offset.value.roundToInt()
)
}
) {
content()
}
}
BottomSheetState.Half
state 或BottomSheetState.Gone
时,GoogleMap
部分,AnchoredBottomSheet
捕获,并且不会传递给可见的( GoogleMap
) Composable
。BottomSheetState.Half
state 中视图的GoogleMap
部分,它不会收到点击。 如何使我的底部工作表实现仅消耗底部工作表内容上发生的单击/拖动?
问题在于Modifier
扩展调用的顺序。 Modifier.swipeable()
应该在Modifier.offset()
之后发生。
@ExperimentalMaterialApi
@Composable
fun AnchoredBottomSheet(
state: BottomSheetState,
maxHeight: Float,
content: @Composable () -> Unit,
) {
val swipeableState = rememberSwipeableState(initialValue = state)
val anchors = mapOf(
BottomSheetState.Gone.toAnchor(maxHeight),
BottomSheetState.Half.toAnchor(maxHeight),
BottomSheetState.Full.toAnchor(maxHeight),
)
Column(
modifier = Modifier
.offset {
IntOffset(
x = 0,
y = swipeableState.offset.value.roundToInt(),
)
}
.swipeable(
enabled = swipeableState.currentValue != BottomSheetState.Gone,
state = swipeableState,
orientation = Orientation.Vertical,
anchors = BottomSheetState.getAnchors(maxHeight),
)
) {
content()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.