Equilat
05/10/2021, 7:57 AMstruct DefaultButton: View {
var body: some View {
Button("Button"){}
.buttonStyle(ButtonDefaultStyle())
}
}
My style is in an external class.
struct ButtonDefaultStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.frame(width: 100, height: 40)
.foregroundColor(Color( colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
.background(configuration.isPressed ? Color( colorLiteral(red: 0.1254901961, green: 0.4666666667, blue: 0.4235294118, alpha: 1)) : Color( colorLiteral(red: 0.1411764706, green: 0.6784313725, blue: 0.5529411765, alpha: 1)))
.clipShape(RoundedRectangle(cornerRadius: 50))
.font(Font.custom("EuclidSquare-Medium", size: 16))
}
}
The best would be to be able to override a style by another style or by an attribute (Button().{}.style1(...).style2(...).color(...)...
Is there a way to do something similar in Compose ?
If not, did I misunderstood Compose philosophy or is it just something currently lacking ?Albert Chang
05/10/2021, 8:53 AM@Composable
fun MyButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: ButtonElevation? = ButtonDefaults.elevation(),
shape: Shape = RoundedCornerShape(percent = 50),
border: BorderStroke? = null,
colors: ButtonColors = ButtonDefaults.buttonColors(
backgroundColor = myColor1,
contentColor = myColor2
),
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
content: @Composable RowScope.() -> Unit
) {
Button(
onClick = onClick,
modifier = modifier.size(width = 100.dp, height = 40.dp),
enabled = enabled,
interactionSource = interactionSource,
elevation = elevation,
shape = shape,
border = border,
colors = colors,
contentPadding = contentPadding,
content = content
)
}
@Composable
fun MyApp() {
MyButton(
onClick = { /*TODO*/ },
modifier = Modifier.size(200.dp, 50.dp)
) {
Text(text = "MyButton")
}
}
Joost Klitsie
05/10/2021, 11:46 AMEquilat
05/10/2021, 11:47 AMAlbert Chang
05/10/2021, 1:22 PM