mikelantzelo
mikelantzelo

Reputation: 331

Parent or child composable trigger click simultaneously

I have a parent composable

Row(
    modifier = modifier
        .width(tableWidth)
        .fillMaxHeight(),
) {
    cells.forEachIndexed { it1, cell ->
        Column(
            modifier = Modifier
                .height(tableHeight),
        ) {
            for (i in cell.indices) {
                Box(
                    modifier = Modifier
                        .width(sideBarWidth)
                        .height(headerHeight)
                        .clickable {
                          //event here
                        }
                ) {
                    eventContent(
                        studentScore =  cell,
                        listIndex = it1,
                        index = i,
                        onEvent = onEvent // event here
                    )
                }
            }
        }
    }
}

and a child which is the eventContent composable as showed above.

I want the parent or child composable to trigger both click events. How can I do this?

Upvotes: 6

Views: 2250

Answers (1)

z.g.y
z.g.y

Reputation: 6217

You can simply create a lambda callback where you can set all the things you need as its parameters.

@Composable
fun MyComposable(onEventCallback: (List, Int, Cell) -> Unit) {

    ...
    ...
    ...

      Box {
          eventContent(
              studentScore =  cell,
              listIndex = it1,
              index = i,
              onEvent = { selectedCell ->
                   onEventCallback(cells, i, selectedCell) // just pass everything on a single call
              }
          )
      }

    ...
    ...
}

And you can simply do separate things from the call site

 MyComposable { list, index, cell ->
       doSomethingOnTheList(list)
       doSomethingOnIndexAndCell(index, cell)
 }

Upvotes: 2

Related Questions