Oh that's tricky, took me a while to figure out wh...
# compose-desktop
b
Oh that's tricky, took me a while to figure out why these 2 previews produced different background colors:
Copy code
@Preview
@Composable
private fun Preview() {
    styled {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colorScheme.surfaceContainerHigh
        ) {
            Text("Weird Background Swap")
        }
    }
}

@Preview
@Composable
private fun Preview1() {
    surface {
        Text("Weird Background Swap")
    }
}


@Composable
fun surface(color: Color = MaterialTheme.colorScheme.surfaceContainerHigh, content: @Composable () -> Unit) {
    styled {
        Surface(
            color = color,
            modifier = Modifier.fillMaxSize()
        ) {
            content()
        }
    }
}


@Composable
fun styled(content: @Composable () -> Unit) {
    MaterialTheme(
        colorScheme = darkScheme
    ) {
        content()
    }
}
p
In surface function there is color parameter which default value is taken from local material theme. But local material theme is set in styled function. In Preview you call styled first thus setting dark theme, but in Preview1 when calling surface, color is taken from default local, because styled was not called yet.