Created
December 7, 2022 13:20
-
-
Save MrNtlu/dcd6cfbdfa93e6ad9d03d8b030829521 to your computer and use it in GitHub Desktop.
Jetpack Compose Dark Theme Main Activity Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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