Bradleycorn
03/04/2021, 9:39 PMCompositionLocal
and when/how to use it. Does this seem like a valid use case:
Using a Material theme, the designer provided direction for Outlined Buttons ...
When a button is displayed on the surface or background color, it should use the Theme's Secondary
color.
When a button is displayed on another color, it should use that color's "on" Color.
To achieve this, I wrote an AppSurface
Composable that wraps a regular surface, and sets the value of a LocalButtonColor
composition local, and then wrote a AppOutlinedButton
Composable that wraps a regular OutlinedButton, and sets it's color using the LocalButtonColor
...
Does this seem like the right approach? (Code is posted in the thread)Bradleycorn
03/04/2021, 9:39 PMval LocalButtonColor = compositionLocalOf { Color.Black }
private val buttonShape = RoundedCornerShape(50)
@Composable
fun AppSurface(
modifier: Modifier = Modifier,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colors.surface,
contentColor: Color = contentColorFor(color),
border: BorderStroke? = null,
elevation: Dp = 0.dp,
content: @Composable () -> Unit
) {
val buttonColor = when (color) {
MaterialTheme.colors.surface -> MaterialTheme.colors.secondary
MaterialTheme.colors.background -> MaterialTheme.colors.background
else -> contentColor
}
CompositionLocalProvider(
LocalButtonColor provides buttonColor
) {
Surface(modifier, shape, color, contentColor, border, elevation, content)
}
}
@Composable
fun AppOutlinedButton(
text: String,
onClick: ()->Unit = {},
color: Color = LocalButtonColor.current
) {
OutlinedButton(
shape = buttonShape,
border = BorderStroke(2.dp, color),
colors = ButtonDefaults.outlinedButtonColors(backgroundColor = Color.Transparent, contentColor = color),
onClick = onClick) {
Text(text = text)
}
}
Bradleycorn
03/04/2021, 9:41 PMSean McQuillan [G]
03/05/2021, 5:04 AMBradleycorn
03/05/2021, 1:45 PM