Skip to content

Instantly share code, notes, and snippets.

@Raiden18
Last active March 23, 2023 15:51
Show Gist options
  • Save Raiden18/dc8395bf036832ec0989ed9e629d940c to your computer and use it in GitHub Desktop.
Save Raiden18/dc8395bf036832ec0989ed9e629d940c to your computer and use it in GitHub Desktop.
Button.kt More complex example with polymorphism
sealed class ButtonSize(
open val heightDpInt: Int,
open val color: Color,
open val textResId: Int
) {
object Small : ButtonSize(16, Color.Red, R.string.small_button_text)
object Medium : ButtonSize(24, Color.Gray, R.string.medium_button_text)
object Large : ButtonSize(32, Color.Green, R.string.large_button_text)
object Huge : ButtonSize(40, Color.Blue, R.string.huge_button_text)
data class Custom(
override val heightDpInt: Int,
override val color: Color,
override val textResId: Int
) : ButtonSize(heightDpInt, color, textResId)
fun getHeightDp(): Dp {
return heightDpInt.dp
}
}
@Composable
fun RenderButton(
buttonSize: ButtonSize,
onButtonClick: () -> Unit
) {
Button(
modifier = Modifier
.height(buttonSize.getHeightDp())
.fillMaxWidth(),
onClick = onButtonClick,
colors = ButtonDefaults.buttonColors(
backgroundColor = buttonSize.color,
),
content = {
Text(text = stringResource(buttonSize.textResId))
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment