Jetpack Compose 中没有连锁反应

问题描述 投票:0回答:3

当我点击

MyBox()
时没有连锁反应我已将 MyTheme(){} 添加到主 @Composable 屏幕,但它不起作用。是不是少了点什么?

@Composable
private MyBox(onClickInvoked: () -> Unit) {
    MyAppTheme(isSystemInDarkTheme()) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .clip(RoundedCornerShape(10.dp))
                .background(MaterialTheme.colors.onBackground)
                .clickable(onClick = { onClickInvoked.invoke() })
                .padding(horizontal = 10.dp, vertical = 15.dp)
        ) {
            Text(
                text = "My text",
                modifier = Modifier
                    .align(Alignment.CenterStart)
                    .padding(end = 95.dp)
                    .wrapContentWidth()
                    .wrapContentHeight(),
                color = MaterialTheme.colors.primary
            )
            Image(
                painter = painterResource(R.drawable.icon),
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 20.dp)
                    .size(60.dp)
            )
        }
    }
}
android kotlin android-jetpack-compose android-jetpack
3个回答
6
投票

使用 compose 1.0.5,我看到设置

indication
后默认的
clickable
LocalIndication.current
LocalIndication.current
PlatformRipple
。因此,当您将
clickable
设置为您的
Box
后,就会产生连锁反应。

在你的情况下,我认为波纹效果不会显示,因为你的盒子背景太暗(通常

MaterialTheme.colors.onBackground
在浅色主题上是黑色的)

我认为你可以更改波纹效果颜色以使其易于查看。

Surface(
    onClick = { onClickInvoked.invoke() },
    modifier = Modifier.fillMaxWidth(),
    shape = RoundedCornerShape(10.dp),
    color = MaterialTheme.colors.onBackground, // normally it is black on Light theme
    indication = rememberRipple(color = Color.White) // color for your ripple, you can use other suitable MaterialTheme.colors for your case to support Light/Dark mode
) {
    Box(modifier = Modifier.padding(horizontal = 10.dp, vertical = 15.dp)) {
        // your Box content
        ...
    }
}

有一个

Modifier.indication
,但经过测试,我发现它不能与
Modifier.clickable
一起使用,所以我使用
Surface


0
投票

我真的不知道你的主题是否有问题。我尝试通过材质主题,波纹会正常显示

MaterialTheme() {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .clip(RoundedCornerShape(10.dp))
            .background(MaterialTheme.colors.secondary)
            .clickable(onClick = { t = "My text" })
            .padding(horizontal = 10.dp, vertical = 15.dp)
    ) {
        Text(
            text = t,
            modifier = Modifier
                .align(Alignment.CenterStart)
                .padding(end = 95.dp)
                .wrapContentWidth()
                .wrapContentHeight(),
            color = MaterialTheme.colors.primary
        )
        Image(
            painter = painterResource(R.drawable.ic_launcher_foreground),
            modifier = Modifier
                .align(Alignment.CenterEnd)
                .padding(end = 20.dp)
                .size(60.dp),
            contentDescription = ""
        )
    }
}

0
投票

像这样提取扩展函数很好:

fun Modifier.setRippleEffectOnClick(onClick: () -> Unit): Modifier = composed {
    clickable(
        interactionSource = remember { MutableInteractionSource() },
        indication = rememberRipple(color = Color.Green),
        onClick = onClick
    )
}

因此,您可以将 clickable 替换为 setRippleEffectOnClick:

@Composable
private MyBox(onClickInvoked: () -> Unit) {
    MyAppTheme(isSystemInDarkTheme()) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .clip(RoundedCornerShape(10.dp))
                .background(MaterialTheme.colors.onBackground)
                .setRippleEffectOnClick { onClickInvoked.invoke() }
//                .clickable(onClick = { onClickInvoked.invoke() })
                .padding(horizontal = 10.dp, vertical = 15.dp)
        ) {
            Text(
                text = "My text",
                modifier = Modifier
                    .align(Alignment.CenterStart)
                    .padding(end = 95.dp)
                    .wrapContentWidth()
                    .wrapContentHeight(),
                color = MaterialTheme.colors.primary
            )
            Image(
                painter = painterResource(R.drawable.icon),
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 20.dp)
                    .size(60.dp)
            )
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.