月份切换与年份导航:prevMonth / nextMonth 逻辑

前言

在日历应用中,月份切换是最基本的交互。"海风日记"的日历页通过左右箭头按钮实现月份切换,同时支持"今天"按钮快速回到当月。

本文将从源码出发,深入讲解月份切换的逻辑实现和边界处理。


一、月份切换实现

1.1 状态变量

typescript 复制代码
@Component
export struct CalendarTab {
  @State curYear: number = new Date().getFullYear()
  @State curMonth: number = new Date().getMonth()  // 0-indexed
  private todayDate = new Date().getDate()
  private todayMonth = new Date().getMonth()
  private todayYear = new Date().getFullYear()
}

1.2 上个月

typescript 复制代码
private prevMonth() {
  if (this.curMonth === 0) {
    this.curYear -= 1      // 跨年
    this.curMonth = 11     // 十二月
  } else {
    this.curMonth -= 1
  }
}

1.3 下个月

typescript 复制代码
private nextMonth() {
  if (this.curMonth === 11) {
    this.curYear += 1      // 跨年
    this.curMonth = 0      // 一月
  } else {
    this.curMonth += 1
  }
}

二、导航按钮

typescript 复制代码
Row({ space: 12 }) {
  // 左箭头
  Button() {
    SymbolGlyph($r('sys.symbol.chevron_left')).fontSize(16).fontColor([COLOR_TEXT_MAIN])
  }
  .backgroundColor('transparent').width(36).height(36)
  .onClick(() => { this.prevMonth() })

  // 月份标题
  Column({ space: 2 }) {
    Text(this.getMonthName(this.curMonth))
      .fontSize(22).fontColor(COLOR_TEXT_MAIN).fontWeight(FontWeight.Bold)
    Text(`${this.curYear}年`).fontSize(12).fontColor(COLOR_TEXT_SECONDARY)
  }
  .alignItems(HorizontalAlign.Center)

  // 右箭头
  Button() {
    SymbolGlyph($r('sys.symbol.chevron_right')).fontSize(16).fontColor([COLOR_TEXT_MAIN])
  }
  .backgroundColor('transparent').width(36).height(36)
  .onClick(() => { this.nextMonth() })
}

三、"今天"按钮

typescript 复制代码
Button('今天')
  .height(30).fontSize(13).fontColor('#FFFFFF').fontWeight(FontWeight.Bold)
  .backgroundColor(COLOR_PRIMARY).borderRadius(15)
  .padding({ left: 12, right: 12 })
  .onClick(() => {
    this.curYear = this.todayYear
    this.curMonth = this.todayMonth
  })

四、年视图的年份导航

typescript 复制代码
Row({ space: 4 }) {
  Button() {
    SymbolGlyph($r('sys.symbol.chevron_left')).fontSize(16).fontColor([COLOR_AMBER])
  }
  .backgroundColor('transparent').width(36).height(36)
  .onClick(() => { this.curYear -= 1 })

  Button() {
    SymbolGlyph($r('sys.symbol.chevron_right')).fontSize(16).fontColor([COLOR_AMBER])
  }
  .backgroundColor('transparent').width(36).height(36)
  .onClick(() => { this.curYear += 1 })
}

五、常见问题

5.1 月份切换后 UI 不更新

问题:点击切换按钮后,月份数字没有变化。

原因 :@State 变量更新了但 UI 未重新渲染。

解决方案 :确保 curYear 和 curMonth 是 @State 变量。


总结

本文通过"海风日记"的日历页,深入讲解了月份切换的实现:

  1. prevMonth/nextMonth:月份切换逻辑
  2. 跨年处理:边界情况处理
  3. "今天"按钮:快速回到当月
  4. 年视图导航:年份切换

下一篇文章将深入讲解 月统计面板,敬请期待。

如果这篇文章对你有帮助,欢迎点赞👍、收藏⭐、关注🔔,你的支持是我持续创作的动力!


相关资源:

相关推荐
小码哥_常9 小时前
一套自己部署的 IM,客户端是同一份 Flutter
前端·后端
学长毕设12 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
努力的小雨14 小时前
给 AI 眼镜开发应用有多难?我从零做了一个陪练,还上架了
后端
excel16 小时前
双 Token 与前端使用 Crypto 公私钥鉴权换 Token,哪种方案更好?
前端·后端
AI入职记16 小时前
AI 代码审查召回率实测:20 个骗过单元测试的变异体,抓到 13 个
后端
浮链序16 小时前
别让 AI 客服把用户当猴耍:百来行 Python 把「转人工」做成一条高可用链路
后端
属于自己的天空16 小时前
Claude Code 插件完全指南:官方市场怎么用,怎么自己写(v2.1.269+)
后端
Tokenfree16 小时前
给大模型调用加一个熔断器:60 行代码,三个阈值
后端
Jason_chen16 小时前
Linux 进程调度器深入解析与实时性优化指南(基于 Linux 5.10+)
后端
1360967572316 小时前
三个 Agent 干同一个活,职责边界到底该怎么划
后端