Dekroo
06/30/2025, 8:08 PM@Composable
fun AppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
dynamicColor: Boolean = true,
content: @Composable() () -> Unit
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
darkTheme -> darkScheme
else -> lightScheme
}
MaterialTheme(
colorScheme = colorScheme,
typography = AppTypography,
content = content
)
}
---------------
Text(
text = qrCode,
modifier = modifier,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurface
)
Dekroo
06/30/2025, 8:28 PMBogdan Vladoiu Lbs
06/30/2025, 8:31 PMclass MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set the default night mode based on system settings or your preference
// This ensures isSystemInDarkTheme() in Compose is correctly initialized
AppCompatDelegate.setDefaultNightMode(
if (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.MODE_NIGHT_YES
} else {
AppCompatDelegate.MODE_NIGHT_NO
}
)
setContent {
AppTheme(
darkTheme = isSystemInDarkTheme(), // This now reflects the correctly set mode
dynamicColor = true,
content = {
Text(
text = "Your QR Code Text Here", // Example text
// ... your existing modifiers
color = MaterialTheme.colorScheme.onSurface
)
}
)
}
}
}
Dekroo
06/30/2025, 8:40 PMBogdan Vladoiu Lbs
06/30/2025, 8:46 PMAppCompatDelegate.setDefaultNightMode()
doesn't resolve the initial text color issue, and it's specifically MaterialTheme.colorScheme.onSurface
on a Text
composable, the problem might be a very specific timing or a subtle interaction with how your AppTheme
defines lightScheme
and darkScheme
.
Since you've confirmed it's only the text color, and it fixes on minimizing/reopening, this points to the colorScheme
being evaluated incorrectly at the very first composition, then correcting itself when the UI is rebuilt.
Ensure isSystemInDarkTheme()
is stable: While AppCompatDelegate.setDefaultNightMode()
helps, sometimes the system's Configuration
isn't fully propagated to LocalConfiguration.current
(which isSystemInDarkTheme()
uses) for the very first frame.
A robust way to get the current system dark mode, ensuring it's stable, is to use LocalConfiguration.current.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
.
Modify your AppTheme
like this:
@Composable
fun AppTheme(
// Pass it explicitly from Activity if not already doing so
// or ensure it's derived from a stable source
darkTheme: Boolean = (LocalConfiguration.current.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES,
dynamicColor: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
darkTheme -> darkScheme
else -> lightScheme
}
MaterialTheme(
colorScheme = colorScheme,
typography = AppTypography,
content = content
)
}
Bogdan Vladoiu Lbs
06/30/2025, 8:47 PMDekroo
07/01/2025, 9:23 AMBogdan Vladoiu Lbs
07/01/2025, 9:24 AMBogdan Vladoiu Lbs
07/01/2025, 9:26 AM