compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Update/Create Material3 example

Open ThraaxSession opened this issue 2 years ago • 5 comments

I together, it would be awesome if there are some Material3 examples. For e.g. how to create gradient color in the TopAppBar in the Scaffold. The internet isn't much helpful and I didn't find something here.

ThraaxSession avatar Mar 26 '23 10:03 ThraaxSession

In order to apply a gradient to a TopAppBar, you have to do some workaround, because a Color and a Brush (for Gradients) are not the same and therefore cannot be applied via the colors parameter of TopAppBar. So in order to apply a gradient to a TopAppBar you have to do something like below:

TopAppBar(
    modifier = Modifier.background(  // <- Apply the gradient you want with Modifier.background(Brush)
        brush = Brush.verticalGradient(
            colors = listOf(
                MaterialTheme.colorScheme.primary,
                MaterialTheme.colorScheme.secondary,
            )
        )
    ),
    title = { Text("Some title") },
    colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = Color.Transparent) // <- Remove container color to see background colors from Modifier
)

The way Material3 works is quite straight forward thanks to the declarative UI approach. The only thing you have to do is look up how the components are defined (see Material3 Components), then see what you can configure (see for that individual implementations and parameter documentation) and then you know what you can do and where you have to find your way around.

For example, the Brush.verticalGradient can be applied on the scaffold instead, allowing to set a different size of gradient with the available parameters (e.g. for expanding the gradient below the top app bar).

malliaridis avatar Mar 26 '23 18:03 malliaridis

Oh I've got it half way right. Just need to apply transparent. Thank you! Yes I find the Apis also innovative. 😎

ThraaxSession avatar Mar 27 '23 07:03 ThraaxSession

You might have to set to transparent other colors too, since the new toolbars have also a state now (flat and scrolled) which affects the background color and eventually the elevation. Haven't looked it up in detail yet, but shouldn't be hard to find out and update accordingly.

malliaridis avatar Mar 27 '23 07:03 malliaridis

It worked. But to be honest. It's not innovative to make something "transparent" to make other colors gradient applied. But that's a google issues for sure.

ThraaxSession avatar Mar 27 '23 15:03 ThraaxSession

It would be anyway awesome to update examples to material3 independetly from my issue! I would like to keep this issue open and addressed. @malliaridis Thank you for your fast help!

ThraaxSession avatar Mar 27 '23 15:03 ThraaxSession