theme-change.uvue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view class="uni-padding-wrap">
  3. <!-- #ifdef APP -->
  4. <view class="uni-common-mt item-box">
  5. <text>osTheme:</text>
  6. <text id="theme">{{ osTheme }}</text>
  7. </view>
  8. <!-- #endif -->
  9. <view class="uni-common-mt item-box">
  10. <text>应用当前主题:</text>
  11. <text id="theme">{{ appTheme }}</text>
  12. </view>
  13. <!-- #ifdef APP -->
  14. <view>
  15. <view class="uni-title uni-common-mt">
  16. <text class="uni-title-text"> 修改appTheme主题(此处仅为演示API,本应用并未完整适配暗黑模式) </text>
  17. </view>
  18. </view>
  19. <view class="uni-list uni-common-pl">
  20. <radio-group @change="radioChange" class="radio-group">
  21. <radio class="uni-list-cell uni-list-cell-pd radio" v-for="(item, index) in items" :key="item"
  22. :class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item" :checked="index === current">
  23. {{ item }}
  24. </radio>
  25. </radio-group>
  26. </view>
  27. <!-- #endif -->
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. osThemeChangeId: 0,
  35. appThemeChangeId: 0,
  36. osTheme: "light" as string,
  37. appTheme: "light" as string,
  38. originalTheme: "light" as string,
  39. current: 0,
  40. items: [
  41. "light",
  42. "dark",
  43. "auto"
  44. ] as string[]
  45. }
  46. },
  47. methods: {
  48. bindOsThemeChange() : number {
  49. //注册osTheme变化监听
  50. return uni.onOsThemeChange((res : OsThemeChangeResult) => {
  51. this.osTheme = res.osTheme
  52. })
  53. },
  54. bindAppThemeChange() : number {
  55. // #ifdef APP
  56. //注册appTheme变化监听
  57. return uni.onAppThemeChange((res : AppThemeChangeResult) => {
  58. this.appTheme = res.appTheme
  59. })
  60. // #endif
  61. // #ifdef WEB || MP
  62. return uni.onHostThemeChange((res : OnHostThemeChangeCallbackResult) => {
  63. this.appTheme = res.hostTheme
  64. })
  65. // #endif
  66. },
  67. radioChange(e : UniRadioGroupChangeEvent) {
  68. const theme = e.detail.value
  69. this.setAppTheme(theme)
  70. uni.showToast({
  71. icon: 'none',
  72. title: '当前选中:' + theme,
  73. })
  74. },
  75. setAppTheme(value : string) {
  76. uni.setAppTheme({
  77. theme: value as 'light' | 'dark' | 'auto',
  78. success: function () {
  79. console.log("设置appTheme为", value, "成功")
  80. },
  81. fail: function (e : IAppThemeFail) {
  82. console.log("设置appTheme为", value, "失败,原因:", e.errMsg)
  83. }
  84. })
  85. }
  86. },
  87. onReady() {
  88. uni.getSystemInfo({
  89. success: (res : GetSystemInfoResult) => {
  90. // #ifdef APP
  91. this.osTheme = res.osTheme!
  92. this.originalTheme = res.appTheme!
  93. this.appTheme = res.appTheme == "auto" ? res.osTheme! : res.appTheme!
  94. this.current = this.items.indexOf(res.appTheme!)
  95. // #endif
  96. // #ifdef WEB || MP
  97. this.appTheme = res.hostTheme
  98. // #endif
  99. }
  100. })
  101. // #ifdef APP
  102. this.osThemeChangeId = this.bindOsThemeChange()
  103. // #endif
  104. this.appThemeChangeId = this.bindAppThemeChange()
  105. },
  106. onUnload() {
  107. //注销监听
  108. // #ifdef APP
  109. uni.offAppThemeChange(this.appThemeChangeId)
  110. uni.offOsThemeChange(this.osThemeChangeId)
  111. // #endif
  112. // #ifdef WEB || MP
  113. uni.offHostThemeChange(this.appThemeChangeId)
  114. // #endif
  115. //暂时屏蔽 避免5.1安卓设备自动化测试不过
  116. // uni.showToast({
  117. // "position": "bottom",
  118. // "title": "已停止监听主题切换"
  119. // })
  120. }
  121. }
  122. </script>
  123. <style>
  124. .item-box {
  125. display: flex;
  126. flex-direction: row;
  127. justify-content: space-between;
  128. }
  129. .uni-list-cell {
  130. justify-content: flex-start;
  131. }
  132. </style>