theme-change.uvue 3.2 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3 4 5
<template>
  <view class="uni-padding-wrap">
    <view class="uni-common-mt item-box">
      <text>osTheme:</text>
      <text id="theme">{{ osTheme }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    </view>
    <view class="uni-common-mt item-box">
      <text>应用当前主题:</text>
      <text id="theme">{{ appTheme }}</text>
    </view>

    <view>
      <view class="uni-title uni-common-mt">
        <text class="uni-title-text"> 修改appTheme主题(此处仅为演示API,本应用并未完整适配暗黑模式) </text>
      </view>
    </view>
    <view class="uni-list uni-common-pl">
      <radio-group @change="radioChange" class="radio-group">
        <radio class="uni-list-cell uni-list-cell-pd radio" v-for="(item, index) in items" :key="item"
          :class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item" :checked="index === current">
          {{ item }}
        </radio>
      </radio-group>
    </view>
shutao-dc's avatar
shutao-dc 已提交
25 26 27 28

  </view>
</template>

DCloud-WZF's avatar
DCloud-WZF 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<script>
  export default {
    data() {
      return {
        osThemeChangeId: 0,
        appThemeChangeId: 0,
        osTheme: "light" as string,
        appTheme: "light" as string,
        originalTheme: "light" as string,
        current: 0,
        items: [
          "light",
          "dark",
          "auto"
        ] as string[]
      }
    },
    methods: {
      bindOsThemeChange() : number {
        //注册osTheme变化监听
        return uni.onOsThemeChange((res : OsThemeChangeResult) => {
          this.osTheme = res.osTheme
        })
      },
      bindAppThemeChange() : number {
        //注册appTheme变化监听
        return uni.onAppThemeChange((res : AppThemeChangeResult) => {
          this.appTheme = res.appTheme
        })
      },
      radioChange(e : UniRadioGroupChangeEvent) {
        const theme = e.detail.value
        this.setAppTheme(theme)
        uni.showToast({
          icon: 'none',
          title: '当前选中:' + theme,
        })
      },
      setAppTheme(value : string) {
        uni.setAppTheme({
          theme: value,
          success: function () {
            console.log("设置appTheme为", value, "成功")
          },
          fail: function (e : IAppThemeFail) {
            console.log("设置appTheme为", value, "失败,原因:", e.errMsg)
          }
        })
      }
    },
    onReady() {
      uni.getSystemInfo({
        success: (res : GetSystemInfoResult) => {
          this.osTheme = res.osTheme!
          this.originalTheme = res.appTheme!
          this.appTheme = res.appTheme == "auto" ? res.osTheme! : res.appTheme!
          this.current = this.items.indexOf(res.appTheme!)
        }
      })
      this.osThemeChangeId = this.bindOsThemeChange()
      this.appThemeChangeId = this.bindAppThemeChange()
    },
    onUnload() {
      //注销监听
      uni.offAppThemeChange(this.appThemeChangeId)
      uni.offOsThemeChange(this.osThemeChangeId)
      uni.showToast({
        "position": "bottom",
        "title": "已停止监听主题切换"
      })
    }
shutao-dc's avatar
shutao-dc 已提交
100 101 102 103 104 105 106 107
  }
</script>

<style>
  .item-box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
DCloud-WZF's avatar
DCloud-WZF 已提交
108 109 110 111
  }

  .uni-list-cell {
    justify-content: flex-start;
shutao-dc's avatar
shutao-dc 已提交
112
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
113
</style>