简体   繁体   English

我们如何从jetpack compose中的切换按钮中消除涟漪效应?

[英]How we can remove ripple effect from toggle button in jetpack compose?

I have the toggle button in android jetpack compose, I want to remove ripple effect, when I click the toggle buttons.我在 android jetpack compose 中有切换按钮,当我单击切换按钮时,我想消除涟漪效应。 I try to find a solution on internet, but I did not find clear solution for this kind of example.我试图在互联网上找到解决方案,但我没有找到此类示例的明确解决方案。 Is there any idea?有什么想法吗?

   @Composable
 fun MainScreen() {

Column(
    modifier = Modifier
        .fillMaxSize(),


  ) {
  
        var selected by remember { mutableStateOf(false)}

        MainRow(
            name = "name1",
            change = selected, onCheckedChange = {
                selected = it

            }))}}}
        
@Composable
fun MainRow(
name: String,
change:Boolean,
onCheckedChange: (Boolean) -> Unit

 ) {

 Row(
    modifier = Modifier
        .padding(8.dp)
        .fillMaxWidth(),

    horizontalArrangement = Arrangement.SpaceBetween

) {

    Text(
        text = name,
       
    )

        Switch(
            modifier = Modifier
                .scale(1f),
            checked = change,
          
            onCheckedChange = onCheckedChange,
            colors = SwitchDefaults.colors(
                checkedThumbColor = Color.Red,
                uncheckedThumbColor = Color.Green,
                checkedTrackColor = Color.Yellow,
                uncheckedTrackColor = Color.Blue
            ))}

From the documentation :文档中:

interactionSource - the MutableInteractionSource representing the stream of Interactions for this Switch. interactionSource - MutableInteractionSource 表示此 Switch 的交互的 stream。 You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this Switch in different Interactions.如果您想观察交互并自定义此 Switch 在不同交互中的外观/行为,您可以创建并传入您自己记住的 MutableInteractionSource。

So you can write your own MutableInteractionSource like this:因此,您可以像这样编写自己的MutableInteractionSource

class DisabledInteractionSource : MutableInteractionSource {

    override val interactions: Flow<Interaction> = emptyFlow()

    override suspend fun emit(interaction: Interaction) {}

    override fun tryEmit(interaction: Interaction) = true

}

And use it like this:并像这样使用它:

    Switch(
        modifier = Modifier
            .scale(1f),
        checked = change,
        interactionSource = remember { DisabledInteractionSource() },
        onCheckedChange = onCheckedChange,
        colors = SwitchDefaults.colors(
            checkedThumbColor = Color.Red,
            uncheckedThumbColor = Color.Green,
            checkedTrackColor = Color.Yellow,
            uncheckedTrackColor = Color.Blue
        )
    )

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

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