Modifier
Conditional Modifier
Section titled “Conditional Modifier”fun Modifier.conditional(condition: Boolean, modifier: Modifier.() -> Modifier): Modifier { return if (condition) then(modifier(Modifier)) else this}onGloballyPositioned
Section titled “onGloballyPositioned”Retrieve widget layout (width, height)
Modifier.onGloballyPositioned { textWidth = it.size.width // Int}Scrollable Column or Row
Section titled “Scrollable Column or Row”Use .verticalScroll() or .horizontalScroll()
val state = rememberScrollState()
Column(Modifier.verticalScroll(state)){
}After binding the state, use .animateScrollTo() to scroll to a location with animation
size & requiredSize
Section titled “size & requiredSize”requiredSize | size |
|---|---|
| Force to set the size no matter what | Respect parent layout size, if overflow then fit in the parent layout |
Box(modifier = Modifier.size(100.dp))
Box(modifier = Modifier.requiredSize(100.dp))zIndex
Section titled “zIndex”Same as z-index in CSS, allows you to change the layer of composables
Modifier.zIndex(1f)Modifier.zIndex(2f) // <- higher layerWeight
Section titled “Weight”weight() takes the remaining space of the composables
Modifier.weight(1f)
@Composablefun WeightExample() { Column(modifier = Modifier.fillMaxHeight().systemBarsPadding()) { Row(modifier = Modifier.fillMaxWidth().height(50.dp)) { Text(text = "Weight 2", modifier = Modifier.weight(2f) .background(Color.Red).padding(8.dp)) Text(text = "Weight 2", modifier = Modifier.weight(2f) .background(Color.Green).padding(8.dp)) Text(text = "Weight 3", modifier = Modifier.weight(3f) .background(Color.Blue).padding(8.dp)) } Row(modifier = Modifier.fillMaxWidth().height(50.dp)) { Text(text = "Weight 2", modifier = Modifier.weight(2f) .background(Color.Yellow).padding(8.dp)) Text(text = "Weight 1", modifier = Modifier.weight(1f) .background(Color.Cyan).padding(8.dp)) } }}Border
Section titled “Border”Modifier.border(2.dp, Color.Green, CircleShape)Safe Area
Section titled “Safe Area”For all bars (bottom nav controller & topbar)
Section titled “For all bars (bottom nav controller & topbar)”Modifier.windowInsetsPadding(WindowInsets.systemBars)// orModifier.systemBarsPadding()For only top bar
Section titled “For only top bar”Modifier.windowInsetsPadding(WindowInsets.statusBars)Rounded
Section titled “Rounded”For Object
Section titled “For Object”Modifier.clip(RoundedCornerShape(20))RoundedCornerShape() also accepts CircleShape
For Border
Section titled “For Border”Modifier.border(1.dp, Color.Red, RoundedCornerShape(20))Gradient
Section titled “Gradient”For Background
Section titled “For Background”Brush.linearGradient(listOf(Color.Red, Color.White))Text Color Gradient
Section titled “Text Color Gradient”
Text( text = "Hello Gradient", style = TextStyle( brush = Brush.linearGradient( colors = listOf(Color.Red, Color.Blue), ), fontSize = 50.sp ))Gradient Directions
Section titled “Gradient Directions”| Method | Direction |
|---|---|
linearGradient | topLeft → bottomRight |
horizontalGradient | Left → Right |
verticalGradient | Top → Bottom |
Shadow
Section titled “Shadow”Box( modifier = Modifier .padding(top = 50.dp) .size(200.dp) .shadow( elevation = 8.dp, // the height of the shadow shape = RoundedCornerShape(16.dp), // border radius of the box clip = false // whether not the ) .background( Color.White )) { Text("Hello Shadow", modifier = Modifier.align(Alignment.Center))}