简体   繁体   中英

Access a widget from another widget in jetpack compose

Using traditional XML you can get the instance of a View using it's id or tag.

How is this possible in jetpack compose?

Card(elevation = 1.dp, shape = RoundedCornerShape(8.dp), color = Color.Transparent) {
      // TARGET is here
      Padding(padding = 8.dp) {
           Text(text = "Net stat", style = +themeTextStyle { h6 })
      }
}
Text(text = netStatusState.value)
HeightSpacer(height = 10.dp)
Divider()
HeightSpacer(height = 10.dp)
Card(elevation = 1.dp, shape = RoundedCornerShape(8.dp), color = Color.Transparent) {
     Padding(padding = 8.dp) {
           Clickable(onClick = {
                            // MODIFY (Remove, change element, update attr) the target
                     }) {
              Button(text = "Click me if you can")
           }
     }
}

Is it even needed such a feature, or it's all done using the state ?

Composables do not have IDs, and you generally shouldn't need to get an instance of a composable. It is helpful to think of Composable functions as print statements. Just like a println() function takes some data and writes it to the console, Composable functions take data and measure/layout/draw this data onto the screen.

If you change the data, and the data is in a class annotated with @Model, the Jetpack compose system will automatically call the appropriate functions again using the new state and your UI will be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM