鸿蒙OSUniApp导航栏组件开发:打造清新简约的用户界面#三方框架 #Uniapp

UniApp 开发实战:打造符合鸿蒙设计风格的日历活动安排组件

在移动应用开发中,日历和活动安排是非常常见的需求。本文将详细介绍如何使用 UniApp 框架开发一个优雅的日历活动安排组件,并融入鸿蒙系统的设计理念,实现一个既美观又实用的功能模块。

设计理念

在开发这个组件之前,我们需要深入理解鸿蒙系统的设计哲学。鸿蒙系统强调"自然、流畅、统一"的设计原则,这些特点将贯穿我们的整个组件实现过程:

  1. 自然:日历的切换和选择要符合用户的直觉
  2. 流畅:动画过渡要平滑,操作响应要及时
  3. 统一:视觉元素要保持一致性,符合系统设计规范
  4. 高效:快速获取和展示日程信息

组件实现

1. 日历选择器组件

首先实现一个基础的日历选择器组件:

<!-- components/harmony-calendar/harmony-calendar.vue -->
<template><view class="harmony-calendar"><view class="calendar-header"><view class="month-switcher"><text class="iconfont icon-left" @click="changeMonth(-1)"></text><text class="current-month">{{ currentYear }}年{{ currentMonth + 1 }}月</text><text class="iconfont icon-right" @click="changeMonth(1)"></text></view><view class="weekday-header"><text v-for="day in weekDays" :key="day">{{ day }}</text></view></view><view class="calendar-body" :class="{ 'with-animation': enableAnimation }"><view class="days-grid"><view v-for="(day, index) in daysArray" :key="index"class="day-cell":class="[{ 'current-month': day.currentMonth },{ 'selected': isSelected(day.date) },{ 'has-events': hasEvents(day.date) }]"@click="selectDate(day)"><text class="day-number">{{ day.dayNumber }}</text><view v-if="hasEvents(day.date)" class="event-indicator"></view></view></view></view></view>
</template><script>
export default {name: 'HarmonyCalendar',props: {events: {type: Array,default: () => []},selectedDate: {type: Date,default: () => new Date()}},data() {return {currentDate: new Date(),currentMonth: new Date().getMonth(),currentYear: new Date().getFullYear(),weekDays: ['日', '一', '二', '三', '四', '五', '六'],enableAnimation: true,daysArray: []}},watch: {currentMonth: {handler() {this.generateCalendar()},immediate: true}},methods: {generateCalendar() {const firstDay = new Date(this.currentYear, this.currentMonth, 1)const lastDay = new Date(this.currentYear, this.currentMonth + 1, 0)const daysInMonth = lastDay.getDate()const startingDay = firstDay.getDay()let days = []// 上个月的日期const prevMonth = new Date(this.currentYear, this.currentMonth - 1, 1)const daysInPrevMonth = new Date(this.currentYear, this.currentMonth, 0).getDate()for (let i = startingDay - 1; i >= 0; i--) {days.push({date: new Date(prevMonth.getFullYear(), prevMonth.getMonth(), daysInPrevMonth - i),dayNumber: daysInPrevMonth - i,currentMonth: false})}// 当前月的日期for (let i = 1; i <= daysInMonth; i++) {days.push({date: new Date(this.currentYear, this.currentMonth, i),dayNumber: i,currentMonth: true})}// 下个月的日期const remainingDays = 42 - days.length // 保持6行固定高度for (let i = 1; i <= remainingDays; i++) {days.push({date: new Date(this.currentYear, this.currentMonth + 1, i),dayNumber: i,currentMonth: false})}this.daysArray = days},changeMonth(delta) {const newDate = new Date(this.currentYear, this.currentMonth + delta, 1)this.currentMonth = newDate.getMonth()this.currentYear = newDate.getFullYear()this.enableAnimation = truesetTimeout(() => {this.enableAnimation = false}, 300)},isSelected(date) {return date.toDateString() === this.selectedDate.toDateString()},hasEvents(date) {return this.events.some(event => new Date(event.date).toDateString() === date.toDateString())},selectDate(day) {this.$emit('date-selected', day.date)}}
}
</script><style lang="scss">
.harmony-calendar {background-color: #ffffff;border-radius: 24rpx;padding: 32rpx;box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);.calendar-header {margin-bottom: 32rpx;.month-switcher {display: flex;justify-content: space-between;align-items: center;margin-bottom: 24rpx;.current-month {font-size: 32rpx;font-weight: 500;color: #333333;}.iconfont {font-size: 40rpx;color: #666666;padding: 16rpx;&:active {opacity: 0.7;}}}.weekday-header {display: grid;grid-template-columns: repeat(7, 1fr);text-align: center;text {font-size: 28rpx;color: #999999;padding: 16rpx 0;}}}.calendar-body {.days-grid {display: grid;grid-template-columns: repeat(7, 1fr);gap: 8rpx;.day-cell {aspect-ratio: 1;display: flex;flex-direction: column;align-items: center;justify-content: center;position: relative;border-radius: 16rpx;transition: all 0.2s ease-in-out;&.current-month {background-color: #f8f8f8;.day-number {color: #333333;}}&.selected {background-color: #2979ff;.day-number {color: #ffffff;}.event-indicator {background-color: #ffffff;}}.day-number {font-size: 28rpx;color: #999999;}.event-indicator {width: 8rpx;height: 8rpx;border-radius: 4rpx;background-color: #2979ff;position: absolute;bottom: 12rpx;}&:active {transform: scale(0.95);}}}}&.with-animation {.days-grid {animation: fadeInScale 0.3s ease-out;}}
}@keyframes fadeInScale {from {opacity: 0.5;transform: scale(0.95);}to {opacity: 1;transform: scale(1);}
}
</style>

2. 活动列表组件

接下来实现活动列表组件,用于展示选中日期的活动:

<!-- components/schedule-list/schedule-list.vue -->
<template><view class="schedule-list"><view class="list-header"><text class="title">活动安排</text><text class="add-btn" @click="$emit('add')">添加</text></view><view class="list-content"><template v-if="filteredEvents.length"><view v-for="event in filteredEvents" :key="event.id"class="schedule-item":class="{ 'completed': event.completed }"@click="toggleEvent(event)"><view class="event-time">{{ formatTime(event.time) }}</view><view class="event-info"><text class="event-title">{{ event.title }}</text><text class="event-location" v-if="event.location">{{ event.location }}</text></view><text class="iconfont icon-check"></text></view></template><view v-else class="empty-tip"><image src="/static/empty.png" mode="aspectFit"></image><text>暂无活动安排</text></view></view></view>
</template><script>
export default {name: 'ScheduleList',props: {selectedDate: {type: Date,required: true},events: {type: Array,default: () => []}},computed: {filteredEvents() {return this.events.filter(event => new Date(event.date).toDateString() === this.selectedDate.toDateString()).sort((a, b) => new Date(`${a.date} ${a.time}`) - new Date(`${b.date} ${b.time}`))}},methods: {formatTime(time) {return time.slice(0, 5) // 只显示小时和分钟},toggleEvent(event) {this.$emit('toggle', event)}}
}
</script><style lang="scss">
.schedule-list {margin-top: 32rpx;.list-header {display: flex;justify-content: space-between;align-items: center;margin-bottom: 24rpx;.title {font-size: 32rpx;font-weight: 500;color: #333333;}.add-btn {font-size: 28rpx;color: #2979ff;padding: 16rpx;&:active {opacity: 0.7;}}}.list-content {.schedule-item {display: flex;align-items: center;padding: 24rpx;background-color: #ffffff;border-radius: 16rpx;margin-bottom: 16rpx;box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);transition: all 0.2s ease-in-out;&.completed {opacity: 0.6;.event-info {text-decoration: line-through;}.icon-check {color: #52c41a;}}.event-time {font-size: 28rpx;color: #666666;margin-right: 24rpx;min-width: 100rpx;}.event-info {flex: 1;.event-title {font-size: 28rpx;color: #333333;margin-bottom: 8rpx;}.event-location {font-size: 24rpx;color: #999999;}}.icon-check {font-size: 40rpx;color: #dddddd;}&:active {transform: scale(0.98);}}.empty-tip {text-align: center;padding: 64rpx 0;image {width: 200rpx;height: 200rpx;margin-bottom: 16rpx;}text {font-size: 28rpx;color: #999999;}}}
}
</style>

3. 使用示例

下面展示如何在页面中使用这两个组件:

<!-- pages/schedule/schedule.vue -->
<template><view class="schedule-page"><harmony-calendar:events="events":selected-date="selectedDate"@date-selected="handleDateSelect"></harmony-calendar><schedule-list:events="events":selected-date="selectedDate"@toggle="toggleEventStatus"@add="showAddEventPopup"></schedule-list><!-- 添加活动弹窗 --><uni-popup ref="addEventPopup" type="bottom"><view class="add-event-form"><input v-model="newEvent.title" placeholder="活动标题" class="input-field" /><input v-model="newEvent.location" placeholder="活动地点" class="input-field" /><picker mode="time" :value="newEvent.time"@change="handleTimeChange"class="input-field"><view>{{ newEvent.time || '选择时间' }}</view></picker><button @click="saveEvent" class="save-btn":disabled="!newEvent.title || !newEvent.time">保存</button></view></uni-popup></view>
</template><script>
import HarmonyCalendar from '@/components/harmony-calendar/harmony-calendar'
import ScheduleList from '@/components/schedule-list/schedule-list'export default {components: {HarmonyCalendar,ScheduleList},data() {return {selectedDate: new Date(),events: [],newEvent: {title: '',location: '',time: '',completed: false}}},methods: {handleDateSelect(date) {this.selectedDate = date},showAddEventPopup() {this.$refs.addEventPopup.open()},handleTimeChange(e) {this.newEvent.time = e.detail.value},saveEvent() {const event = {...this.newEvent,id: Date.now(),date: this.selectedDate}this.events.push(event)this.$refs.addEventPopup.close()// 重置表单this.newEvent = {title: '',location: '',time: '',completed: false}// 提示保存成功uni.showToast({title: '添加成功',icon: 'success'})},toggleEventStatus(event) {const index = this.events.findIndex(e => e.id === event.id)if (index > -1) {this.events[index].completed = !this.events[index].completed}}}
}
</script><style lang="scss">
.schedule-page {padding: 32rpx;min-height: 100vh;background-color: #f5f5f5;
}.add-event-form {background-color: #ffffff;padding: 32rpx;border-radius: 24rpx 24rpx 0 0;.input-field {width: 100%;height: 88rpx;border-bottom: 2rpx solid #f0f0f0;margin-bottom: 24rpx;padding: 0 24rpx;font-size: 28rpx;}.save-btn {margin-top: 32rpx;background-color: #2979ff;color: #ffffff;border-radius: 44rpx;height: 88rpx;line-height: 88rpx;font-size: 32rpx;&:active {opacity: 0.9;}&[disabled] {background-color: #cccccc;}}
}
</style>

性能优化

  1. 虚拟列表优化

当活动数量较多时,可以使用虚拟列表优化性能:

// 在 schedule-list 组件中添加
import VirtualList from '@/components/virtual-list'export default {components: {VirtualList}// ...其他配置
}
  1. 状态管理

对于大型应用,建议使用 Vuex 管理日程数据:

// store/modules/schedule.js
export default {state: {events: []},mutations: {ADD_EVENT(state, event) {state.events.push(event)},TOGGLE_EVENT(state, eventId) {const event = state.events.find(e => e.id === eventId)if (event) {event.completed = !event.completed}}},actions: {async fetchEvents({ commit }) {// 从服务器获取数据const events = await api.getEvents()commit('SET_EVENTS', events)}}
}
  1. 缓存优化

使用本地存储缓存日程数据:

// utils/storage.js
export const saveEvents = (events) => {try {uni.setStorageSync('schedule_events', JSON.stringify(events))} catch (e) {console.error('保存失败:', e)}
}export const getEvents = () => {try {const events = uni.getStorageSync('schedule_events')return events ? JSON.parse(events) : []} catch (e) {console.error('读取失败:', e)return []}
}

适配建议

  1. 暗黑模式支持
// 在组件样式中添加
.harmony-calendar {&.dark {background-color: #1c1c1e;.calendar-header {.current-month {color: #ffffff;}}.day-cell {&.current-month {background-color: #2c2c2e;.day-number {color: #ffffff;}}}}
}
  1. 响应式布局

使用 flex 布局和 rpx 单位确保在不同设备上的适配:

.schedule-page {display: flex;flex-direction: column;min-height: 100vh;@media screen and (min-width: 768px) {flex-direction: row;.harmony-calendar {flex: 0 0 400rpx;}.schedule-list {flex: 1;margin-left: 32rpx;}}
}

总结

通过本文的讲解,我们实现了一个功能完整的日历活动安排组件。该组件不仅具有优雅的界面设计,还融入了鸿蒙系统的设计理念,同时考虑了性能优化和多端适配等实际开发中的重要因素。

在实际开发中,我们可以根据具体需求对组件进行扩展,例如:

  • 添加重复活动功能
  • 实现活动提醒
  • 支持多人协作
  • 添加数据同步功能

希望这篇文章能够帮助你更好地理解如何在 UniApp 中开发高质量的组件,同时也能为你的实际项目开发提供有价值的参考。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/diannao/85069.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在 HTML 文件中添加图片的常用方法

本文详解HTML图片插入方法&#xff1a;1&#xff09;通过<img>标签实现&#xff0c;必须含src和alt属性&#xff1b;2&#xff09;路径支持绝对/相对引用&#xff1b;3&#xff09;建议设置width/height保持比例&#xff1b;4&#xff09;响应式方案用srcset适配不同设备…

LangChain-自定义Tool和Agent结合DeepSeek应用实例

除了调用LangChain内置工具外&#xff0c;也可以自定义工具 实例1&#xff1a; 自定义多个工具 from langchain.agents import initialize_agent, AgentType from langchain_community.agent_toolkits.load_tools import load_tools from langchain_core.tools import tool, …

代码随想录算法训练营第60期第五十天打卡

大家好&#xff0c;首先感慨一下&#xff0c;时间过的真是快&#xff0c;不知不觉我们的训练营就已经到第五十天了&#xff0c;首先祝贺自己一直在坚持&#xff0c;今天是我们动态规划章节的收官之战&#xff0c;明天我们就会走进一个全新的算法章节单调栈&#xff0c;我们要为…

如何发布npm包?

如何发布npm包&#xff1f; 1. 注册账号[npm官网](https://www.npmjs.com/)2. 检查 npm 源是否在官方 npm 仓库&#xff0c;如果不在&#xff0c;进行切换3. 检查4. 打包配置5. 发布6. 使用错误&#xff1a;版本更新命令 1. 注册账号npm官网 2. 检查 npm 源是否在官方 npm 仓库…

AI工具使用的最佳实践,如何通过AI工具提高创作与工作效率

随着科技的迅猛发展&#xff0c;人工智能&#xff08;AI&#xff09;已从遥不可及的未来构想&#xff0c;转变为广泛应用于各行业的实用工具。AI不仅在内容创作、设计、写作等领域展现出巨大潜力&#xff0c;还通过自动化和智能化显著提升了工作效率。本文将深入探讨如何通过选…

漏洞Reconfigure the affected application to avoid use of weak cipher suites. 修复方案

修复方案&#xff1a;禁用弱加密套件&#xff08;Weak Cipher Suites&#xff09; 1. 确认当前使用的加密套件 在修复前&#xff0c;先检查应用程序或服务器当前支持的加密套件&#xff1a; OpenSSL (适用于HTTPS/TLS服务)openssl ciphers -v ALL:COMPLEMENTOFALL | sortNgi…

AI对软件工程的影响及未来发展路径分析报告

目录 第一部分&#xff1a;引言 研究背景与意义 报告框架与方法论 第二部分&#xff1a;AI对不同行业软件工程的影响分析 数字化行业 制造业 零售业 工业领域 第三部分&#xff1a;大厂AI软件工程实践案例分析 微软 谷歌 阿里巴巴 华为 第四部分&#xff1a;未来…

WSL里执行python深度学习的一些方法记录

安装anaconda3&#xff1a; 可以直接从 Download Now | Anaconda 中下载&#xff0c;然后拷贝到WSL环境的某个目录&#xff0c;执行 bash xxxxxxx.sh 即可安装。 启动jupyter notebook&#xff1a; 先conda activate 当前环境&#xff0c;然后pip install jupyter 此时&am…

使用 SpyGlass Power Verify 解决方案中的规则

本节提供了关于使用 SpyGlass Power Verify 解决方案 的相关信息。内容组织如下: SpyGlass Power Verify 简介运行 SpyGlass Power Verify 解决方案在 SpyGlass Power Verify 解决方案中评估结果SpyGlass Power Verify 解决方案中的参数SpyGlass Power Verify 报告1 SpyGlass …

spring4第3课-ioc控制反转-详解依赖注入的4种方式

1&#xff0c;属性注入&#xff1b; 2&#xff0c;构造函数注入&#xff1b;(通过类型&#xff1b;通过索引&#xff1b;联合使用) 3&#xff0c;工厂方法注入&#xff1b;(非静态工厂&#xff0c;静态工厂) 4&#xff0c;泛型依赖注入&#xff1b;(Spring4 整合 Hibernate4…

使用Rust和并发实现一个高性能的彩色分形图案渲染

分形与 Mandelbrot Mandelbrot 集 (Mandelbrot Set) 是复数平面上一个点的集合,以数学家 Benot Mandelbrot 的名字命名。它是最著名的分形之一。一个复数 c 是否属于 Mandelbrot 集,取决于一个简单的迭代过程: z n + 1 = z n 2 + c z_{n+1}=z_{n}^2+c zn+1​=zn2​+c 如果…

微信小程序的软件测试用例编写指南及示例--性能测试用例

以下是针对微信小程序的性能测试用例补充,结合代码逻辑和实际使用场景,从加载性能、渲染性能、资源占用、交互流畅度等维度设计测试点,并标注对应的优化方向: 一、加载性能测试用例 测试项测试工具/方法测试步骤预期结果优化方向冷启动加载耗时微信开发者工具「性能」面板…

行为型:观察者模式

目录 1、核心思想 2、实现方式 2.1 模式结构 2.2 实现案例 3、优缺点分析 4、适用场景 5、注意事项 1、核心思想 目的&#xff1a;针对被观察对象与观察者对象之间一对多的依赖关系建立起一种行为自动触发机制&#xff0c;当被观察对象状态发生变化时主动对外发起广播&…

t009-线上代驾管理系统

项目演示地址 摘 要 使用旧方法对线上代驾管理系统的信息进行系统化管理已经不再让人们信赖了&#xff0c;把现在的网络信息技术运用在线上代驾管理系统的管理上面可以解决许多信息管理上面的难题&#xff0c;比如处理数据时间很长&#xff0c;数据存在错误不能及时纠正等问题…

LVS-NAT 负载均衡群集

目录 简介 一、LVS 与群集技术基础 1.1 群集技术概述 1.2 负载均衡群集的分层结构 1.3 负载均衡工作模式 二、LVS 虚拟服务器核心组件与配置 2.1 LVS 内核模块与管理工具 2.2 负载调度算法解析 2.3 ipvsadm 管理工具实战 三、NFS 共享存储服务配置 3.1 NFS 服务基础…

LLaMaFactory - 支持的模型和模板 常用命令

一、 环境准备 激活LLaMaFactory环境&#xff0c;进入LLaMaFactory目录 cd LLaMA-Factoryconda activate llamafactory 下载模型 #模型下载 from modelscope import snapshot_download model_dir snapshot_download(Qwen/Qwen2.5-0.5B-Instruct) 二、启动一个 Qwen3-0.6B…

EDW2025|数据治理的神话破除——从误区到现实

在当今数据驱动的世界中&#xff0c;数据治理已成为企业成功的关键因素。然而&#xff0c;许多组织在实施数据治理时&#xff0c;常常被一些常见的误区所困扰。本文将逐一破除这些误区&#xff0c;揭示数据治理的真实面貌。 误区一&#xff1a;你需要一个大的预算&#xff01;…

AIGC与影视制作:技术革命、产业重构与未来图景

文章目录 一、AIGC技术全景&#xff1a;从算法突破到产业赋能1. **技术底座&#xff1a;多模态大模型的进化路径**2. **核心算法&#xff1a;从生成对抗网络到扩散模型的迭代** 二、AIGC在影视制作全流程中的深度应用1. **剧本创作&#xff1a;从“灵感枯竭”到“创意井喷”**2…

ReactJS 中的 JSX工作原理

文章目录 前言✅ 1. JSX 是什么&#xff1f;&#x1f527; 2. 编译后的样子&#xff08;核心机制&#xff09;&#x1f9f1; 3. React.createElement 做了什么&#xff1f;&#x1f9e0; 4. JSX 与组件的关系&#x1f504; 5. JSX 到真实 DOM 的过程&#x1f4d8; 6. JSX 与 Fr…

Spring Advisor增强规则实现原理介绍

Spring Advisor增强规则实现原理介绍 一、什么是 Advisor&#xff1f;1. Advisor 的定义与本质接口定义&#xff1a; 2. Advisor 的核心作用统一封装切点与通知构建拦截器链的基础实现增强逻辑的灵活组合 二. Sprin当中的实现逻辑1 Advisor 接口定义2 PointcutAdvisor 接口定义…