Skip to content

Instantly share code, notes, and snippets.

@Raiden18
Last active June 2, 2022 10:12
Show Gist options
  • Save Raiden18/e1c1ed4dfb7c2a13ab9c16858f4dbaf1 to your computer and use it in GitHub Desktop.
Save Raiden18/e1c1ed4dfb7c2a13ab9c16858f4dbaf1 to your computer and use it in GitHub Desktop.
Button without polymophism
sealed class ButtonSize {
object Small : ButtonSize()
object Medium : ButtonSize()
object Large : ButtonSize()
object Huge : ButtonSize()
data class Custom(val heightDpInt: Int) : ButtonSize()
}
@Composable
fun RenderButton(
buttonSize: ButtonSize,
onButtonClick: () -> Unit,
text: String
) {
Button(
modifier = Modifier.height(buttonSize.getButtonHeight()).fillMaxWidth(),
onClick = onButtonClick,
content = { Text(text = text) }
)
}
private fun ButtonSize.getButtonHeight(): Dp {
return when (this) {
ButtonSize.Small -> 16.dp
ButtonSize.Medium -> 24.dp
ButtonSize.Large -> 32.dp
ButtonSize.Huge -> 40.dp
is ButtonSize.Custom -> this.heightDpInt.dp
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment