uview-ui项目暗黑模式(笔记)

本文介绍了一个基于Vue的项目如何实现深色与浅色主题的切换功能。通过在main.js中引入并使用setTheme.js文件提供的混合(mixin),可以在整个应用范围内设置默认的主题样式。setTheme.js文件中定义了获取主题的方法,并根据不同主题调整组件样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、main.js
import mixin from '@/common/js/setTheme.js'
Vue.use(mixin)
// import {getTheme} from '@/common/js/setTheme.js';
// Vue.prototype.$theme = getTheme;
2、App.vue
//监听系统主题变化
onThemeChange(e){
	this.getTheme()
},
3、setTheme.js
import {
	isEmpty
} from '@/common/js/util.js';
export default {
	install(Vue) {
		Vue.mixin({
			data() {
				return {
					theme: 'light',
					navTheme: {},
					themeStyle: {},
				}
			},
			onLoad() {
				this.getTheme();
			},
			methods: {
				getTheme() {
					let temp = this.$SysCache.get('theme');
					if (!isEmpty(temp)) {
						this.theme = temp;
					}
					this.navTheme = {
						background: '#2d6ec4',
						color: '#ffffff'
					}
					if (this.theme == 'dark') {
						this.themeStyle = {
							color: '#fff',
							fontsize: '30rpx',
							// background: '#242424'
						}
						//修改tabBar主体
						uni.setTabBarStyle({
							color: '#535353',
							backgroundColor: '#242424',
							borderStyle: 'white',
							selectedColor:'#ffffff'
						})
					} else {
						this.themeStyle = {
							color: '#333',
							fontsize: '30rpx'
						}
						uni.setTabBarStyle({
							backgroundColor: '#ffffff',
							borderStyle: 'black',
							color: '#7A7E83',
							selectedColor:'#2d6ec4'
						})
					}
				},
			}
		})
	}
}

4、util.js
//判断一个值是否为空
export const isEmpty = (v) => {
	switch (typeof v) {
		case 'undefined':
			return true;
		case 'string':
			if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
			break;
		case 'boolean':
			if (!v) return true;
			break;
		case 'number':
			if (0 === v || isNaN(v)) return true;
			break;
		case 'object':
			if (null === v || v.length === 0) return true;
			for (var i in v) {
				return false;
			}
			return true;
	}
	return false;
}
5、最外层项目中的uni.scss
/**
 * 这里是uni-app内置的常用样式变量
 *
 * uni-app 官方扩展插件及插件市场(https://ext.dcloud.net.cn)上很多三方插件均使用了这些样式变量
 * 如果你是插件开发者,建议你使用scss预处理,并在插件代码中直接使用这些变量(无需 import 这个文件),方便用户通过搭积木的方式开发整体风格一致的App
 *
 */

/**
 * 如果你是App开发者(插件使用者),你可以通过修改这些变量来定制自己的插件主题,实现自定义主题功能
 *
 * 如果你的项目同样使用了scss预处理,你也可以直接在你的 scss 代码中使用如下变量,同时无需 import 这个文件
 */
 @import 'uview-ui/theme.scss';
 @import '@/static/theme/theme.scss';

6、theme.scss

/**
 * dark  (深色模式)
 */

/* 按钮相关颜色 */
.dark-btn-special{
	background-color: #2d6ec4 !important;
	color: #ffff !important;
}
.dark-btn-primary{
	background-color: #ffffff !important;
	color:#191919 !important;
}
/* 文字基本颜色 */
.dark-text-primary{
	color: #ffffff !important;
}
.dark-text-dark{
	color: #333333 !important;
}
.dark-text-light{
	color: #ffffff !important;
}
/* 背景颜色 */
.dark-bg-primary{
	background-color:#131619!important;
}
.dark-sepcial-reverse{
	background-color: #191D23 !important;
}
.dark-bg-special{
	background-color:#191D23!important;
}
.dark-bg-general{
	background-color:#191D23!important;
}
/*反色背景色*/
.dark-bg-reverse{
	background-color:#131619!important;
}
.dark-bg-subSec{
	background-color:#191D23!important;
}
.dark-bg-tips{
	background-color:#191D23 !important;
}
/* 边框颜色 */
.dark-border{
	border-color: #c8c7cc !important;
}
/*盒阴影*/
.dark-box-shadow{
	box-shadow: 0px 0px 3px 0px #ccc !important;
}
/*首页通知公告背景色*/
.dark-bg-notice{
	background-color: #191D23  !important;
}
/*mask  遮挡层背景色*/
.dark-bg-mask{
	background-color: #3f3f3f  !important;
}
 


/**
 * light  (浅色色模式)
 */
/* 按钮相关颜色 */
.light-btn-special{
	background-color: #2d6ec4 !important;
	color: #ffff !important;
}
.light-btn-primary{
	background-color: #ffffff !important;
	color: #333333 !important;
}
/* 文字基本颜色 */
.light-text-primary{
	color: #333333 !important;
}
.light-text-dark{
	color: #ffffff !important;
}
.light-text-light{
	color: #666666 !important;
}
/* 页面整体背景颜色 */
.light-bg-primary{
	background-color: #f6f6f6 !important;
}
/* 布局背景颜色 */
.light-bg-special{
	background-color: #2d6ec4 !important;
}
.light-sepcial-reverse{
	background-color:#eeeeef !important;
}
//分段器
.light-bg-subSec{
	background-color:#0551B5!important;
}
/*反色背景色*/
.light-bg-reverse{
	background-color:#ffffff !important;
}
//分段器
.light-bg-tips{
	background-color:#FFFCEE!important;
}
/* 布局背景颜色 */
.light-bg-general{
	background-color: #ffffff !important;
}
/* 边框颜色 */
.light-border{
	border-color: none !important;
}
/*盒阴影*/
.light-box-shadow{
	box-shadow:none  !important;
}
/*首页通知公告背景色*/
.light-bg-notice{
	background-color: #F7FAFF !important;
} 
/*mask  遮挡层背景色*/
.light-bg-mask{
	background-color: rgba(0, 0, 0, 0.2) !important;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值