iOS 13+、Android 10+ 提供了暗黑模式/深色模式,之前的模式称为light,暗黑称为dark。
同时也要注意,低于上述版本的手机,系统层没有暗黑模式概念。
在uni-app x中,有3种主题概念:OSTheme、hostTheme、appTheme。每种主题在不同平台支持度不同,获取、设置和监听变化的方式也不同。
| 主题概念 | 描述 | App | Web | 小程序 | 获取方式 | 设置方式 | 监听变化 |
|---|---|---|---|---|---|---|---|
| osTheme | 手机OS的当前主题 | √ | x | x | uni.getDeviceInfo | - | uni.onOsThemeChange |
| hostTheme | 浏览器或小程序宿主的当前主题 | x | √ | √ | uni.getAppBaseInfo | - | uni.onHostThemeChange |
| appTheme | App当前主题 | √ | X | x | uni.getAppBaseInfo | uni.setAppTheme | uni.onAppThemeChange |
Web和小程序注意:
一般情况下,独立设置主题的场景常见于App平台,所以App平台新增了appTheme的概念。appTheme有几个用途:
开发者做主题适配时需要先明确需求,这3种做法,需要做的事情都不一样:
开发者做主题适配时需处理的内容范围,涉及manifest.json、theme.json、pages.json、app.uvue,以及自己的uvue页面。
web 端、小程序需要配置 manifest.json 中 web、mp-weixin 根节点的 "darkmode": true。配置后如果不生效请重新编译运行
{
"mp-weixin": {
"darkmode": true
},
"web": {
"darkmode": true
}
}
pages.json的亮黑设置,需要通过theme.json处理。
要特别注意,适配暗黑模式,如果要适配pages.json中的tabbar和Navigationbar,需放置theme.json文件。
下面是pages.json中的globalStyle设置,在属性值中,通过@来引用theme.json中定义的值:
"globalStyle": {
"navigationBarTextStyle": "@navigationBarTextStyle",
"navigationBarBackgroundColor": "@navigationBarBackgroundColor",
"backgroundColor": "@backgroundColor",
"backgroundTextStyle": "@backgroundTextStyle"
},
下面是theme.json的样例。theme.json的位置放在pages.json同级目录下。
在light和dark节点下,分别命名一批同名的变量,并分别赋值。这些变量可以在pages.json里直接引用。
{
"light": {
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#007AFF",
"backgroundColor": "#efeff4",
"tabBarPagebackgroundColorContent": "#efeff4",
"backgroundTextStyle": "dark"
},
"dark": {
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#1F1F1F",
"backgroundColor": "#1F1F1F",
"tabBarPagebackgroundColorContent": "#1F1F1F",
"backgroundTextStyle": "light"
}
}
完整的theme.json教程详见:theme.json
theme.json 里的变量仅能用于 pages.json。uvue页面不能引用。
在web和小程序中,theme.json的dark部分生效的前提是:
darkmode:true在App中,可以通过manifest.json的app.defaultAppTheme配置应用默认主题,可取值为light、dark、auto,默认值为light。配置为auto时,appTheme会跟随OS主题变化。
{
"app": {
"defaultAppTheme": "auto"
}
}
如果应用为用户提供主题切换功能,可以在运行时通过uni.setAppTheme设置light、dark或auto。
完成上述基础配置后,页面和组件的主题样式可以根据平台及版本选择以下一种方案。
以下平台可以使用@media (prefers-color-scheme: light)和@media (prefers-color-scheme: dark)设置主题样式:
在支持上述能力的平台,推荐优先使用媒体查询适配主题。
媒体查询会根据hostTheme或appTheme自动匹配。主题变化时,匹配的样式也会自动更新,无需监听主题变化、维护响应式变量或动态切换class。详见@media媒体查询。
页面只需要使用固定的class,通过媒体查询分别定义亮色和暗色样式:
<template>
<view class="page">
<text class="title">根据当前主题显示不同颜色的文字</text>
</view>
</template>
<style>
@media (prefers-color-scheme: light) {
.page {
--text-color: #333333;
}
}
@media (prefers-color-scheme: dark) {
.page {
--text-color: #ffffff;
}
}
.title {
color: var(--text-color);
}
</style>
媒体查询和page选择器,可以在app.uvue里使用,从而实现所有页面的效果批量控制。除非页面配置了禁止全局样式影响。
如果App平台需要为用户提供light、dark、auto选项,直接调用uni.setAppTheme即可。appTheme变化后,媒体查询会自动更新匹配的样式。
以下App平台场景,由于不支持媒体查询,只能通过API使用监听主题变化,然后动态切换class:
在已支持媒体查询的平台,如果业务逻辑还需要读取当前主题状态,可以另外使用主题API获取和监听,但样式仍可使用媒体查询,无需动态切换class。
为了在同一套代码中兼容上述App平台场景,以下示例在各端统一使用动态class。为避免每个页面都监听主题变化,可以在app.uvue中获取并监听主题,将结果存放在store/index.uts中,供各页面使用。
如果应用只需要跟随上家,不独立设置主题,App平台需要先将app.defaultAppTheme配置为auto,然后可以这样处理:
// app.uvue
import { state } from '@/store/index.uts'
onLaunch(() => {
// #ifdef WEB || MP-WEIXIN || MP-ALIPAY
state.isDark = (uni.getAppBaseInfo().hostTheme == 'dark')
uni.onHostThemeChange((result) => {
state.isDark = (result.hostTheme == 'dark')
})
// #endif
// #ifdef APP
state.isDark = (uni.getDeviceInfo().osTheme == 'dark')
uni.onOsThemeChange((result: OsThemeChangeResult) => {
state.isDark = (result.osTheme == 'dark')
})
// #endif
})
store/index.uts的内容如下:
type State = {
// 是否为暗黑主题
isDark: boolean
}
export const state = reactive({
isDark: false
} as State)
如果App平台允许用户独立设置主题,则需要获取和监听appTheme:
// app.uvue
import { state } from '@/store/index.uts'
onLaunch(() => {
// #ifdef WEB || MP-WEIXIN || MP-ALIPAY
state.isDark = (uni.getAppBaseInfo().hostTheme == 'dark')
uni.onHostThemeChange((result) => {
state.isDark = (result.hostTheme == 'dark')
})
// #endif
// #ifdef APP
const appTheme = uni.getAppBaseInfo().appTheme
state.isDark = appTheme == 'auto'
? uni.getDeviceInfo().osTheme == 'dark'
: appTheme == 'dark'
uni.onAppThemeChange((result: AppThemeChangeResult) => {
state.isDark = (result.appTheme == 'dark')
})
// #endif
})
可以在app.uvue的全局样式中定义亮色和暗色class。除非页面或组件的样式隔离策略禁止全局样式影响,否则页面可以直接使用这些class。
.theme-light {
--text-color: #333333;
}
.theme-dark {
--text-color: #ffffff;
}
页面根节点根据state.isDark动态切换class:
<template>
<view :class="state.isDark ? 'theme-dark' : 'theme-light'">
<text class="title">根据当前主题显示不同颜色的文字</text>
</view>
</template>
<script setup lang="ts">
import { state } from '@/store/index.uts'
</script>
<style>
.title {
color: var(--text-color);
}
</style>
动态切换class,由于要执行script,性能没有媒体查询高。另外动态切换class的代码,和theme.json的执行可能不在同一帧,会造成theme.json先生效、class后生效,有闪烁感。推荐升级到5.25的蒸汽模式,使用媒体查询方案适配主题。
uni-app x的App和Web平台框架中自带的界面,均已适配暗黑模式(小程序平台由小程序宿主自行适配)
uni-app x的内置组件,在App和Web平台均支持css设置所有样式,这样就可以在所有样式控制中使用css变量。但小程序平台的内置组件,依赖其自身实现,有的组件需要通过属性控制样式,此时无法使用css变量。
app.uvue、页面和组件样式中使用@media (prefers-color-scheme: light)和@media (prefers-color-scheme: dark)定义不同主题下的样式,pages/CSS/prefers-color-scheme页面提供了完整示例。设置应用主题
uni.setAppTheme用于设置App当前主题。开发者仍需为不同主题定义相应的页面和组件样式。它的作用是:
@media (prefers-color-scheme: light)和@media (prefers-color-scheme: dark)匹配的样式当然组件作者也可以不监听onAppThemeChange,而是暴露主题切换API给开发者,由开发者监听主题切换,再调用组件的主题切换API。
uni-app x的UI相关的API(比如showModal),也会响应setAppTheme。
| 名称 | 类型 | 必填 | 兼容性 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | SetAppThemeOptions | 是 | |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 兼容性 |
|---|---|---|---|
| theme | string | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | 错误码 - 702001 参数错误 - 2002000 未知错误 | ||||||||||
| |||||||||||||
| errSubject | string | 是 | 统一错误主题(模块)名称 | ||||||||||
| data | any | 否 | 错误信息中包含的数据 | ||||||||||
| cause | Error | 否 | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||
| errMsg | string | 是 | |||||||||||
uni.setAppTheme({
theme: "auto",
success: function() {
console.log("设置appTheme为 auto 成功")
},
fail: function(e: IAppThemeFail) {
console.log("设置appTheme为 auto 失败,原因:", e.errMsg)
}
})
开启监听应用主题变化
版本历史调整
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| callback | (res: AppThemeChangeResult) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| appTheme | string | 是 | 应用主题 | ||||||||||
| |||||||||||||
| 类型 |
|---|
| number |
//callbackId 用于注销监听
val callbackId = uni.onAppThemeChange((res: AppThemeChangeResult) => {
console.log("onAppThemeChange", res.appTheme)
})
取消监听应用主题变化
| Web | Android | iOS | HarmonyOS |
|---|---|---|---|
| x | 4.18 | 4.18 | 4.71 |
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| id | number | 是 |
val callbackId = uni.onAppThemeChange((res: AppThemeChangeResult) => {
console.log("onAppThemeChange", res.appTheme)
})
//...
//...
//注销监听
uni.offAppThemeChange(this.appThemeChangeId)
开启监听系统主题变化
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| callback | (res: OsThemeChangeResult) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| osTheme | string | 是 | 系统主题 | ||||||||||
| |||||||||||||
| 类型 |
|---|
| number |
//callbackId 用于注销监听
val callbackId = uni.onOsThemeChange((res: OsThemeChangeResult)=> {
console.log("onOsThemeChange---", res.osTheme)
})
注意:
dark,更低版本无法获取、监听OS的主题。取消监听系统主题变化
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| id | number | 是 |
val callbackId = uni.onOsThemeChange((res: OsThemeChangeResult)=> {
console.log("onOsThemeChange---", res.osTheme)
})
...
...
//注销监听
uni.offOsThemeChange(callbackId)
监听宿主题状态变化。
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| callback | (result: OnHostThemeChangeCallbackResult) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hostTheme | string | 是 | 主题名称 | ||||||||||
| |||||||||||||
| 类型 |
|---|
| number |
取消监听宿主题状态变化。
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| id | number | 是 |
监听系统主题状态变化。 已废弃,在web、小程序上推荐使用 onHostThemeChange
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| callback | (result: OnThemeChangeCallbackResult) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| theme | string | 是 | 主题名称 | ||||||||||
| |||||||||||||
取消监听系统主题状态变化。 已废弃,在web、小程序上推荐使用 offHostThemeChange
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| callback | (result: OnThemeChangeCallbackResult) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| theme | string | 是 | 主题名称 | ||||||||||
| |||||||||||||
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<template>
<view class="uni-padding-wrap">
<!-- #ifdef APP -->
<view class="uni-common-mt item-box">
<text>osTheme:</text>
<text id="theme">{{ data.osTheme }}</text>
</view>
<!-- #endif -->
<view class="uni-common-mt item-box">
<text>应用当前主题:</text>
<text id="theme">{{ data.appTheme }}</text>
</view>
<!-- #ifdef APP -->
<view>
<view class="uni-title uni-common-mt">
<text class="uni-title-text"> 修改appTheme主题(此处仅为演示API,本应用并未完整适配暗黑模式) </text>
</view>
</view>
<enum-data :items="data.items" title="appTheme" @change="radioChange"></enum-data>
<!-- #endif -->
<view class="mt-15">
<text class="fs-15 fw-bold color-text-333">input / textarea 默认颜色</text>
<text class="mt-10 color-text-333">切换主题观察默认文字和背景色,清空内容后观察 placeholder 字体颜色。</text>
<!-- 仅设置布局,保留组件默认的文字、背景和 placeholder 颜色。 -->
<input class="theme-input" v-model="preview.defaultInputValue" placeholder="input 默认 placeholder 颜色" />
<textarea class="theme-textarea" v-model="preview.defaultTextareaValue" placeholder="textarea 默认 placeholder 颜色" />
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">text 默认文字颜色</text>
<text class="theme-hint">切换主题观察默认文字颜色,暗黑模式下预期为白色。</text>
<!-- 不给待验证的 text 设置文字色,避免覆盖组件默认值。 -->
<view id="theme-default-view" class="theme-sample">
<text id="theme-default-text">text 默认文字颜色。</text>
</view>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">导航栏 / TabBar</text>
<text class="theme-hint">观察顶部原生导航栏,并通过下方入口查看原生 TabBar。两者使用应用当前配置,可与规范目标色对照。</text>
<text class="theme-hint">规范目标:浅色背景 #FFFFFF、前景黑色;深色背景 #1C1C1E、前景白色。TabBar 顶部分隔线:浅色 #CCCCCC、深色 #49494B。</text>
<navigator url="/pages/tabBar/component" open-type="switchTab" class="mt-10">
<button id="theme-open-tabbar">查看原生 TabBar</button>
</navigator>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">combox / uni-combobox 组合框</text>
<text class="theme-hint">观察输入区域、文字、箭头和候选浮层;暗黑表单背景预期为 #3B3B3B,placeholder 在两种主题下均为 #757575。</text>
<text class="theme-hint">uni-combox(兼容组件)</text>
<view id="theme-combox-placeholder" class="mt-10">
<uni-combox v-model="preview.comboxPlaceholder" :options="preview.options" placeholder="uni-combox 默认 placeholder" />
</view>
<text class="theme-hint">uni-combobox</text>
<view id="theme-combobox-placeholder" class="mt-10">
<uni-combobox v-model="preview.comboboxPlaceholder" :options="preview.options" placeholder="请选择,观察默认 placeholder" />
</view>
<view id="theme-combobox-value" class="mt-10">
<uni-combobox v-model="preview.comboboxValue" :options="preview.options" />
</view>
<view id="theme-combobox-disabled" class="mt-10">
<uni-combobox model-value="禁用状态" :options="preview.options" disabled />
</view>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">rich-text 默认颜色</text>
<text class="theme-hint">切换主题观察 rich-text 的默认文字颜色。</text>
<rich-text id="theme-rich-text-default" class="theme-sample" nodes="<p>rich-text 默认文字颜色。</p>" />
</view>
<!-- #ifndef MP-ALIPAY -->
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">editor 默认颜色</text>
<text class="theme-hint">输入文字和清空内容,观察默认文字、背景及 placeholder 颜色。默认 placeholder 预期为 #757575。</text>
<editor id="theme-editor-default" class="theme-editor theme-sample" placeholder="editor 默认 placeholder 颜色" />
</view>
<!-- #endif -->
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">picker 选择器</text>
<text class="theme-hint">分别打开普通、日期和时间选择器,检查弹出面板、选项、取消及确定按钮的主题颜色。</text>
<picker id="theme-picker-selector" class="mt-10" :range="preview.options" :value="preview.pickerIndex" @change="onPickerChange">
<view class="theme-sample"><text>普通选择:{{ preview.options[preview.pickerIndex] }}</text></view>
</picker>
<picker id="theme-picker-date" mode="date" :value="preview.date" @change="onDateChange">
<view class="theme-sample"><text>日期选择:{{ preview.date }}</text></view>
</picker>
<picker id="theme-picker-time" mode="time" :value="preview.time" @change="onTimeChange">
<view class="theme-sample"><text>时间选择:{{ preview.time }}</text></view>
</picker>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">picker-view / picker-view-column</text>
<text class="theme-hint">上下滚动,检查默认文字、选中区域、分隔线和上下遮罩颜色。</text>
<picker-view id="theme-picker-view" class="theme-picker-view mt-10" :value="preview.pickerViewValue" indicator-style="height: 40px" @change="onPickerViewChange">
<picker-view-column>
<view v-for="option in preview.options" :key="option" class="theme-picker-item">
<text>{{ option }}</text>
</view>
</picker-view-column>
</picker-view>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">checkbox</text>
<text class="theme-hint">点击切换选中状态,检查未选中、选中和禁用状态的边框、背景与勾选颜色。</text>
<checkbox-group>
<label class="theme-choice-row"><checkbox value="checked" :checked="true" /><text>默认选中</text></label>
<label class="theme-choice-row"><checkbox value="unchecked" /><text>默认未选中</text></label>
<label class="theme-choice-row"><checkbox value="disabled-checked" :checked="true" disabled /><text>禁用且选中</text></label>
<label class="theme-choice-row"><checkbox value="disabled" disabled /><text>禁用且未选中</text></label>
</checkbox-group>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">radio</text>
<text class="theme-hint">切换选项,检查未选中、选中和禁用状态的边框、背景与选中标记。</text>
<radio-group>
<label class="theme-choice-row"><radio value="checked" :checked="true" /><text>默认选中</text></label>
<label class="theme-choice-row"><radio value="unchecked" /><text>默认未选中</text></label>
</radio-group>
<radio-group>
<label class="theme-choice-row"><radio value="disabled-checked" :checked="true" disabled /><text>禁用且选中</text></label>
<label class="theme-choice-row"><radio value="disabled" disabled /><text>禁用且未选中</text></label>
</radio-group>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">button</text>
<text class="theme-hint">检查默认、primary、warn、镂空、禁用及加载状态的文字、背景和边框颜色。</text>
<button class="mt-10">默认按钮</button>
<button type="primary" class="mt-10">primary 按钮</button>
<button type="warn" class="mt-10">warn 按钮</button>
<button plain class="mt-10">plain 按钮</button>
<button disabled class="mt-10">默认禁用按钮</button>
<button type="primary" disabled class="mt-10">primary 禁用按钮</button>
<button type="warn" disabled class="mt-10">warn 禁用按钮</button>
<button loading class="mt-10">loading 按钮</button>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">switch</text>
<text class="theme-hint">点击开关,检查开、关及禁用状态的轨道和滑块颜色。</text>
<view class="theme-choice-row"><switch :checked="true" /><text>开启</text></view>
<view class="theme-choice-row"><switch /><text>关闭</text></view>
<view class="theme-choice-row"><switch :checked="true" disabled /><text>禁用且开启</text></view>
<view class="theme-choice-row"><switch disabled /><text>禁用且关闭</text></view>
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">slider</text>
<text class="theme-hint">拖动滑块,检查默认轨道、已选区域、滑块和数值文字;对照禁用状态。</text>
<slider id="theme-slider-default" class="mt-10" :value="40" show-value />
<slider id="theme-slider-disabled" class="mt-10" :value="60" show-value disabled />
</view>
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">scroll-view 滚动条与默认下拉刷新</text>
<text class="theme-hint">滚动此页和下方区域,观察页面及 scroll-view 的默认滚动条;从区域顶部下拉或点击按钮查看刷新指示器,约 1.5 秒后结束。</text>
<!-- 不设置 refresher-default-style / refresher-background,让组件自行适配主题。 -->
<scroll-view id="theme-scroll-view" class="theme-scroll-preview theme-sample" direction="vertical" :show-scrollbar="true"
:refresher-enabled="true" :refresher-triggered="preview.scrollRefreshing" @refresherrefresh="onScrollRefresh">
<view v-for="index in 12" :key="index" class="theme-scroll-item"><text>scroll-view 默认颜色 {{ index }}</text></view>
</scroll-view>
<button id="theme-refresh-scroll" class="mt-10" @click="refreshScrollView">显示 scroll-view 默认刷新指示器</button>
<text class="theme-hint">refresherrefresh 事件次数:{{ preview.scrollRefreshCount }}</text>
</view>
<!-- #ifndef MP-ALIPAY -->
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">list-view 默认下拉刷新</text>
<text class="theme-hint">从列表顶部下拉或点击按钮,检查默认刷新指示器是否随主题变化;约 1.5 秒后结束。</text>
<list-view id="theme-list-view" class="theme-scroll-preview theme-sample" :show-scrollbar="true"
:refresher-enabled="true" :refresher-triggered="preview.listRefreshing" @refresherrefresh="onListRefresh">
<list-item v-for="index in 12" :key="index" class="theme-scroll-item"><text>list-view 默认颜色 {{ index }}</text></list-item>
</list-view>
<button id="theme-refresh-list" class="mt-10" @click="refreshListView">显示 list-view 默认刷新指示器</button>
<text class="theme-hint">refresherrefresh 事件次数:{{ preview.listRefreshCount }}</text>
</view>
<!-- #endif -->
<view class="theme-section">
<text class="fs-15 fw-bold color-text-333">loading</text>
<text class="theme-hint">默认 loading 在暗黑模式下预期为白色;对照普通、bold 和 paused 状态。</text>
<view class="theme-choice-row"><loading /><text class="u-ml-10">默认 loading</text></view>
<view class="theme-choice-row"><loading bold /><text class="u-ml-10">bold loading</text></view>
<view class="theme-choice-row"><loading paused /><text class="u-ml-10">paused loading</text></view>
<!-- #ifdef APP-IOS -->
<view class="theme-choice-row"><loading :ios-spinner="true" /><text class="u-ml-10">iOS 雪花</text></view>
<view class="theme-choice-row"><loading :ios-spinner="false" /><text class="u-ml-10">iOS loading 圈</text></view>
<!-- #endif -->
</view>
</view>
</template>
<script setup lang="ts">
import { ItemType } from '@/components/enum-data/enum-data-types'
type Data = {
osThemeChangeId: number;
appThemeChangeId: number;
osTheme: string;
appTheme: string;
originalTheme: string;
current: number;
items: ItemType[];
}
const data = reactive({
osThemeChangeId: 0,
appThemeChangeId: 0,
osTheme: 'light',
appTheme: 'light',
originalTheme: 'light',
current: 0,
items: [
{ value: 0, name: 'light', checked: false },
{ value: 1, name: 'dark', checked: false },
{ value: 2, name: 'auto', checked: false }
] as ItemType[]
} as Data)
// 新增组件的演示状态独立于主题 API,保留现有 data 和自动化调用方式。
const preview = reactive({
defaultInputValue: 'input 默认文字颜色',
defaultTextareaValue: 'textarea 默认文字颜色',
options: ['选项一', '选项二', '选项三', '选项四', '选项五'],
comboxPlaceholder: '',
comboboxPlaceholder: '',
comboboxValue: '选项二',
pickerIndex: 0,
date: '2026-01-01',
time: '12:00',
pickerViewValue: [0] as number[],
scrollRefreshing: false,
listRefreshing: false,
scrollRefreshCount: 0,
listRefreshCount: 0
})
let scrollRefreshTimer = 0
let listRefreshTimer = 0
function onPickerChange(event : UniPickerChangeEvent) {
preview.pickerIndex = event.detail.value as number
}
function onDateChange(event : UniPickerChangeEvent) {
preview.date = event.detail.value as string
}
function onTimeChange(event : UniPickerChangeEvent) {
preview.time = event.detail.value as string
}
function onPickerViewChange(event : UniPickerViewChangeEvent) {
preview.pickerViewValue = event.detail.value
}
function refreshScrollView() {
if (preview.scrollRefreshing) return
preview.scrollRefreshing = true
scrollRefreshTimer = setTimeout(() => {
preview.scrollRefreshing = false
}, 1500)
}
function refreshListView() {
if (preview.listRefreshing) return
preview.listRefreshing = true
listRefreshTimer = setTimeout(() => {
preview.listRefreshing = false
}, 1500)
}
function onScrollRefresh() {
preview.scrollRefreshCount++
refreshScrollView()
}
function onListRefresh() {
preview.listRefreshCount++
refreshListView()
}
function bindOsThemeChange() : number {
return uni.onOsThemeChange((res : OsThemeChangeResult) => {
data.osTheme = res.osTheme
})
}
function bindAppThemeChange() : number {
// #ifdef APP
return uni.onAppThemeChange((res : AppThemeChangeResult) => {
data.appTheme = res.appTheme
})
// #endif
// #ifdef WEB || MP
return uni.onHostThemeChange((res : OnHostThemeChangeCallbackResult) => {
data.appTheme = res.hostTheme
})
// #endif
}
function setAppTheme(value : string) {
uni.setAppTheme({
theme: value as 'light' | 'dark' | 'auto',
success: function () {
console.log('设置appTheme为', value, '成功')
},
fail: function (e : IAppThemeFail) {
console.log('设置appTheme为', value, '失败,原因:', e.errMsg)
}
})
}
function radioChange(value : number) {
const theme = data.items[value].name
setAppTheme(theme)
}
onReady(() => {
uni.getSystemInfo({
success: (res : GetSystemInfoResult) => {
// #ifdef APP
data.osTheme = res.osTheme!
data.originalTheme = res.appTheme!
data.appTheme = res.appTheme == 'auto' ? res.osTheme! : res.appTheme!
data.current = data.items.findIndex((item : ItemType) : boolean => {
const currentItem = item.name == res.appTheme!
if (currentItem) {
item.checked = true
}
return currentItem
})
// #endif
// #ifdef WEB || MP
data.appTheme = res.hostTheme!
// #endif
}
})
// #ifdef APP
data.osThemeChangeId = bindOsThemeChange()
// #endif
data.appThemeChangeId = bindAppThemeChange()
})
onUnload(() => {
clearTimeout(scrollRefreshTimer)
clearTimeout(listRefreshTimer)
// #ifdef APP
uni.offAppThemeChange(data.appThemeChangeId)
uni.offOsThemeChange(data.osThemeChangeId)
// #endif
// #ifdef WEB || MP
uni.offHostThemeChange(data.appThemeChangeId)
// #endif
})
defineExpose({
data,
preview,
setAppTheme,
radioChange
})
</script>
<style>
.item-box {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.mt-15 { margin-top: 15px; }
.mt-10 { margin-top: 10px; }
.mb-15 { margin-bottom: 15px; }
.fs-15 { font-size: 15px; }
.fw-bold { font-weight: bold; }
.color-text-333 { color: var(--text-color, #333333); }
.u-ml-10 { margin-left: 10px; }
.theme-input {
height: 40px;
margin-top: 10px;
padding: 0px 10px;
}
.theme-textarea {
width: 100%;
height: 80px;
margin-top: 10px;
padding: 10px;
}
/* 组件样例仅设置布局,不覆盖组件的默认颜色。 */
.theme-section {
margin-top: 20px;
margin-bottom: 15px;
}
.theme-hint {
margin-top: 10px;
font-size: 13px;
line-height: 20px;
color: var(--text-color, #333333);
}
.theme-sample {
margin-top: 10px;
padding: 10px;
}
.theme-editor {
height: 120px;
min-height: 0px;
}
.theme-picker-view {
width: 100%;
height: 200px;
}
.theme-picker-item {
height: 40px;
align-items: center;
justify-content: center;
}
.theme-choice-row {
flex-direction: row;
align-items: center;
margin-top: 10px;
margin-bottom: 10px;
}
.theme-scroll-preview {
height: 180px;
}
.theme-scroll-item {
height: 48px;
padding: 10px;
}
</style>
| 名称 | 类型 | 必备 | 描述 |
|---|---|---|---|
| errMsg | string | 是 | 错误信息 |