如何在任何Jetpack合成视图上禁用涟漪效果? [英] How to disable ripple effect on any Jetpack Compose view?

本文介绍了如何在任何Jetpack合成视图上禁用涟漪效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jetpack Compose中,如何在单击项目时消除(或更改)涟漪效果?

这是NavigationBar来自Material Design 3

的示例
var selectedItem by remember { mutableStateOf(0) }
val items = listOf("Songs", "Artists", "Playlists")

NavigationBar {
    items.forEachIndexed { index, item ->
        NavigationBarItem(
            icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
            label = { Text(item) },
            selected = selectedItem == index,
            onClick = { selectedItem = index }
        )
    }
}

正在尝试使用

添加Modifier
modifier = Modifier.clickable(interactionSource = interactionSource,indication = null){}

NavigationBarNavigationBarItem上都不起作用。

推荐答案

可以通过提供LocalRippleTheme来完成。CompositionLocalProvidercontent中的所有视图都不会产生涟漪。

CompositionLocalProvider(
    LocalRippleTheme provides ClearRippleTheme
) {
    NavigationBar {
        items.forEachIndexed { index, item ->
            NavigationBarItem(
                icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
                label = { Text(item) },
                selected = selectedItem == index,
                onClick = { selectedItem = index }
            )
        }
    }
}

ClearRippleTheme

object ClearRippleTheme : RippleTheme {
    @Composable
    override fun defaultColor(): Color = Color.Transparent

    @Composable
    override fun rippleAlpha() = RippleAlpha(
        draggedAlpha = 0.0f,
        focusedAlpha = 0.0f,
        hoveredAlpha = 0.0f,
        pressedAlpha = 0.0f,
    )
}

这篇关于如何在任何Jetpack合成视图上禁用涟漪效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆