I want to set the gradient background of my TopAppBar
:
My code:
TopAppBar(
modifier = Modifier.background(
Brush.horizontalGradient(
colors = listOf(
Color.Red,
Color.Green
)
)
),
title = { Text("UI Components") },
backgroundColor = Color.Transparent
)
Result:
I found this post: Jetpack Compose Button with gradient background? for button - so I set backgroundColor transparent and custom background via a modifier. Sadly in my case, there is an additional shadow around text which I don't know how to remove. What should I change or maybe TopAppBar is just not designed to be used using gradient and I should write something completely custom?
To use gradients, you first need a Container widget, and within that you need to access its decoration property. Start by building the decoration of the Container widget in your _MyHomePageState build method in main. dart .
Target an element for which you want to create a gradient background and head to CSS Properties – Background. Once you click on the Gradient button within the Background menu, you can choose the type of the gradient – linear or radial.
This shadow is caused by default elevation
. Set it to zero:
TopAppBar(
modifier = Modifier.background(
Brush.horizontalGradient(
colors = listOf(
Color.Red,
Color.Green
)
)
),
title = { Text("UI Components") },
backgroundColor = Color.Transparent,
elevation = 0.dp
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With