【发布时间】:2022-10-07 16:47:15
【问题描述】:
我目前的代码如下:
@Composable
fun AppTheme(darkTheme: Boolean = false,
content: @Composable() () -> Unit) {
val colors = if (darkTheme) DarkThemeColors else LightThemeColors
MaterialTheme(
colors = colors,
typography = MaterialTheme.typography,
shapes = MaterialTheme.shapes,
) {
content()
}
}
我在这样的主要活动中调用:
@Inject
lateinit var application: WeatherApplication
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme(darkTheme = application.isDark.value) {
WeatherApp(onToggleTheme = { application.toggleLightTheme()} )
}
切换灯光主题基本上是这样的:
isDark.value = !isDark.value
然后我在像这样的另一个屏幕中调用 onClick:
@Composable
fun SettingsScreen(onToggleTheme: () -> Unit)
IconButton(onClick = onToggleTheme)
这完成了黑暗和光明的主题,现在如果我想要更多的主题怎么办,也许像这样?
@Composable
fun AppTheme(currentTheme: String,
content: @Composable() () -> Unit) {
val colors = when (currentTheme) {
\"Pink\" -> PinkThemeColors
\"Light\" -> LightThemeColors
\"Red\" -> RedThemeColors
\"Rainbow\" -> RainbowTheme
else -> LightThemeColors
}
MaterialTheme(
colors = colors,
typography = MaterialTheme.typography,
shapes = MaterialTheme.shapes,
) {
content()
}
}
我如何在另一个屏幕的 onClick 按钮中调用它(一个按钮用于红色,另一个用于粉红色等)?
我仍然是初学者,很抱歉有任何困惑,任何帮助将不胜感激。
-
您可以将 Sealed 类用于 currentTheme 而不是字符串
标签: android kotlin themes android-jetpack-compose android-jetpack