2

I'm using the Accompanist systemUiController library and I'm setting darkIcons=false

while in Light Theme but the effect doesn't apply on devices with android 11. It seems to work on devices with android 10 for example.

This is the code with which I'm trying to set the colors.

 val systemUiController = rememberSystemUiController()
 systemUiController.setStatusBarColor(color=statusBarColor,darkIcons=false) 

where statusBarColor is a darker color which represents the reason I want white foreground/icons

While in Dark Theme it works to set darkIcons to both true and false and the effect applies accordingly

This is the status bar on LightTheme with darkIcons=false and darkIcons=true

This is the status bar on DarkTheme with darkIcons=false enter image description here

This is the status bar on DarkTheme with darkIcons=true enter image description here

For reference this is my whole Theme.kt

private val LightBase = ASBTheme(
    material = lightColors(
        background = FigmaPrimaryWhite,
        onBackground = FigmaTextBlack,
        surface = FigmaPrimaryWhite,
        onSurface = FigmaTextBlack,
        primary = FigmaSecondaryAvastBlue,
        error = FigmaStatusPink,
    ),
    textColorLabel = FigmaSecondaryAvastPurple,
    colorAccent = FigmaPrimaryGreen,
    ... //bunch of custom colors
)

private val DarkBase = ASBTheme(
    material = darkColors(
        background = FigmaPrimaryBlackBg,
        onBackground = FigmaTextWhite,
        surface = FigmaSecondaryAvastBlueDark,
        onSurface = FigmaTextWhite,
        primary = FigmaSecondaryBlackDark,
        error = FigmaStatusPink
    ),
        textColorLabel = FigmaSecondaryAvastPurpleBright,
        colorAccent = FigmaStatusGreen,
        ... //bunch of custom colors
)

private val LocalAsbTheme = staticCompositionLocalOf { LightBase }
var navBarColor: Color? = null
var statusBarColor: Color? = null

@Composable
fun ASBTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkBase
    } else {
        LightBase
    }
    if (darkTheme) {
        navBarColor = DarkBase.background
        statusBarColor = DarkBase.primary
    } else {
        navBarColor = LightBase.background
        statusBarColor = LightBase.primary
    }

    SetBarsTheme(statusBarColor!!, navBarColor!!)

    CompositionLocalProvider(
        LocalAsbTheme provides colors,
    ) {
        MaterialTheme(
            colors = colors.material,
            content = content,
        )
    }
}

val MaterialTheme.asbTheme: ASBTheme
    @Composable
    @ReadOnlyComposable
    get() = LocalAsbTheme.current

SetBarsTheme() is where I'm trying to set the status bar colors depending on the lifecycle event so that the colors would maintain after onPause() / onStop(). I also tried to set the colors outside of this logic and the bug still persists.

@Composable
fun SetBarsTheme(
    statusBarColor: Color,
    navigationBarColor: Color,
    darkIcons:Boolean=false,
) {
    val lifecycleOwner = LocalLifecycleOwner.current
    val systemUiController = rememberSystemUiController()

    DisposableEffect(lifecycleOwner) {
        // Create an observer that triggers our remembered callbacks
        // for sending analytics events
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_RESUME) {
               systemUiController.setStatusBarColor(color=statusBarColor,darkIcons)
                systemUiController.setNavigationBarColor(color=navigationBarColor)
            }
        }

        // Add the observer to the lifecycle
        lifecycleOwner.lifecycle.addObserver(observer)

        // When the effect leaves the Composition, remove the observer
        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }
}
4

0 回答 0