如何启用 Compose 小吃栏的滑动关闭行为

Har*_*r S 9 android android-snackbar android-jetpack-compose

下面的简单代码用于显示 Compose Snackbar

当 onClick 事件发生时,此代码正确显示 Snackbar。

 val scaffoldState = rememberScaffoldState() // this contains the `SnackbarHostState`
    val coroutineScope = rememberCoroutineScope()

    Scaffold(
        modifier = Modifier,
        scaffoldState = scaffoldState // attaching `scaffoldState` to the `Scaffold`
    ) {
        Button(
            onClick = {
                coroutineScope.launch { // using the `coroutineScope` to `launch` showing the snackbar
                    // taking the `snackbarHostState` from the attached `scaffoldState`
                    val snackbarResult = scaffoldState.snackbarHostState.showSnackbar(
                        message = "This is your message",
                        actionLabel = "Do something."
                    )
                    when (snackbarResult) {
                        SnackbarResult.Dismissed -> Log.d("SnackbarDemo", "Dismissed")
                        SnackbarResult.ActionPerformed -> Log.d("SnackbarDemo", "Snackbar's button clicked")
                    }
                }
            }
        ) {
            Text(text = "A button that shows a Snackbar")
        }
    }
Run Code Online (Sandbox Code Playgroud)

如何在右/左滑动时关闭小吃栏?

Phi*_*hov 10

没有SnackbarHost这样的功能。但你可以用论证来扩展它nackbarHost

此外,如果您希望小吃栏仅通过滑动即可消失,您可能需要将持续时间设置为Indefinite

Scaffold(
    modifier = Modifier,
    scaffoldState = scaffoldState,
    snackbarHost = { SwipeableSnackbarHost(it) } // modification 1
) {
    Button(
        onClick = {
            coroutineScope.launch {
                val snackbarResult = scaffoldState.snackbarHostState.showSnackbar(
                    message = "This is your message",
                    actionLabel = "Do something.",
                    duration = SnackbarDuration.Indefinite,  // modification 2
                )
                when (snackbarResult) {
                    SnackbarResult.Dismissed -> Log.d("SnackbarDemo", "Dismissed")
                    SnackbarResult.ActionPerformed -> Log.d(
                        "SnackbarDemo",
                        "Snackbar's button clicked"
                    )
                }
            }
        }
    ) {
        Text(text = "A button that shows a Snackbar")
    }
}
Run Code Online (Sandbox Code Playgroud)

SwipeableSnackbarHost受到这个答案的启发

enum class SwipeDirection {
    Left,
    Initial,
    Right,
}

@Composable
fun SwipeableSnackbarHost(hostState: SnackbarHostState) {
    if (hostState.currentSnackbarData == null) { return }
    var size by remember { mutableStateOf(Size.Zero) }
    val swipeableState = rememberSwipeableState(SwipeDirection.Initial)
    val width = remember(size) {
        if (size.width == 0f) {
            1f
        } else {
            size.width
        }
    }
    if (swipeableState.isAnimationRunning) {
        DisposableEffect(Unit) {
            onDispose {
                when (swipeableState.currentValue) {
                    SwipeDirection.Right,
                    SwipeDirection.Left -> {
                        hostState.currentSnackbarData?.dismiss()
                    }
                    else -> {
                        return@onDispose
                    }
                }
            }
        }
    }
    val offset = with(LocalDensity.current) {
        swipeableState.offset.value.toDp()
    }
    SnackbarHost(
        hostState,
        snackbar = { snackbarData ->
            Snackbar(
                snackbarData,
                modifier = Modifier.offset(x = offset)
            )
        },
        modifier = Modifier
            .onSizeChanged { size = Size(it.width.toFloat(), it.height.toFloat()) }
            .swipeable(
                state = swipeableState,
                anchors = mapOf(
                    -width to SwipeDirection.Left,
                    0f to SwipeDirection.Initial,
                    width to SwipeDirection.Right,
                ),
                thresholds = { _, _ -> FractionalThreshold(0.3f) },
                orientation = Orientation.Horizontal
            )
    )
}
Run Code Online (Sandbox Code Playgroud)