Skip to content

Instantly share code, notes, and snippets.

@MrNtlu
Created December 7, 2022 13:20
Show Gist options
  • Save MrNtlu/dcd6cfbdfa93e6ad9d03d8b030829521 to your computer and use it in GitHub Desktop.
Save MrNtlu/dcd6cfbdfa93e6ad9d03d8b030829521 to your computer and use it in GitHub Desktop.
Jetpack Compose Dark Theme Main Activity Part 2
@Composable
fun DarkThemeScreen(
dataStoreUtil: DataStoreUtil,
themeViewModel: ThemeViewModel,
) {
var switchState by remember {themeViewModel.isDarkThemeEnabled }
val coroutineScope = rememberCoroutineScope()
Scaffold {
Switch(
checked = switchState,
onCheckedChange = {
switchState = it
coroutineScope.launch {
dataStoreUtil.saveTheme(it)
}
},
thumbContent = {
Icon(
modifier = Modifier
.size(SwitchDefaults.IconSize),
imageVector = if (switchState) Icons.Rounded.DarkMode else Icons.Rounded.LightMode,
contentDescription = "Switch Icon"
)
},
colors = SwitchDefaults.colors(
checkedTrackColor = MaterialTheme.colorScheme.primary,
checkedThumbColor = MaterialTheme.colorScheme.onPrimary,
),
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment