简体   繁体   English

修改 Jetpack Compose 中 IconButton 的波纹颜色

[英]Modify ripple color of IconButton in Jetpack Compose

How can I change the ripple color of an IconButton?如何更改 IconButton 的波纹颜色?

I tried doing it this way, but it doesn't change:我试过这样做,但它没有改变:

IconButton(
        onClick = { onClick() },
        modifier = Modifier.clickable(
          onClick = { onClick() },
          indication = rememberRipple(color = MyCustomTheme.colors.primary),
          interactionSource =  remember { MutableInteractionSource() },
        )
      )

I don't know if you found a way to make it work for the whole app but I found a way to do so.我不知道您是否找到了使其适用于整个应用程序的方法,但我找到了一种方法。 So I'm posting this incase someone else has a similar issue.所以我发布这个以防其他人有类似的问题。

You can set the custom RippleTheme object as described by Gabriele Mariotti's answer then you can pass the CompositionLocalProvider() as content in MaterialTheme.您可以按照 Gabriele Mariotti 的回答所述设置自定义RippleTheme object 然后您可以将CompositionLocalProvider()作为 MaterialTheme 中的内容传递。 The content from the app's theme can then be set as content for CompositionalLocalProvider() .然后可以将应用程序theme的内容设置为CompositionalLocalProvider()的内容。

Take a look here:看看这里:

private object JetNewsRippleTheme : RippleTheme {
    // Here you should return the ripple color you want
    // and not use the defaultRippleColor extension on RippleTheme.
    // Using that will override the ripple color set in DarkMode
    // or when you set light parameter to false
    @Composable
    override fun defaultColor(): Color = MaterialTheme.colors.primary

    @Composable
    override fun rippleAlpha(): RippleAlpha = RippleTheme.defaultRippleAlpha(
        Color.Black,
        lightTheme = !isSystemInDarkTheme()
    )
}

Then for your app theme it should be:那么对于您的应用主题,它应该是:

@Composable
fun JetNewsTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    MaterialTheme(
        colors = if (darkTheme) DarkColors else LightColors,
        typography = JetNewsTypography,
        shapes = JetNewsShapes
    ) {
        CompositionLocalProvider(
            LocalRippleTheme provides JetNewsRippleTheme,
            content = content
        )
    }
}

This method should work for the whole app unless you explicitly set another RippleTheme directly to Composables below the AppTheme hierarchy.此方法应适用于整个应用程序,除非您将另一个RippleTheme直接设置为AppTheme层次结构下的 Composables。 And it doesn't conflict with other types of ComposableLocalProvider values you may set directly to your other Composables .并且它不会与您可以直接设置给其他Composables的其他类型的ComposableLocalProvider值冲突。

Currently ( 1.0.0 ) your code doesn't work since the Ripple is implemented in a .clickable modifier defined inside the IconButton .目前( 1.0.0 )您的代码不起作用,因为 Ripple 是在 IconButton 内定义的.clickable修饰符中IconButton的。

The appearance of the Ripples is based on a RippleTheme and you can define a custom RippleTheme and apply to your composable with the LocalRippleTheme .涟漪的外观基于RippleTheme ,您可以定义自定义RippleTheme并使用LocalRippleTheme应用于您的可组合。

Something like:就像是:

private object RippleCustomTheme: RippleTheme {

    //Your custom implementation...
    @Composable
    override fun defaultColor() =
        RippleTheme.defaultRippleColor(
            Color.Red, 
            lightTheme = true
        )

    @Composable
    override fun rippleAlpha(): RippleAlpha =
        RippleTheme.defaultRippleAlpha(
            Color.Black,
            lightTheme = true
        )
}

and:和:

CompositionLocalProvider(LocalRippleTheme provides  RippleCustomTheme) {
    IconButton(
        onClick = { },
    ) {
        Icon(Icons.Filled.Add, "")
    }
}

在此处输入图像描述 在此处输入图像描述

It's a bit late, but maybe this code will help someone.有点晚了,但也许这段代码会对某人有所帮助。

To make your code work you need to apply interactionSource to the button itself too.要使您的代码正常工作,您还需要将interactionSource 应用于按钮本身。

You can change the color locally in this way.您可以通过这种方式在本地更改颜色。 It is important to apply height after calling the indication in order to keep the correct button and ripple dimensions.在调用指示后应用高度以保持正确的按钮和波纹尺寸非常重要。

val interactionSource = remember { MutableInteractionSource() }
val rippleColor = Color.RED
val shape = RoundedCornerShape(size = 16.dp)

Button(
    modifier = Modifier
        .clip(shape = shape)
        .indication(
            interactionSource = interactionSource,
            indication = rememberRipple(color = rippleColor)
        )
        .height(height = 40.dp),
    shape = shape,
    interactionSource = interactionSource,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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