Alex Styl
06/06/2025, 12:35 PMAlex Styl
06/06/2025, 12:36 PMfun Modifier.outline(width: Dp, color: Color, shape: Shape): Modifier {
return border(width, color, shape).padding(width)
}
And I use it like this:
Modifier
.padding(16.dp)
.outline(2.dp, ComposeTheme.colors[outline].copy(0.5f), ComposeTheme.shapes[medium])
.background(ComposeTheme.colors[dialog], shape = ComposeTheme.shapes[medium])
Alex Styl
06/06/2025, 12:37 PMAlex Styl
06/06/2025, 12:58 PMRoundedCornerShape(12.dp)
Alex Styl
06/06/2025, 1:05 PM@Composable
fun Modifier.outline(thickness: Dp, color: Color, shape: Shape): Modifier {
return this.then(
Modifier
.background(color, shape = shape)
.padding(thickness)
.clip(shape)
)
}
Olivier Patry
06/06/2025, 4:11 PMAlex Styl
06/06/2025, 4:43 PMOlivier Patry
06/06/2025, 5:46 PMAlex Styl
06/06/2025, 9:07 PMAlex Styl
06/06/2025, 9:08 PM@Composable
fun Modifier.outline(width: Dp, color: Color, shape: Shape): Modifier {
val outer = shape as RoundedCornerShape
val density = LocalDensity.current
val innerShape = object : CornerBasedShape(
topStart = outer.topStart,
topEnd = outer.topEnd,
bottomStart = outer.bottomStart,
bottomEnd = outer.bottomEnd
) {
override fun createOutline(
size: Size,
topStart: Float,
topEnd: Float,
bottomEnd: Float,
bottomStart: Float,
layoutDirection: LayoutDirection,
): Outline {
return if (topStart + topEnd + bottomEnd + bottomStart == 0.0f) {
Outline.Rectangle(size.toRect())
} else {
val thickness = with(density) { width.toPx() }
Outline.Rounded(
RoundRect(
rect = size.toRect(),
topLeft = CornerRadius((if (layoutDirection == Ltr) topStart else topEnd) - thickness),
topRight = CornerRadius((if (layoutDirection == Ltr) topEnd else topStart) - thickness),
bottomRight =
CornerRadius((if (layoutDirection == Ltr) bottomEnd else bottomStart) - thickness),
bottomLeft =
CornerRadius((if (layoutDirection == Ltr) bottomStart else bottomEnd) - thickness)
)
)
}
}
override fun copy(
topStart: CornerSize,
topEnd: CornerSize,
bottomEnd: CornerSize,
bottomStart: CornerSize,
): CornerBasedShape {
return shape.copy(topStart, topEnd, bottomEnd, bottomStart)
}
}
return background(color = color, shape = shape)
.padding(width)
.clip(innerShape)
}
Alex Styl
06/07/2025, 9:32 AMoutline()
in the framework:
You know the drill:
https://issuetracker.google.com/issues/423073237romainguy
06/07/2025, 9:37 AMAlex Styl
06/07/2025, 9:44 AMromainguy
06/07/2025, 9:46 AMAlex Styl
06/07/2025, 9:47 AMShubham Singh
06/07/2025, 11:06 AMI thought we added a border style to choose inside/outside/centered but maybe I'm misrememberingThis would be amazing to have 👏
Sanlorng
06/07/2025, 12:30 PM