theme-change.uvue 2.6 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">
shutao-dc's avatar
shutao-dc 已提交
8 9
      <text>appTheme:</text>
      <text id="theme">{{ appTheme }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11
    </view>

shutao-dc's avatar
shutao-dc 已提交
12 13 14 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 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
    <view>
      <view class="uni-title uni-common-mt">
        <text class="uni-title-text"> 修改appTheme主题 </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>

  </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 {
        return uni.onOsThemeChange(function(res: OsThemeChangeResult) {
          this.osTheme = res.osTheme
        })
      },
      bindAppThemeChange(): number {
        return uni.onAppThemeChange(function(res: AppThemeChangeResult) {
          this.appTheme = res.appTheme
        })
      },
      radioChange(e : UniRadioGroupChangeEvent) {
        const theme = e.detail.value
        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.appTheme = res.appTheme!
          this.current = this.items.indexOf(this.appTheme)
        }
      })
      this.osThemeChangeId = bindOsThemeChange()
      this.appThemeChangeId = bindAppThemeChange()
    }
  }
</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 已提交
100
</style>