theme-change.uvue 2.9 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
    </view>
7
    <view class="uni-common-mt item-box">
8
      <text>应用当前主题:</text>
shutao-dc's avatar
shutao-dc 已提交
9
      <text id="theme">{{ appTheme }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11
    </view>

shutao-dc's avatar
shutao-dc 已提交
12 13
    <view>
      <view class="uni-title uni-common-mt">
W
wanganxp 已提交
14
        <text class="uni-title-text"> 修改appTheme主题(此处仅为演示API,本应用并未完整适配暗黑模式) </text>
shutao-dc's avatar
shutao-dc 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
      </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>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        osThemeChangeId: 0,
        appThemeChangeId: 0,
        osTheme: "light" as string,
        appTheme: "light" as string,
        current: 0,
        items: [
          "light",
          "dark",
          "auto"
        ] as string[]
      }
    },
    methods: {
      bindOsThemeChange(): number {
48
        //注册osTheme变化监听
49
        return uni.onOsThemeChange((res: OsThemeChangeResult)=> {
shutao-dc's avatar
shutao-dc 已提交
50 51 52 53
          this.osTheme = res.osTheme
        })
      },
      bindAppThemeChange(): number {
54
        //注册appTheme变化监听
55
        return uni.onAppThemeChange((res: AppThemeChangeResult) => {
shutao-dc's avatar
shutao-dc 已提交
56 57 58 59 60
          this.appTheme = res.appTheme
        })
      },
      radioChange(e : UniRadioGroupChangeEvent) {
        const theme = e.detail.value
61
        this.setAppTheme(theme)
shutao-dc's avatar
shutao-dc 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        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!
83 84
          this.appTheme = res.appTheme == "auto" ? res.osTheme! : res.appTheme!
          this.current = this.items.indexOf(res.appTheme!)
shutao-dc's avatar
shutao-dc 已提交
85 86
        }
      })
87 88
      this.osThemeChangeId = this.bindOsThemeChange()
      this.appThemeChangeId = this.bindAppThemeChange()
89 90 91 92 93
    },
    onUnload() {
      //注销监听
      uni.offAppThemeChange(this.appThemeChangeId)
      uni.offOsThemeChange(this.osThemeChangeId)
shutao-dc's avatar
shutao-dc 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
    }
  }
</script>

<style>
  .item-box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .uni-list-cell {
    justify-content: flex-start;
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
107
</style>