Last active
June 13, 2021 01:20
-
-
Save Bruno125/25c7fdd1280d3821fb51b008935b09f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.foundation.border | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.material.Button | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.composed | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
@Preview(showBackground = true) | |
@Composable | |
fun TestLayout() { | |
Row { | |
Column1(Modifier.weight(1f)) | |
Column2(Modifier.weight(1f)) | |
} | |
} | |
@Composable | |
fun Column1(modifier: Modifier = Modifier) { | |
Column(modifier.withBorder()) { | |
Button(onClick = { }) { Text("Button") } | |
Button(onClick = { }) { Text("Button") } | |
} | |
} | |
@Composable | |
fun Column2(modifier: Modifier = Modifier) { | |
Column(modifier.withBorder()) { | |
Text("Large Loremp Text Ipsum") | |
Text("Large Loremp Text Ipsum") | |
} | |
} | |
fun Modifier.withBorder() = composed { border(1.dp, Color.Red) } |
Yep @CarlitosDroid, and that's because Composables are meant to be reusable, so you can't make assumptions about the container in which they will be. That's why the recommended approach is to always pass an optional modifier to the function. In that way, layout logic can be delegated to the container.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kind of interesting that weight modifier is only enabled in certain scopes like RowScope, ColumnScope. I had some problems trying to use weight inside a Card(because it hasn't a scope). Thanks!