https://kotlinlang.org logo
#compose
Title
# compose
m

Mark Murphy

09/27/2020, 6:44 PM
Is there a known limitation on composable local functions? This is the
RadioButtonSample()
from the SDK:
Copy code
@Composable
fun RadioGroupSample() {
    val radioOptions = listOf("Calls", "Missed", "Friends")
    val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }
    Column {
        radioOptions.forEach { text ->
            Row(Modifier
                .fillMaxWidth()
                .preferredHeight(56.dp)
                .selectable(
                    selected = (text == selectedOption),
                    onClick = { onOptionSelected(text) }
                )
                .padding(horizontal = 16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(
                    selected = (text == selectedOption),
                    onClick = { onOptionSelected(text) }
                )
                Text(
                    text = text,
                    style = MaterialTheme.typography.body1.merge(),
                    modifier = Modifier.padding(start = 16.dp)
                )
            }
        }
    }
}
It works, including changing the checked state of the
RadioButton()
widgets on click events. This is a refactored version of that sample, pulling the
Row()
declaration out into a local function:
Copy code
@Composable
fun RadioGroupSample() {
  val radioOptions = listOf("Calls", "Missed", "Friends")
  val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }

  @Composable
  fun RadioRow(text: String) {
    Row(Modifier
      .fillMaxWidth()
      .preferredHeight(56.dp)
      .selectable(
        selected = (text == selectedOption),
        onClick = { onOptionSelected(text) }
      )
      .padding(horizontal = 16.dp),
      verticalAlignment = Alignment.CenterVertically
    ) {
      RadioButton(
        selected = (text == selectedOption),
        onClick = { onOptionSelected(text) }
      )
      Text(
        text = text,
        style = MaterialTheme.typography.body1.merge(),
        modifier = Modifier.padding(start = 16.dp)
      )
    }
  }

  Column {
    radioOptions.forEach { text -> RadioRow(text) }
  }
}
It compiles and renders, but the
RadioRow()
widgets do not get recomposed when the state changes.
a

Adam Powell

09/27/2020, 7:30 PM
Not sure if we're tracking any known issues around this so far, please file it
m

Mark Murphy

09/27/2020, 7:37 PM
I looked for one before posting here and came up dry, so... https://issuetracker.google.com/issues/169471826
👍 1