theme-change.uvue 3.1 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
      </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,
38
        originalTheme: "light" as string,
shutao-dc's avatar
shutao-dc 已提交
39 40 41 42 43 44 45 46 47 48
        current: 0,
        items: [
          "light",
          "dark",
          "auto"
        ] as string[]
      }
    },
    methods: {
      bindOsThemeChange(): number {
49
        //注册osTheme变化监听
50
        return uni.onOsThemeChange((res: OsThemeChangeResult)=> {
shutao-dc's avatar
shutao-dc 已提交
51 52 53 54
          this.osTheme = res.osTheme
        })
      },
      bindAppThemeChange(): number {
55
        //注册appTheme变化监听
56
        return uni.onAppThemeChange((res: AppThemeChangeResult) => {
shutao-dc's avatar
shutao-dc 已提交
57 58 59 60 61
          this.appTheme = res.appTheme
        })
      },
      radioChange(e : UniRadioGroupChangeEvent) {
        const theme = e.detail.value
62
        this.setAppTheme(theme)
shutao-dc's avatar
shutao-dc 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        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!
84
          this.originalTheme = res.appTheme!
85 86
          this.appTheme = res.appTheme == "auto" ? res.osTheme! : res.appTheme!
          this.current = this.items.indexOf(res.appTheme!)
shutao-dc's avatar
shutao-dc 已提交
87 88
        }
      })
89 90
      this.osThemeChangeId = this.bindOsThemeChange()
      this.appThemeChangeId = this.bindAppThemeChange()
91 92 93 94 95
    },
    onUnload() {
      //注销监听
      uni.offAppThemeChange(this.appThemeChangeId)
      uni.offOsThemeChange(this.osThemeChangeId)
W
wanganxp 已提交
96 97 98 99
      uni.showToast({
        "position":"bottom",
        "title":"已停止监听主题切换"
      })
shutao-dc's avatar
shutao-dc 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
    }
  }
</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 已提交
113
</style>