Skip to content

Modifier

fun Modifier.conditional(condition: Boolean, modifier: Modifier.() -> Modifier): Modifier {
return if (condition) then(modifier(Modifier)) else this
}

Retrieve widget layout (width, height)

Modifier.onGloballyPositioned {
textWidth = it.size.width // Int
}

Use .verticalScroll() or .horizontalScroll()

val state = rememberScrollState()
Column(Modifier.verticalScroll(state)){
}

After binding the state, use .animateScrollTo() to scroll to a location with animation

requiredSizesize
Force to set the size no matter whatRespect parent layout size, if overflow then fit in the parent layout
Box(modifier = Modifier.size(100.dp))
Box(modifier = Modifier.requiredSize(100.dp))

Same as z-index in CSS, allows you to change the layer of composables

Modifier.zIndex(1f)
Modifier.zIndex(2f) // <- higher layer

weight() takes the remaining space of the composables

Modifier.weight(1f)

Weight Example

@Composable
fun 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))
}
}
}
Modifier.border(2.dp, Color.Green, CircleShape)

For all bars (bottom nav controller & topbar)

Section titled “For all bars (bottom nav controller & topbar)”
Modifier.windowInsetsPadding(WindowInsets.systemBars)
// or
Modifier.systemBarsPadding()
Modifier.windowInsetsPadding(WindowInsets.statusBars)
Modifier.clip(RoundedCornerShape(20))

RoundedCornerShape() also accepts CircleShape

Modifier.border(1.dp, Color.Red, RoundedCornerShape(20))
Brush.linearGradient(listOf(Color.Red, Color.White))

Text Gradient

Text(
text = "Hello Gradient",
style = TextStyle(
brush = Brush.linearGradient(
colors = listOf(Color.Red, Color.Blue),
),
fontSize = 50.sp
)
)
MethodDirection
linearGradienttopLeft → bottomRight
horizontalGradientLeft → Right
verticalGradientTop → Bottom
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))
}