简体   繁体   English

如何在 Jetpack Compose 中使用图标切换按钮?

[英]How to use Icon Toggle button in Jetpack Compose?

I want to use Icon button in Jetpack Compose, but I couldn't understand Jetpack Compose docs.我想在 Jetpack Compose 中使用图标按钮,但我无法理解 Jetpack Compose 文档。 Can someone share a sample code for toggle button similar to this one ?有人可以分享与此类似的切换按钮的示例代码吗?
When user clicks on a button, I want to animate it like in Instagram with a bounce animation.当用户单击一个按钮时,我想像在 Instagram 中一样使用弹跳 animation 对其进行动画处理。

You can combine IconToggleButton with transitions.您可以将IconToggleButton与转换结合使用。 Sample code (using version 1.0.0-beta05):示例代码(使用版本 1.0.0-beta05):

import android.annotation.SuppressLint
import androidx.compose.animation.animateColor
import androidx.compose.animation.core.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@SuppressLint("UnusedTransitionTargetStateParameter")
@Composable
fun FavoriteButton(
    isChecked: Boolean,
    onClick: () -> Unit
) {
    IconToggleButton(
        checked = isChecked,
        onCheckedChange = { onClick() }
    ) {
        val transition = updateTransition(isChecked, label = "Checked indicator")

        val tint by transition.animateColor(
            label = "Tint"
        ) { isChecked ->
            if (isChecked) Color.Red else Color.Black
        }

        val size by transition.animateDp(
            transitionSpec = {
                if (false isTransitioningTo true) {
                    keyframes {
                        durationMillis = 250
                        30.dp at 0 with LinearOutSlowInEasing // for 0-15 ms
                        35.dp at 15 with FastOutLinearInEasing // for 15-75 ms
                        40.dp at 75 // ms
                        35.dp at 150 // ms
                    }
                } else {
                    spring(stiffness = Spring.StiffnessVeryLow)
                }
            },
            label = "Size"
        ) { 30.dp }

        Icon(
            imageVector = if (isChecked) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder,
            contentDescription = null,
            tint = tint,
            modifier = Modifier.size(size)
        )
    }
}

@Preview("Favorite Button")
@Composable
fun FavoriteButtonPreview() {
    val (isChecked, setChecked) = remember { mutableStateOf(false) }
    MaterialTheme {
        Surface {
            FavoriteButton(
                isChecked = isChecked, 
                onClick = { setChecked(!isChecked) }
            )
        }
    }
}

These are the required dependencies for this sample:这些是此示例所需的依赖项:

dependencies {
    implementation 'androidx.core:core-ktx:1.6.0-alpha01'

    implementation "androidx.compose.ui:ui:1.0.0-beta05"
    implementation "androidx.compose.material:material:1.0.0-beta05"
    implementation "androidx.compose.ui:ui-tooling:1.0.0-beta05"
    implementation 'androidx.activity:activity-compose:1.3.0-alpha06'
}

For more details about transition and keyframes and ways to customize them, see Compose's Animation documentation .有关transitionkeyframes以及自定义它们的方法的更多详细信息,请参阅 Compose 的Animation 文档

Maybe it's a good idea to use Chips instead.也许改用芯片是个好主意。

Filter chips use tags or descriptive words to filter content.过滤器芯片使用标签或描述性词来过滤内容。 They can be a good alternative to toggle buttons or checkboxes.它们可以是切换按钮或复选框的不错选择。

Also it's possible to use Modifier.selectable .也可以使用Modifier.selectable

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

相关问题 我们如何在jetpack compose中为每行使用切换按钮? - How we can use toggle button for per row in jetpack compose? 在 Jetpack compose 中添加抽屉切换按钮 - Add drawer toggle button in Jetpack compose 如何在 Jetpack Compose 中按高度缩放图标宽度 - How to scale icon width by height in Jetpack Compose 如何在 Jetpack Compose 中使用小部件? - How to use widgets in Jetpack Compose? 如何在jetpack compose中使用LayoutAlign - how to use LayoutAlign in jetpack compose 如何在 Jetpack Compose 中使用 Viewmodel - How to use Viewmodel with Jetpack compose 我们如何从jetpack compose中的切换按钮中消除涟漪效应? - How we can remove ripple effect from toggle button in jetpack compose? 在没有单选按钮的情况下在 Jetpack Compose 中创建切换按钮组 - Create Toggle Button Group in Jetpack Compose without Radio Buttons 如果在 android 喷气背包中为 NavigationBar 组合选择和取消选择图标,如何更改图标,就像我们在 xml 中为选定的 state 使用的选择器一样? - How to change icon if selected and unselected in android jetpack compose for NavigationBar like selector we use in xml for selected state? 如何使用jetpack compose将按钮标记为可选择 - How to mark a button as selectable with jetpack compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM