Answers for "Reusing Composables in Jetpack Compose Kotlin Android Studio"

0

Reusing Composables in Jetpack Compose Kotlin Android Studio

// CommonLayouts.kt

@Composable
fun Header(content: @Composable () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.Green)
            .padding(horizontal = 8.dp)
    ) {
        content();
    }
}

@Composable
fun Footer(color: Color, message: String) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(color)
            .padding(horizontal = 8.dp)
    ) {
        Box(contentAlignment = Alignment.Center) {
            Text(text = message)
        }
    }
}

/* ------------------------X---------------------------- */

// MainActivity.kt
class MainActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentLayout {
            Header {
                Button(onClick = {/* TODO */}) {
                    Text(text = "Click me!")
                }
            }
            /* More content */
            Footer(Color.Red, "Jetpack Compose")
        }
    }
}
Posted by: Guest on May-13-2021

Code answers related to "Reusing Composables in Jetpack Compose Kotlin Android Studio"

Browse Popular Code Answers by Language