简体   繁体   English

ANDROID - Jetpack Compose:如何实现主题设置?

[英]ANDROID - Jetpack Compose: how to implement theme settings?

I'm having hard times trying to implement a preference who switch the application theme.我很难实现切换应用程序主题的偏好。

Normally I would be able to do it with XML but with Jetpack Compose I saw that the best practice is to use DataStore ( https://developer.android.com/topic/libraries/architecture/datastore )通常我可以使用 XML 来完成,但使用 Jetpack Compose 我发现最好的做法是使用DataStore ( https://developer.android.com/topic/libraries/architecture/datastore )

Someone please, can explain me from scratch how to store a preference and change the whole application theme when the user click on a button for example?有人请,例如,当用户单击按钮时,可以从头开始向我解释如何存储首选项并更改整个应用程序主题?

Thank you谢谢

I believe that the DataStore documentation already explains in great detail how it works...我相信DataStore 文档已经非常详细地解释了它是如何工作的......
Talking about Compose we have the isSystemInDarkTheme function which brings up the current theme of the user's android system (which can be seen in Theme.kt inside the ui package that Android Studio automatically creates in a Compose project).谈到 Compose,我们有isSystemInDarkTheme函数,它会调出用户 android 系统的当前主题(可以在 Android Studio 在 Compose 项目中自动创建的 ui 包内的Theme.kt中看到)。

What needs to be done is whenever you open the app, check if the user has chosen to change the theme manually in your app through a function that accesses the DataStore, in other words, provide a Flow<Boolean?> , with a logic like if null the user has not changed the theme on your app, so you follow with the android system and if have the boolean, pass and apply to the MaterialTheme of Compose.需要做的是,每当您打开应用程序时,检查用户是否选择通过访问 DataStore 的函数在您的应用程序中手动更改主题,换句话说,提供一个Flow<Boolean?> ,其逻辑如下如果为 null,则用户尚未更改应用程序的主题,因此您使用 android 系统,如果有布尔值,则传递并应用到 Compose 的MaterialTheme
And you also need to create a function that sends a boolean to the DataStore regarding the theme change on the app.您还需要创建一个函数,向 DataStore 发送有关应用程序主题更改的布尔值。

Here is a repository with example code allowing the user to change the theme between dark and light and saving the option in the DataStore.是一个带有示例代码的存储库,允许用户在深色和浅色之间更改主题并将选项保存在 DataStore 中。 And here is an article with theoretical and practical explanation about themes in Compose which I think may contribute to the clarification. 是一篇关于 Compose 主题的理论和实践解释的文章,我认为这可能有助于澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Material 3 Jetpack Compose 中实现 BottomSheet Android - How to implement BottomSheet in Material 3 Jetpack Compose Android 如何在 Jetpack Compose 中实现搜索 - Android - How to implement search in Jetpack Compose - Android 如何在 android jetpack compose ui 中实现水平滚动条? - How to implement horizontal scroller in android jetpack compose ui? Android Jetpack Compose (Composable) 如何正确实现 Swipe Refresh - Android Jetpack Compose (Composable) How to properly implement Swipe Refresh 如何使用 Android Jetpack Compose 实现 BottomAppBar 和 BottomDrawer 模式? - How to implement BottomAppBar and BottomDrawer pattern using Android Jetpack Compose? 如何在 Jetpack Compose 中引用主题属性? - How to reference theme attributes in Jetpack Compose? 如何使 Jetpack Compose 主题可定制? - How to make Jetpack Compose theme customizable? 如何在 Jetpack Compose 中实现平移 + 缩放动画? - How to implement a translate + scale animation in Jetpack Compose? 如何使用 Jetpack Compose 在应用程序中实现本地化 - How to implement in app localization with Jetpack Compose 如何在 Jetpack Compose 中实现 list multiSelect? - How to implement list multiSelect in Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM