提交 5fbfda0d 编写于 作者: shutao-dc's avatar shutao-dc

补充theme-change示例

上级 dcaa9890
...@@ -1309,6 +1309,13 @@ ...@@ -1309,6 +1309,13 @@
"navigationBarTitleText": "日历" "navigationBarTitleText": "日历"
} }
}, },
{
"path": "pages/API/theme-change/theme-change",
"style": {
"navigationBarTitleText": "主题切换",
"enablePullDownRefresh": false
}
},
// #endif // #endif
{ {
"path": "pages/template/schema/schema", "path": "pages/template/schema/schema",
...@@ -1380,13 +1387,6 @@ ...@@ -1380,13 +1387,6 @@
"enablePullDownRefresh": false "enablePullDownRefresh": false
} }
}, },
// {
// "path": "pages/API/theme-change/theme-change",
// "style": {
// "navigationBarTitleText": "主题切换",
// "enablePullDownRefresh": false
// }
// },
{ {
"path": "uni_modules/uni-pay-x/pages/success/success", "path": "uni_modules/uni-pay-x/pages/success/success",
"style": { "style": {
......
// uni-app自动化测试教程: uni-app自动化测试教程: https://uniapp.dcloud.net.cn/worktile/auto/hbuilderx-extension/
describe('API-theme-change', () => {
let page;
const isApp = process.env.UNI_OS_NAME === "android" || process.env.UNI_OS_NAME === "ios";
if (!isApp) {
it('dummyTest', () => {
expect(1).toBe(1)
})
return
}
beforeAll(async () => {
page = await program.reLaunch('/pages/API/theme-change/theme-change')
await page.waitFor(600);
});
it("check-set-app-theme", async () => {
await page.callMethod('setAppTheme', "dark")
await page.waitFor(300)
expect(await page.data('appTheme')).toBe("dark")
})
});
<template> <template>
<view class="uni-padding-wrap"> <view class="uni-padding-wrap">
<view class="uni-common-mt item-box"> <view class="uni-common-mt item-box">
<text>theme:</text> <text>osTheme:</text>
<text id="theme">{{ dataInfo.theme }}</text> <text id="theme">{{ osTheme }}</text>
</view>
<view class="uni-common-mt item-box">
<text>fn1 trigger num:</text>
<text id="fn1-trigger-num">{{ dataInfo.fn1TriggerNum }}</text>
</view> </view>
<view class="uni-common-mt item-box"> <view class="uni-common-mt item-box">
<text>fn2 trigger num:</text> <text>appTheme:</text>
<text id="fn2-trigger-num">{{ dataInfo.fn2TriggerNum }}</text> <text id="theme">{{ appTheme }}</text>
</view> </view>
<button class="uni-common-mt" id="bind-fn1" @click="bindFn1">
bind fn1
</button>
<button class="uni-common-mt" id="remove-fn1" @click="removeFn1">
remove fn1
</button>
<button class="uni-common-mt" id="bind-fn2" @click="bindFn2">
bind fn2
</button>
<button class="uni-common-mt" id="remove-fn2" @click="removeFn2">
remove fn2
</button>
</view>
</template>
<script setup lang="uts">
type DataInfo = {
fn1TriggerNum: number
fn2TriggerNum: number
theme: string
}
const dataInfo = reactive<DataInfo>({
fn1TriggerNum: 0,
fn2TriggerNum: 0,
theme: ''
})
const fn1 = (options : OnThemeChangeOptions) => { <view>
dataInfo.fn1TriggerNum++ <view class="uni-title uni-common-mt">
console.log('fn1 triggered options: ', options) <text class="uni-title-text"> 修改appTheme主题 </text>
dataInfo.theme = options.theme </view>
} </view>
<view class="uni-list uni-common-pl">
const fn2 = (options : OnThemeChangeOptions) => { <radio-group @change="radioChange" class="radio-group">
dataInfo.fn2TriggerNum++ <radio class="uni-list-cell uni-list-cell-pd radio" v-for="(item, index) in items" :key="item"
console.log('fn2 triggered options: ', options) :class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item"
dataInfo.theme = options.theme :checked="index === current">
} {{ item }}
</radio>
const bindFn1 = () => { </radio-group>
uni.onOSThemeChange(fn1) </view>
}
const removeFn1 = () => { </view>
uni.offOSThemeChange(fn1) </template>
}
const bindFn2 = () => { <script>
uni.onOSThemeChange(fn2) export default {
} data() {
const removeFn2 = () => { return {
uni.offOSThemeChange(fn2) osThemeChangeId: 0,
} appThemeChangeId: 0,
osTheme: "light" as string,
onReady(() => { appTheme: "light" as string,
bindFn1() current: 0,
bindFn2() items: [
}) "light",
</script> "dark",
"auto"
<style> ] as string[]
.item-box { }
display: flex; },
flex-direction: row; methods: {
justify-content: space-between; 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;
}
</style> </style>
...@@ -500,6 +500,16 @@ ...@@ -500,6 +500,16 @@
url: 'facial-recognition-verify', url: 'facial-recognition-verify',
} }
] as Page[], ] as Page[],
},
{
id: 'theme-change',
name: '主题切换',
pages: [
{
name: '主题切换',
url: 'theme-change',
}
] as Page[]
}, },
// #endif // #endif
{ {
...@@ -546,16 +556,6 @@ ...@@ -546,16 +556,6 @@
} }
] as Page[] ] as Page[]
}, },
// {
// id: 'theme-change',
// name: '主题切换',
// pages: [
// {
// name: '主题切换',
// url: 'theme-change',
// }
// ] as Page[]
// },
// #endif // #endif
/* /*
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册