是否可以从外部 API 更新 jetpack 中的颜色?
Is it possible to update Colors in jetpack from external API?
是否可以在应用程序启动时从外部 API 更新 Jetpack 中的 Colors.kt。更准确地说,我想在应用程序启动时从外部 API 获取“primary”、“primaryVariant”、“secondary” 的颜色值。
在 Jetpack compose 中实现动态主题非常容易。
您所需要的只是存储在变量中并传递给主题的颜色状态。
示例代码,
Activity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
App()
}
}
}
@Composable
fun App(
viewModel: MainActivityViewModel = hiltViewModel(),
) {
val dynamicColors by viewModel.colors.collectAsState()
WhosebugAnswersTheme(
dynamicColors = dynamicColors,
) {
Column(
modifier = Modifier.fillMaxSize(),
) {
Button(
onClick = {
viewModel.fetchColors()
},
) {
Text("Fetch Colors")
}
Text(
text = "This is current Primary Color",
color = MaterialTheme.colors.primary,
)
Text(
text = "This is current Secondary Color",
color = MaterialTheme.colors.secondary,
)
Button(
onClick = {
viewModel.resetTheme()
},
) {
Text("Reset Theme")
}
}
}
}
视图模型
class MainActivityViewModel : ViewModel() {
private var _colors: MutableStateFlow<Colors?> = MutableStateFlow(
value = null
)
val colors: StateFlow<Colors?> = _colors
fun fetchColors() {
viewModelScope.launch(Dispatchers.IO) {
delay(1000L)
_colors.value = Colors(
primary = Color.Green,
primaryVariant = Color.Black,
secondary = Color.DarkGray,
secondaryVariant = Color.LightGray,
background = Color.White,
surface = Color.White,
error = Color.Red,
onPrimary = Color.Blue,
onSecondary = Color.Cyan,
onBackground = Color.LightGray,
onSurface = Color.White,
onError = Color.Red,
isLight = true,
)
}
}
fun resetTheme() {
_colors.value = null
}
}
fetchColors()
方法用于模拟 API 调用并在那里进行实际的 API 调用。
是否可以在应用程序启动时从外部 API 更新 Jetpack 中的 Colors.kt。更准确地说,我想在应用程序启动时从外部 API 获取“primary”、“primaryVariant”、“secondary” 的颜色值。
在 Jetpack compose 中实现动态主题非常容易。
您所需要的只是存储在变量中并传递给主题的颜色状态。
示例代码,
Activity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
App()
}
}
}
@Composable
fun App(
viewModel: MainActivityViewModel = hiltViewModel(),
) {
val dynamicColors by viewModel.colors.collectAsState()
WhosebugAnswersTheme(
dynamicColors = dynamicColors,
) {
Column(
modifier = Modifier.fillMaxSize(),
) {
Button(
onClick = {
viewModel.fetchColors()
},
) {
Text("Fetch Colors")
}
Text(
text = "This is current Primary Color",
color = MaterialTheme.colors.primary,
)
Text(
text = "This is current Secondary Color",
color = MaterialTheme.colors.secondary,
)
Button(
onClick = {
viewModel.resetTheme()
},
) {
Text("Reset Theme")
}
}
}
}
视图模型
class MainActivityViewModel : ViewModel() {
private var _colors: MutableStateFlow<Colors?> = MutableStateFlow(
value = null
)
val colors: StateFlow<Colors?> = _colors
fun fetchColors() {
viewModelScope.launch(Dispatchers.IO) {
delay(1000L)
_colors.value = Colors(
primary = Color.Green,
primaryVariant = Color.Black,
secondary = Color.DarkGray,
secondaryVariant = Color.LightGray,
background = Color.White,
surface = Color.White,
error = Color.Red,
onPrimary = Color.Blue,
onSecondary = Color.Cyan,
onBackground = Color.LightGray,
onSurface = Color.White,
onError = Color.Red,
isLight = true,
)
}
}
fun resetTheme() {
_colors.value = null
}
}
fetchColors()
方法用于模拟 API 调用并在那里进行实际的 API 调用。