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

Jaime

08/10/2022, 10:18 PM
Hello, I have a problem with
isSystemInDarkTheme()
I am adding it to the theme but sometimes it returns true after false
Copy code
2022-08-10 17:12:22.215 6489-6489/ E/isSystemInDarkTheme: true darkMode 
2022-08-10 17:12:23.338 6489-6489/ E/isSystemInDarkTheme: true darkMode 
2022-08-10 17:12:24.478 6489-6489/ E/isSystemInDarkTheme: false darkMode 
2022-08-10 17:12:30.008 6489-6489/ E/isSystemInDarkTheme: false darkMode 
2022-08-10 17:12:30.697 6489-6489/ E/isSystemInDarkTheme: false darkMode
Copy code
@Composable
fun Theme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    ProvideHapiColors(colors) {
        MaterialTheme(
            colors = if (darkTheme) darkColors() else lightColors(),
            typography = Typography,
            shapes = Shapes,
            content = content
        )
    }
}
c

Chris Sinco [G]

08/10/2022, 10:23 PM
Do you notice the theme changing as well?
j

Jaime

08/10/2022, 10:24 PM
yes sir
sometimes dark is shown and other times light
use compose 1.2.0-beta02
c

Chris Sinco [G]

08/10/2022, 10:26 PM
Do you have a snippet of where you are using this Theme?
j

Jaime

08/10/2022, 10:27 PM
yes, one moment
Copy code
setContent {
    Theme {
        Surface(
            color = HapiTheme.colors.uiBackground,
            modifier = Modifier.fillMaxSize()
        ) {
            App(
                activity = this@MainActivity,
                application = application
            )
        }
    }
}
c

Chris Sinco [G]

08/10/2022, 10:51 PM
And do you have the code for HapiTheme?
And are you seeing the theme changes on Preview or on device?
j

Jaime

08/10/2022, 10:58 PM
Copy code
@Composable
fun HapiTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    ProvideHapiColors(colors) {
        MaterialTheme(
            colors = if (darkTheme) darkColors() else lightColors(),
            typography = Typography,
            shapes = Shapes,
            content = content
        )
    }
}
if it is a device
113 Views