https://kotlinlang.org logo
#compose
Title
# compose
v

Vivek Modi

07/17/2022, 11:46 AM
Another question can we override
MaterialTheme.colorScheme.background
to change specific color with according to theme type?
Copy code
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
    Greeting("Android")
}
If I change of
Surface(color = Color.Black)
in dark theme which is showing black background but if I change white theme it showing me black background, So is there way to override colorScheme or any other alternative recommended solution for this?
theme.kt
Copy code
package com.vivek.sportsresult.ui.theme

import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.google.accompanist.systemuicontroller.rememberSystemUiController

private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40
)

@Composable
fun SportsResultTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val systemUiController = rememberSystemUiController()
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }
        darkTheme -> {
            DarkColorScheme
        }
        else -> {
            LightColorScheme
        }
    }

    if (darkTheme) {
        systemUiController.setSystemBarsColor(
            color = Color.Black
        )
        MaterialTheme.colorScheme.background.copy(

        )
    } else {
        systemUiController.setSystemBarsColor(
            color = Color.White
        )
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}
v

Vivek Modi

07/17/2022, 1:37 PM
I cannot find the right stuff in this above link
j

Jessica Ernst

07/17/2022, 1:39 PM
that explains the whole theming in compose
and how it works
d

Dmitry Strekha

07/17/2022, 6:25 PM
You can use CompositionLocal to provide custom colors. something like this:
Copy code
val colors = if (isSystemInDarkTheme()) {
     DarkColorScheme.copy(....)
} else {
    LightColorScheme.copy(....)
}

CompositionLocalProvider(
    LocalColors provides colors
) {
    Surface { ... }
}
Just have a look
MaterialTheme
implementation, you'll get the idea but if you need to replace every
background
color, pass it as a param to your
*ColorScheme
fun
v

Vivek Modi

07/17/2022, 6:27 PM
Sure I'll try, thanks for the advice.
8 Views