繁体   English   中英

如何使可滑动的可组合只消耗直接在内容上的点击和拖动?

[英]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捕获,并且不会传递给可见的( GoogleMapComposable
  • 例如,在这张照片中,屏幕的青色部分代表BottomSheetState.Half state 中视图的GoogleMap部分,它不会收到点击。 BottomSheetState.Half

问题

如何使我的底部工作表实现仅消耗底部工作表内容上发生的单击/拖动?

问题在于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM