Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gradient background in TopAppBar using Jetpack Compose

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:

rendered TopAppBar

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?

like image 645
Krystian Kaniowski Avatar asked Sep 06 '21 15:09

Krystian Kaniowski


People also ask

How do you add gradient background color in flutter?

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 .

How do I make a color gradient in Wordpress?

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.


1 Answers

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
)

like image 165
Philip Dukhov Avatar answered Sep 28 '22 00:09

Philip Dukhov