mino24
mino24

Reputation: 5

Jetpack Compose custom layout clicks don't work

I created my own class called MyInputRow.

@Composable
fun MyInputRow(
    title: String,
    modifier: Modifier = Modifier,
    bgColor: Color,
    icon: Painter,
    clickAction: Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(6.dp)
            .background(color = bgColor)
            .clickable { clickAction },
        verticalAlignment = Alignment.CenterVertically,
    )
    {
        Text(
            modifier = Modifier
                .weight(1f)
                .padding(6.dp)
                .wrapContentWidth(Alignment.Start),
            text = title,
        )
        Image(
            painter = icon,
            contentDescription = "icon"
        )
    }
}

But, this clickAction does not work. Specifically, clickAction is called for some reason when the screen is displayed, and clickAction is not called when Row is clicked.
It is called as follows.

class AddActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        setContent {
            MyTheme {
                    ...
                    AddMain()
            }
        }
    }
}

@Composable
fun AddMain() {
        BarcodeLayout()
    },
}

@Composable
fun BarcodeLayout() {
    Column(Modifier.fillMaxWidth()) {
        Text(text = {...})

        MyInputRow(
            title = ...,
            bgColor = ...,
            icon = ...,
            clickAction = scanBarcode()
        )
    }
}

fun scanBarcode() {
 Timber.d("call scanBarcode")
}

I don't know why. Please help me.

Upvotes: 0

Views: 1119

Answers (1)

krupa parekh
krupa parekh

Reputation: 666

There are 2 options you can try.

Option 1:

.clickable { clickAction() }

or Option 2:

.clickable(clickAction)

Upvotes: 6

Related Questions