ButtonComponent with preview parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class ButtonConfig( | |
val buttonContent: @Composable RowScope.(Modifier) -> Unit, | |
val background: Color, | |
val contentColor: Color, | |
) | |
fun interface ButtonData { | |
@Composable | |
fun value(): ButtonConfig | |
} | |
private val primaryWithIcon = ButtonData { | |
ButtonConfig( | |
buttonContent = { | |
ButtonContentWithIcon(text = "Primary button with icon") | |
}, | |
background = MaterialTheme.colorScheme.primary, | |
contentColor = MaterialTheme.colorScheme.onPrimary | |
) | |
} | |
private val secondaryWithIcon = ButtonData { | |
ButtonConfig( | |
buttonContent = { | |
ButtonContentWithIcon(text = "Secondary button with icon") | |
}, | |
background = MaterialTheme.colorScheme.secondary, | |
contentColor = MaterialTheme.colorScheme.onSecondary | |
) | |
} | |
private val primaryWithoutIcon = ButtonData { | |
ButtonConfig( | |
buttonContent = { | |
ButtonContentWithoutIcon(text = "Primary button without icon") | |
}, | |
background = MaterialTheme.colorScheme.primary, | |
contentColor = MaterialTheme.colorScheme.onPrimary | |
) | |
} | |
private val secondaryWithoutIcon = ButtonData { | |
ButtonConfig( | |
buttonContent = { | |
ButtonContentWithoutIcon(text = "Secondary button without icon") | |
}, | |
background = MaterialTheme.colorScheme.secondary, | |
contentColor = MaterialTheme.colorScheme.onSecondary | |
) | |
} | |
class ButtonConfigProvider : PreviewParameterProvider<ButtonData> { | |
override val values: Sequence<ButtonData> = sequenceOf( | |
primaryWithIcon, secondaryWithIcon, primaryWithoutIcon, secondaryWithoutIcon | |
) | |
} | |
@Composable | |
fun RowScope.ButtonContentWithIcon(text: String) { | |
Icon( | |
imageVector = Icons.Outlined.Star, | |
contentDescription = "", | |
modifier = Modifier.padding(end = 8.dp) | |
) | |
Text(text = text) | |
} | |
@Composable | |
fun RowScope.ButtonContentWithoutIcon(text: String) { | |
Text(text = text) | |
} | |
@Composable | |
fun ButtonComponent( | |
background: Color, | |
contentColor: Color, | |
onClick: () -> Unit, | |
modifier: Modifier = Modifier, | |
content: @Composable (RowScope, Modifier) -> Unit, | |
) { | |
val buttonColors = ButtonDefaults.buttonColors( | |
containerColor = background, | |
contentColor = contentColor, | |
) | |
Button(colors = buttonColors, onClick = onClick, modifier = modifier) { | |
content(this, Modifier) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment