美食探店应用基于HarmonyOS API 24实战解析定义一个键为字符串、值为 CuisineMeta 类型的映射对象,返回值必定符合 CuisineMeta 接口结构

智能驾驶小白必看:MEB低速紧急制动系统的工作原理与传感器配置详解 本文深入解析了MEB低速紧急制动系统的工作原理与传感器配置。该系统作为智能驾驶的关键安全冗余,专为应对停车场、拥堵路段等复杂低速场景设计。文章详细拆解了其如何通过超声波雷达、环视摄像头等传感器融合感知环境,并基于碰撞时间(TTC)等模型进行风险评估与分级制动,最终实现主动防碰撞,是智能驾驶技术实用化的重要体现。 阅读详情

本文将对一个基于 HarmonyOS ArkTS 开发的美食探店类应用进行逐段源码解析。该应用涵盖了从数据建模、状态管理、UI 布局到交互设计的完整移动端开发链路,包含推荐、收藏、附近、排行和个人中心五大核心模块。通过逐段拆解,我们将深入探讨 ArkTS 声明式 UI 的开发范式、@Observed 与 @State 等状态装饰器的实际运用、自定义 Builder 的组件化封装思想,以及模态弹窗、分类筛选、数据排序等常见业务场景的实现细节。

在这里插入图片描述


一、接口定义与类型系统:为业务数据奠定类型基石

ArkTS 作为 TypeScript 的超集,具备强大的静态类型系统。在应用开发初期,先定义核心业务的接口类型,是一种良好的工程实践。这不仅能获得 IDE 的智能提示与类型检查,还能让数据结构的边界清晰可见。

1.1 菜系元数据接口

interface CuisineMeta {
  label: string
  icon: string
  color: string
  bg: string
}

在这里插入图片描述

技术解析:

CuisineMeta 接口定义了菜系分类所需的可视化元数据。该接口包含四个字段:label 用于显示菜系的中文名称,如"中餐"、“日料”;icon 存储对应的 Emoji 图标字符,这是移动端应用中轻量级图标展示的一种常见做法,避免了引入额外的图标字体库或图片资源;color 定义了该菜系在界面中的主题色,用于文字、边框或强调元素的着色;bg 则是带有透明度(如 #33E53935 中的前两位 33 表示 20% 不透明度)的背景色,常用于标签、徽章或浅色背景区域。

这种将视觉属性与业务数据分离的设计思路非常精妙。在传统的开发模式中,视觉表现往往散落在各个 UI 组件中,难以统一维护。而此处通过配置化的方式,将每种菜系的视觉特征集中管理,后续无论是更换主题色、调整透明度,还是新增菜系分类,都只需修改一处配置对象即可全局生效。这种"数据驱动视觉"的设计理念,正是声明式 UI 框架所倡导的。

1.2 价格等级元数据接口

interface PriceLevelMeta {
  label: string
  symbol: string
  color: string
}

在这里插入图片描述

技术解析:

PriceLevelMeta 接口负责描述餐厅的价格等级信息。label 提供语义化描述,如"便宜"、“适中”、“较贵”、“昂贵”;symbol 使用连续的"¥"字符直观展示价格档位(¥、¥¥、¥¥¥、¥¥¥¥),这种符号化的表达方式在餐饮类 App 中极为常见,用户一眼即可感知消费水平;color 同样为主题色,不同价格等级可以对应不同的色彩暗示,例如绿色代表实惠、橙色代表中等、红色或紫色代表高端。

值得注意的是,价格等级使用 number 作为键(1 到 4),而菜系使用 string 作为键。这种差异反映了业务实体的本质特征:价格等级是具有明确顺序关系的离散数值,适合数学比较和排序;而菜系是名义型分类数据,没有天然的顺序关系,使用字符串键更为合理。

1.3 评价项接口

interface ReviewItem {
  restaurantName: string
  rating: number
  content: string
  date: string
  cuisine: string
}

在这里插入图片描述

技术解析:

ReviewItem 接口定义了用户评价的数据结构。restaurantName 关联被评价的餐厅;rating 为 1-5 的整数评分;content 存储评价文本内容;date 采用 MM-DD 格式的字符串表示评价日期,这种简化格式适合社交类展示场景,避免了年份信息的冗余;cuisine 字段则记录了该餐厅所属的菜系,便于在个人中心页面展示与该菜系对应的视觉风格。

该接口的设计体现了"最小够用原则",每个字段都有明确的展示用途,没有多余的冗余信息。在真实的后端对接场景中,此类接口通常还会包含用户 ID、评价 ID、点赞数、图片列表等字段,但在本示例的 Mock 数据中进行了合理简化。

1.4 设置项元数据接口

interface SettingMeta {
  icon: string
  title: string
  subtitle: string
  color: string
}

在这里插入图片描述

技术解析:

SettingMeta 接口用于描述个人中心页面的设置列表项。icon 同样使用 Emoji 字符作为图标;title 为主标题,如"我的评价"、“优惠券”;subtitle 为辅助说明文字,如"共8条评价"、“3张可用”;color 为该项的主题色,用于左侧图标或强调文字的着色。

将设置项抽象为数据接口,使得个人中心页面的列表可以通过 ForEach 循环渲染,极大地减少了重复代码。这种数据驱动列表的设计模式,是声明式 UI 开发中最基础也最重要的技巧之一。


二、数据模型与装饰器:构建响应式状态的核心

2.1 餐厅数据模型类

@Observed
export class RestaurantItem {
  id: number = 0
  name: string = ''
  cuisine: string = ''
  rating: number = 0
  priceLevel: number = 1
  address: string = ''
  tags: string[] = []
  reviewCount: number = 0
  avgPrice: number = 0
  isFavorited: boolean = false
  distance: number = 0
  imageColor: string = ''
  description: string = ''

  constructor(id: number, name: string, cuisine: string, rating: number,
    priceLevel: number, address: string, tags: string[], reviewCount: number,
    avgPrice: number, isFavorited: boolean, distance: number,
    imageColor: string, description: string) {
    this.id = id
    this.name = name
    this.cuisine = cuisine
    this.rating = rating
    this.priceLevel = priceLevel
    this.address = address
    this.tags = tags
    this.reviewCount = reviewCount
    this.avgPrice = avgPrice
    this.isFavorited = isFavorited
    this.distance = distance
    this.imageColor = imageColor
    this.description = description
  }
}

在这里插入图片描述

技术解析:

这是整个应用最核心的数据模型类。@Observed 装饰器是 HarmonyOS ArkTS 状态管理框架中的关键装饰器之一,它的作用是标记该类为"可观察对象"。与 @State 只能装饰简单类型或对象引用不同,@Observed 可以深入观察对象内部的属性变化。当 RestaurantItem 实例的任意属性(如 isFavorited)发生改变时,ArkUI 框架能够检测到这种变化,并自动触发依赖该数据的 UI 组件重新渲染。

export 关键字表明该类可以被其他模块导入使用,虽然在单文件应用中这一特性未被显式使用,但它体现了良好的模块化设计意识。

该类包含了 13 个属性,完整覆盖了一家餐厅在展示层面所需的全部信息:

  • 基础信息:id、name、address、description
  • 分类与定价:cuisine(菜系)、priceLevel(价格等级 1-4)、avgPrice(人均消费)
  • 评分数据:rating(评分 0-5)、reviewCount(评价数量)
  • 用户状态:isFavorited(是否收藏)
  • 位置信息:distance(距离,单位公里)
  • 视觉属性:imageColor(主题色,用于替代真实图片)、tags(标签数组)

构造函数采用了完整的显式参数列表,虽然参数较多,但在 Mock 数据初始化时非常直观。每个参数都有明确的类型注解,确保在实例化时不会发生类型错误。tags: string[] 使用了数组类型语法,表明一家餐厅可以拥有多个标签。

2.2 菜系配置对象

const CUISINE_CONFIG: Record<string, CuisineMeta> = {
  '中餐': { label: '中餐', icon: '🥘', color: '#E53935', bg: '#33E53935' },
  '日料': { label: '日料', icon: '🍣', color: '#FF6F00', bg: '#33FF6F00' },
  '西餐': { label: '西餐', icon: '🥩', color: '#7B1FA2', bg: '#337B1FA2' },
  '韩餐': { label: '韩餐', icon: '🍖', color: '#D81B60', bg: '#33D81B60' },
  '火锅': { label: '火锅', icon: '🍲', color: '#FF5722', bg: '#33FF5722' },
  '烧烤': { label: '烧烤', icon: '🔥', color: '#BF360C', bg: '#33BF360C' }
}

在这里插入图片描述

技术解析:

Record<string, CuisineMeta> 是 TypeScript 提供的工具类型,表示一个键为字符串、值为 CuisineMeta 类型的映射对象。这种类型定义确保了访问 CUISINE_CONFIG[key] 时,返回值必定符合 CuisineMeta 接口结构。

配置对象将六种主流菜系的可视化属性集中管理。每种菜系的 color 都选用了与其文化特征或食物属性相符的色彩:中餐使用中国红 #E53935,日料选用暖橙色 #FF6F00 呼应三文鱼与暖食,西餐采用深紫色 #7B1FA2 体现高端优雅,韩餐使用玫红色 #D81B60 呼应韩式辣酱,火锅采用橙红色 #FF5722 象征热辣,烧烤使用深褐色 #BF360C 对应炭火与烤物。

bg 字段中的颜色值前两位为 33,即十六进制的 51,对应约 20% 的透明度。这种半透明的背景色在白色卡片上能够产生微妙的色块效果,既区分了不同菜系,又不会过于抢眼。

2.3 价格等级配置对象

const PRICE_LEVEL_CONFIG: Record<number, PriceLevelMeta> = {
  1: { label: '便宜', symbol: '¥', color: '#4CAF50' },
  2: { label: '适中', symbol: '¥¥', color: '#FF9800' },
  3: { label: '较贵', symbol: '¥¥¥', color: '#FF5722' },
  4: { label: '昂贵', symbol: '¥¥¥¥', color: '#D81B60' }
}

在这里插入图片描述

技术解析:

Record<number, PriceLevelMeta> 定义了以数字为键的映射表,与 priceLevel 属性的类型完美对应。价格等级的颜色设计遵循了直观的语义映射:绿色 #4CAF50 代表便宜/实惠,橙色 #FF9800 代表中等消费,橙红色 #FF5722 代表较贵,玫红色 #D81B60 代表昂贵。这种从绿到红的渐变色彩心理暗示,符合用户对价格敏感度的直觉认知。

2.4 分类常量数组

const CATEGORIES: string[] = ['全部', '中餐', '日料', '西餐', '韩餐', '火锅', '烧烤']

技术解析:

CATEGORIES 数组定义了推荐页面顶部分类筛选器的全部选项。'全部' 作为第一个元素,承担了"显示所有餐厅"的默认筛选功能。数组的顺序决定了 UI 中分类标签的排列顺序。在后续的分类筛选逻辑中,当 activeCategory 为 '全部' 时,通常意味着不应用任何菜系过滤条件。


三、Mock 数据:模拟真实业务场景

3.1 餐厅 Mock 数据

const mockRestaurants: RestaurantItem[] = [
  new RestaurantItem(1, '老王川菜馆', '中餐', 4.8, 2, '南京路步行街128号', ['正宗', '辣味', '性价比'], 356, 65, true, 0.5, '#E53935', '正宗川菜,麻辣鲜香'),
  new RestaurantItem(2, '樱花日料店', '日料', 4.9, 4, '淮海中路456号', ['新鲜', '精致', '包间'], 892, 280, true, 1.2, '#FF6F00', '怀石料理,匠心之作'),
  // ... 共16条数据
]

技术解析:

Mock 数据是前端开发中用于模拟后端接口返回的重要手段。本应用构造了 16 家风格各异的餐厅数据,覆盖了全部六种菜系和四个价格等级。每条数据都经过精心设计,具有合理的业务逻辑关联:

  • 川菜馆使用红色主题,价格适中(¥65),距离近(0.5km),标签突出"正宗"、“辣味”
  • 日料店使用橙色主题,价格昂贵(¥280),评价数多(892条),标签强调"新鲜"、“精致”
  • 牛排馆使用紫色主题,定位高端(¥580),评价数最多(1245条),标签为"约会"、“高端”、“景观”

这种差异化的数据设计,使得在 UI 展示时能够产生丰富的视觉层次和信息密度,便于验证各种筛选、排序和展示逻辑的正确性。

isFavorited 属性在 16 条数据中有约一半为 true,这保证了"收藏"标签页有足够的数据可以展示。distance 属性从 0.3km 到 4.0km 不等,为"附近"页面的距离排序提供了有意义的比较基础。

3.2 评价 Mock 数据

const mockReviews: ReviewItem[] = [
  { restaurantName: '老王川菜馆', rating: 5, content: '水煮鱼太好吃了,麻辣鲜香!', date: '07-20', cuisine: '中餐' },
  { restaurantName: '樱花日料店', rating: 5, content: '三文鱼非常新鲜,环境也好', date: '07-18', cuisine: '日料' },
  // ... 共8条数据
]

技术解析:

评价数据共 8 条,分布于不同菜系和餐厅。date 字段采用倒序排列,从 07-20 递减到 07-02,模拟了最新评价在前的时间线展示逻辑。cuisine 字段的冗余存储(虽然可以通过餐厅名关联查询到菜系)是一种常见的反范式设计,目的是在展示评价列表时,能够直接根据菜系渲染对应的背景色或图标,无需额外的关联查询。

3.3 个人中心设置项 Mock 数据

const PROFILE_SETTINGS: SettingMeta[] = [
  { icon: '📝', title: '我的评价', subtitle: '共8条评价', color: '#E53935' },
  { icon: '❤️', title: '我的收藏', subtitle: '共8家餐厅', color: '#FF6F00' },
  { icon: '🎟️', title: '优惠券', subtitle: '3张可用', color: '#FFD54F' },
  { icon: '📍', title: '我的足迹', subtitle: '已探索15家店', color: '#4CAF50' },
  { icon: '🌙', title: '夜间模式', subtitle: '跟随系统', color: '#7B1FA2' },
  { icon: '🔔', title: '消息通知', subtitle: '已开启', color: '#D81B60' },
  { icon: '❓', title: '关于我们', subtitle: 'v3.1.0', color: '#9E9E9E' }
]

技术解析:

设置项数组的设计体现了数据驱动列表的思想。每个设置项都有独立的 color,使得列表呈现丰富的色彩搭配,避免了单一色调的单调感。subtitle 字段中的动态数据(如"共8条评价"、“3张可用”)在真实应用中应当通过计算属性或状态管理动态生成,此处为简化实现而使用硬编码字符串。


四、工具函数:业务逻辑的纯函数封装

4.1 平均评分计算

function getAverageRating(): number {
  return Math.round(mockRestaurants.reduce((sum: number, r: RestaurantItem) => sum + r.rating, 0) / mockRestaurants.length * 10) / 10
}

技术解析:

该函数使用 Array.prototype.reduce 计算所有餐厅评分的平均值,并通过 Math.round(value * 10) / 10 的技巧保留一位小数。这种保留小数的方式比 toFixed(1) 更精确,因为 toFixed 返回字符串类型,而此处需要继续进行数值运算。函数内部显式标注了参数类型 (sum: number, r: RestaurantItem),增强了代码的可读性和类型安全性。

4.2 收藏数量计算

function getFavoritedCount(): number {
  return mockRestaurants.filter((r: RestaurantItem) => r.isFavorited).length
}

技术解析:

filter 方法筛选出 isFavorited 为 true 的餐厅数组,然后通过 .length 获取收藏总数。这是一个典型的纯函数,没有副作用,输入确定则输出确定。在推荐页面的 Banner 和个人中心的统计区域都会调用此函数,体现了工具函数的复用价值。

4.3 平均价格计算

function getAveragePrice(): number {
  return Math.round(mockRestaurants.reduce((sum: number, r: RestaurantItem) => sum + r.avgPrice, 0) / mockRestaurants.length)
}

技术解析:

与平均评分类似,但此处直接取整(无小数),因为人均消费在宏观统计层面通常不需要精确到个位数。该结果展示在推荐页面的 Banner 区域,作为平台整体的消费水平概览。

4.4 菜系数量统计

function getCuisineCount(cuisine: string): number {
  return mockRestaurants.filter((r: RestaurantItem) => r.cuisine === cuisine).length
}

技术解析:

这是一个带参数的纯函数,根据传入的菜系名称返回对应餐厅的数量。在排行页面的"菜系分布"区域,该函数被重复调用六次,分别统计六大菜系的餐厅数量,用于绘制横向分布条形图。


五、枚举与主入口:Tab 导航的架构设计

5.1 Tab 枚举定义

enum AppTab {
  RECOMMEND = 0,
  FAVORITE = 1,
  NEARBY = 2,
  RANKING = 3,
  PROFILE = 4
}

技术解析:

使用枚举(enum)而非魔法数字(magic number)来标识 Tab 页,是类型安全的最佳实践。枚举成员既有数值(0-4),又有语义化的名称(RECOMMEND、FAVORITE 等)。在 if/else 条件判断中使用枚举,可以大幅降低代码的维护成本和出错概率。当需要新增或调整 Tab 顺序时,只需修改枚举定义即可,无需在代码中搜寻散落的数字。

5.2 主入口组件 MainApp

@Entry
@Component
struct MainApp {
  @State activeTab: AppTab = AppTab.RECOMMEND

  @Builder
  contentArea() {
    Column() {
      if (this.activeTab === AppTab.RECOMMEND) {
        RecommendContent()
      } else if (this.activeTab === AppTab.FAVORITE) {
        FavoriteContent()
      } else if (this.activeTab === AppTab.NEARBY) {
        NearbyContent()
      } else if (this.activeTab === AppTab.RANKING) {
        RankingContent()
      } else if (this.activeTab === AppTab.PROFILE) {
        ProfileContent()
      }
    }
    .layoutWeight(1)
  }

技术解析:

@Entry 装饰器标记 MainApp 为应用的入口组件,是 ArkTS 应用启动时第一个被渲染的组件。@Component 则是所有自定义组件的必需装饰器。

@State activeTab: AppTab = AppTab.RECOMMEND 声明了一个组件级状态变量。@State 是 ArkTS 最基础的状态装饰器,当它修饰的变量发生变化时,组件会自动重新渲染。初始值设为 AppTab.RECOMMEND,意味着应用启动后默认展示"推荐"页面。

contentArea() 是一个自定义 Builder 函数(通过 @Builder 装饰器标记)。Builder 是 ArkTS 中复用 UI 结构的重要手段,它类似于一个返回 UI 描述的函数。在 contentArea 中,使用了 if/else if 条件分支来实现 Tab 内容的切换——这是一种条件渲染(Conditional Rendering)模式。当 activeTab 变化时,框架会销毁旧组件并创建新组件,确保每个 Tab 页都有独立的组件生命周期。

.layoutWeight(1) 是关键的布局属性,它使得 Column 在垂直方向上占据所有剩余空间。这意味着内容区域会填充底部导航栏之上的全部空间,实现了经典的"内容区 + 底部导航"布局结构。

5.3 底部导航栏项 Builder

  @Builder
  bottomTabItem(icon: string, label: string, tab: AppTab) {
    Column() {
      Text(icon)
        .fontSize(20)
        .opacity(this.activeTab === tab ? 1.0 : 0.45)
      Text(label)
        .fontSize(9)
        .fontColor(this.activeTab === tab ? '#E53935' : '#999999')
        .margin({ top: 1 })
      if (this.activeTab === tab) {
        Column()
          .width(18).height(3)
          .backgroundColor('#E53935')
          .borderRadius(2)
          .margin({ top: 2 })
      }
    }
    .layoutWeight(1)
    .alignItems(HorizontalAlign.Center)
    .padding({ top: 5, bottom: 5 })
    .onClick(() => { this.activeTab = tab })
  }

技术解析:

bottomTabItem 是底部导航栏中单个 Tab 按钮的 Builder 封装。它接收三个参数:图标字符、标签文字和对应的 Tab 枚举值。

该 Builder 实现了丰富的交互视觉反馈:

  • 图标透明度:当 Tab 被选中时,图标完全不透明(opacity: 1.0),未选中时半透明(0.45),形成明显的视觉层级差异。
  • 文字颜色:选中时使用主题红色 #E53935,未选中时使用灰色 #999999。
  • 底部指示条:通过条件渲染 if (this.activeTab === tab),仅在选中状态下显示一条宽 18、高 3 的红色圆角短线,作为当前位置的明确指示。这种设计在 iOS 和 Material Design 中都非常常见。

.layoutWeight(1) 让每个 Tab 项在水平方向上均分父容器宽度,保证了五个 Tab 按钮等宽分布。.onClick(() => { this.activeTab = tab }) 是点击事件处理器,点击后将 activeTab 设置为对应的枚举值,触发 MainApp 的重新渲染,进而切换内容区域显示的组件。

5.4 主组件构建

  build() {
    Column() {
      this.contentArea()
      Row() {
        this.bottomTabItem('🔥', '推荐', AppTab.RECOMMEND)
        this.bottomTabItem('⭐', '收藏', AppTab.FAVORITE)
        this.bottomTabItem('📍', '附近', AppTab.NEARBY)
        this.bottomTabItem('🏆', '排行', AppTab.RANKING)
        this.bottomTabItem('👤', '我的', AppTab.PROFILE)
      }
      .width('100%')
      .backgroundColor('#FFFFFF')
      .padding({ top: 4, bottom: 6 })
      .shadow({ radius: 8, color: '#1A000000', offsetY: -2 })
    }
    .width('100%').height('100%')
    .backgroundColor('#FFF8E1')
  }
}

技术解析:

build() 方法是每个 ArkTS 组件必须实现的方法,负责描述组件的 UI 结构。在 MainApp 的 build 方法中,整体采用 Column 垂直布局,从上到下依次为内容区域和底部导航栏。

底部导航栏使用 Row 水平布局,依次调用五次 bottomTabItem Builder,传入对应的 Emoji 图标和标签。Row 的背景设为白色,并添加了向上的阴影(offsetY: -2),阴影颜色 #1A000000 具有约 10% 的不透明度,这种微妙的阴影效果让底部导航栏与内容区产生层级分离感,符合 Material Design 的 elevation 设计理念。

根 Column 的背景色设为 #FFF8E1,一种温暖的米黄色,为整个应用奠定了温馨、美食相关的视觉基调。宽高均设为 100%,确保应用占满整个屏幕。


六、推荐页面:复杂交互与状态管理的典范

6.1 组件状态定义

@Component
struct RecommendContent {
  @State activeCategory: string = '全部'
  @State showAddModal: boolean = false
  @State showDeleteModal: boolean = false
  @State selectedRestaurant: RestaurantItem | null = null

技术解析:

RecommendContent 是五个 Tab 页面中交互最复杂的组件。它声明了四个状态变量:

  • activeCategory: 记录当前选中的菜系分类,初始为 '全部'。该状态驱动顶部分类 Chip 的高亮样式和列表的筛选逻辑。
  • showAddModal 和 showDeleteModal: 两个布尔状态控制两个模态弹窗的显示与隐藏。在 ArkTS 中,模态框通常通过条件渲染(if (this.showAddModal))实现,而非传统 DOM 操作中的 display: none/block。
  • selectedRestaurant: 当前选中的餐厅对象,用于在删除评价弹窗中显示餐厅名称。类型为 RestaurantItem | null,表明它可能为空。

6.2 Banner 卡片 Builder

  @Builder
  bannerCard() {
    Column() {
      Text('🍜 发现美食')
        .fontSize(24).fontColor('#FFFFFF').fontWeight(FontWeight.Bold)
      Text('探索城市中最好的餐厅')
        .fontSize(12).fontColor('#FFD54F')
        .margin({ top: 4 })
      Row() {
        Column() {
          Text(`${mockRestaurants.length}`)
            .fontSize(20).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
          Text('餐厅')
            .fontSize(9).fontColor('#FFE8B0')
        }
        .alignItems(HorizontalAlign.Center).margin({ right: 20 })
        Column() {
          Text(`${getAverageRating()}`)
            .fontSize(20).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
          Text('平均评分')
            .fontSize(9).fontColor('#FFE8B0')
        }
        .alignItems(HorizontalAlign.Center).margin({ right: 20 })
        Column() {
          Text(`¥${getAveragePrice()}`)
            .fontSize(20).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
          Text('人均')
            .fontSize(9).fontColor('#FFE8B0')
        }
        .alignItems(HorizontalAlign.Center)
      }
      .margin({ top: 12 })
    }
    .width('100%')
    .padding(20)
    .borderRadius(20)
    .linearGradient({
      angle: 135,
      colors: [['#E53935', 0.0], ['#FF6F00', 1.0]]
    })
    .shadow({ radius: 8, color: '#33E53935', offsetY: 4 })
    .margin({ left: 16, right: 16, bottom: 12 })
  }

技术解析:

Banner 卡片是推荐页面的视觉焦点。它采用了从红色到橙色的 135 度线性渐变(linearGradient),角度 135 度表示从左上到右下的对角渐变,这种斜向渐变比水平或垂直渐变更具动感和现代感。

卡片内部展示三组平台级统计数据:餐厅总数、平均评分和人均消费。数值使用金黄色 #FFD54F 突出显示,与红色背景形成高对比度;标签文字使用浅黄色 #FFE8B0,作为次要信息的柔和展示。三个统计数据使用 Row 水平排列,每个数据组内部使用 Column 垂直居中布局,组间通过 margin({ right: 20 }) 保持间距。

.shadow({ radius: 8, color: '#33E53935', offsetY: 4 }) 为卡片添加了向下的阴影,阴影颜色使用了与背景渐变起始色相同的红色,并带有 20% 透明度,这种与卡片同色系阴影的设定,使阴影效果更加自然和谐。

6.3 分类标签 Chip Builder

  @Builder
  categoryChip(cat: string) {
    Text(cat)
      .fontSize(12)
      .fontColor(this.activeCategory === cat ? '#FFFFFF' : '#666666')
      .padding({ left: 14, right: 14, top: 6, bottom: 6 })
      .backgroundColor(this.activeCategory === cat ? CUISINE_CONFIG[cat]?.color ?? '#E53935' : '#FFFFFF')
      .borderRadius(16)
      .shadow(this.activeCategory === cat ? { radius: 4, color: '#33E53935', offsetY: 2 } : { radius: 2, color: '#10000000', offsetY: 1 })
      .onClick(() => { this.activeCategory = cat })
  }

技术解析:

categoryChip 实现了典型的"胶囊标签"(Pill Tag)交互组件。当标签被选中时,文字变为白色,背景变为该菜系对应的主题色(通过 CUISINE_CONFIG[cat]?.color ?? '#E53935' 获取,空值合并运算符 ?? 保证当配置不存在时回退到默认红色);未选中时,文字为深灰色,背景为纯白色。

阴影效果也有状态差异:选中时阴影更明显(radius 4,红色半透明),未选中时阴影更淡(radius 2,黑色极低透明度)。这种差异化的阴影处理,让选中的标签在视觉上"浮起",产生按压与凸起的状态对比。

.borderRadius(16) 配合足够的水平内边距(14),形成了完美的圆角胶囊形状。.onClick 事件直接修改 activeCategory 状态,触发整个列表的重新筛选和渲染。

6.4 餐厅卡片 Builder

  @Builder
  restaurantCard(item: RestaurantItem) {
    Column() {
      Row() {
        Column()
          .width(60).height(60)
          .borderRadius(12)
          .backgroundColor(item.imageColor)

        Column() {
          Text(item.name)
            .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Bold)
          Row() {
            Text('★'.repeat(Math.floor(item.rating)))
              .fontSize(12).fontColor('#FFD54F')
            Text(item.rating.toFixed(1))
              .fontSize(11).fontColor('#FF9800')
              .margin({ left: 4 })
            Text(`${item.reviewCount}条评价`)
              .fontSize(10).fontColor('#999999')
              .margin({ left: 8 })
          }
          .margin({ top: 4 })
          Row() {
            Text(CUISINE_CONFIG[item.cuisine].icon)
              .fontSize(10)
            Text(CUISINE_CONFIG[item.cuisine].label)
              .fontSize(10).fontColor(CUISINE_CONFIG[item.cuisine].color)
              .margin({ left: 2 })
            Text(PRICE_LEVEL_CONFIG[item.priceLevel].symbol)
              .fontSize(11).fontColor(PRICE_LEVEL_CONFIG[item.priceLevel].color)
              .margin({ left: 8 })
            Text(`人均¥${item.avgPrice}`)
              .fontSize(10).fontColor('#999999')
              .margin({ left: 8 })
          }
          .margin({ top: 4 })
          Text(item.address)
            .fontSize(10).fontColor('#AAAAAA')
            .margin({ top: 4 })
        }
        .layoutWeight(1)
        .margin({ left: 12 })
        .alignItems(HorizontalAlign.Start)

        Column() {
          Text(item.isFavorited ? '❤️' : '🤍')
            .fontSize(20)
            .onClick(() => { item.isFavorited = !item.isFavorited })
          Text(`${item.distance}km`)
            .fontSize(9).fontColor('#E53935')
            .margin({ top: 6 })
            .padding({ left: 4, right: 4, top: 2, bottom: 2 })
            .backgroundColor('#33E53935')
            .borderRadius(4)
        }
        .alignItems(HorizontalAlign.Center)
      }
      .width('100%')

      Row() {
        ForEach(item.tags, (tag: string) => {
          Text(tag)
            .fontSize(9).fontColor('#FF6F00')
            .padding({ left: 6, right: 6, top: 2, bottom: 2 })
            .backgroundColor('#FFF3E0')
            .borderRadius(4)
            .margin({ right: 6 })
        })
        Column().layoutWeight(1)
        Text(item.description)
          .fontSize(9).fontColor('#BBBBBB')
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      }
      .width('100%')
      .margin({ top: 8 })
    }
    .width('100%')
    .padding(14)
    .backgroundColor('#FFFFFF')
    .borderRadius(16)
    .shadow({ radius: 4, color: '#15000000', offsetY: 2 })
    .margin({ bottom: 10 })
    .onClick(() => {
      this.selectedRestaurant = item
      this.showDeleteModal = true
    })
  }

技术解析:

restaurantCard 是整个应用中信息量最密集的 UI 组件,它完整展示了一家餐厅的全部核心信息。

布局结构分析:
卡片整体使用 Column 垂直布局,内部分为上下两层:

上层信息区(Row 水平布局):

  • 左侧色块:一个 60x60 的圆角矩形(borderRadius(12)),使用 item.imageColor 作为背景色。在实际产品中,这里通常是一张真实的餐厅图片;在示例中使用纯色块作为占位,既保持了视觉节奏,又避免了引入图片资源。
  • 中间信息区:使用 layoutWeight(1) 占据所有剩余宽度,内部垂直排列餐厅名称、评分行、菜系价格行和地址。alignItems(HorizontalAlign.Start) 确保所有文本左对齐。
    • 评分行使用 '★'.repeat(Math.floor(item.rating)) 生成对应数量的实心星星字符,配合 .toFixed(1) 格式化的数字评分和灰色评价数量,构成了完整的评分展示体系。
    • 菜系价格行通过查询配置对象获取图标、文字、颜色和价格符号,展现了配置化驱动的强大复用能力。
  • 右侧操作区:垂直排列收藏按钮和距离标签。收藏按钮使用三元表达式 item.isFavorited ? '❤️' : '🤍' 实现红心/白心的状态切换,点击时直接取反 item.isFavorited。由于 RestaurantItem 类被 @Observed 装饰器标记,这一属性变化会被框架自动捕获并触发 UI 更新。距离标签使用红色半透明背景的小徽章样式,突出显示餐厅与用户的距离。

下层标签区(Row 水平布局):

  • 左侧使用 ForEach 循环渲染所有标签,每个标签采用橙色文字 + 浅橙色背景的配色方案,圆角 4 的小标签样式。
  • Column().layoutWeight(1) 作为弹性占位,将描述文字推到最右侧。
  • 描述文字使用 .maxLines(1) 和 .textOverflow({ overflow: TextOverflow.Ellipsis }) 实现单行省略,避免长文本破坏布局。

卡片容器样式:
白色背景、16 圆角、微妙的阴影效果,底部 10 的外边距确保卡片之间有呼吸感。点击卡片会打开删除评价弹窗(虽然业务逻辑上这里更像是"查看详情"的入口,但示例中简化为弹窗演示)。

6.5 模态弹窗公共 Builder

  @Builder
  modalOverlay(onClose: () => void) {
    Column()
      .width('100%').height('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .onClick(onClose)
  }

技术解析:

modalOverlay 是一个高度可复用的公共 Builder,用于创建模态弹窗的背景遮罩层。它创建一个占满全屏的半透明黑色层(rgba(0,0,0,0.5)),并监听点击事件,点击时调用传入的 onClose 回调函数关闭弹窗。这种将关闭逻辑通过回调参数化的设计,使得同一个 Builder 可以被不同的弹窗复用,体现了" Don’t Repeat Yourself "的编程原则。

6.6 写评价弹窗 Builder

  @Builder
  addReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showAddModal = false })

      Column() {
        Text('✍️ 写评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 16 })

        Column() {
          Text('餐厅名称')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('老王川菜馆')
            .fontSize(14).fontColor('#333333')
            .padding({ top: 8, bottom: 8, left: 12, right: 12 })
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评分')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('☆').fontSize(28).fontColor('#CCCCCC')
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评价内容')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('这家店味道很正宗,服务也不错...')
            .fontSize(13).fontColor('#AAAAAA')
            .padding(12)
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('标签')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('好吃').fontSize(11).fontColor('#FF6F00')
              .padding({ left: 8, right: 8, top: 4, bottom: 4 })
              .backgroundColor('#FFF3E0').borderRadius(6).margin({ right: 6 })
            Text('实惠').fontSize(11).fontColor('#4CAF50')
              .padding({ left: 8, right: 8, top: 4, bottom: 4 })
              .backgroundColor('#E8F5E9').borderRadius(6).margin({ right: 6 })
            Text('环境好').fontSize(11).fontColor('#7B1FA2')
              .padding({ left: 8, right: 8, top: 4, bottom: 4 })
              .backgroundColor('#F3E5F5').borderRadius(6)
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 16 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showAddModal = false })
          Text('发布评价')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showAddModal = false })
        }
        .width('100%')
      }
      .width('88%')
      .padding(20)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

技术解析:

addReviewModal 展示了写评价弹窗的完整 UI 结构。该弹窗采用了经典的"遮罩层 + 居中内容卡片"布局模式。

弹窗内容分为五个区块:标题区、餐厅名称区、评分区、评价内容区和标签区,最后是底部的操作按钮行。

表单区域设计特点:

  • 每个表单字段都使用 Column 垂直包裹,顶部是灰色小字标签(.fontSize(12).fontColor('#999999')),下方是内容区域。
  • alignSelf(ItemAlign.Start) 让标签在父容器中左对齐,打破了默认的居中行为。
  • 输入框区域使用 #FFF8E1 浅黄色背景作为只读/占位状态的颜色提示,圆角 8 提供了柔和的边缘。
  • 评分区域使用四个实心黄星和一个空心灰星,直观展示 4 分评分。每个星星都是独立的 Text 组件,在真实应用中可以通过循环和点击事件实现可交互的星级评分组件。
  • 标签区域展示了三个不同颜色的标签,分别使用橙/绿/紫的配色方案,与菜系配置中的色彩体系保持一致。

底部按钮设计:
"取消"和"发布评价"按钮采用左右等宽布局(均使用 layoutWeight(1)),中间通过 margin({ left: 12 }) 保持间距。取消按钮使用浅灰色背景,发布按钮使用红色主题色背景,形成明确的主次操作区分。两个按钮的点击事件都仅关闭了弹窗(在真实应用中,发布按钮应当包含表单验证和数据提交逻辑)。

内容卡片宽度设为 88%,既保证了在窄屏设备上的边距,又在大屏设备上不会过度拉伸。20 的圆角和强烈的阴影(radius: 20, color: '#33000000')让卡片产生明显的浮层效果,与底层页面清晰分离。

6.7 删除评价确认弹窗 Builder

  @Builder
  deleteReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showDeleteModal = false })

      Column() {
        Text('🗑️')
          .fontSize(40).margin({ bottom: 12 })
        Text('删除评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 8 })
        Text(`确定要删除对「${this.selectedRestaurant?.name ?? ''}」的评价吗?`)
          .fontSize(13).fontColor('#999999')
          .textAlign(TextAlign.Center)
          .margin({ bottom: 20 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showDeleteModal = false })
          Text('删除')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showDeleteModal = false })
        }
        .width('100%')
      }
      .width('72%')
      .padding(24)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

技术解析:

删除弹窗采用了比写评价弹窗更窄的卡片宽度(72% vs 88%),因为删除确认所需的信息量较少,窄卡片更聚焦用户注意力。弹窗顶部使用了一个巨大的垃圾桶 Emoji(🗑️,fontSize 40)作为视觉锚点,立刻传达操作性质。

提示文字中使用了可选链操作符 ?. 和空值合并运算符 ??:this.selectedRestaurant?.name ?? ''。这是一种防御性编程实践,即使 selectedRestaurant 为 null,也不会引发运行时错误,而是优雅地回退到空字符串。

"删除"按钮使用红色背景,遵循了业界对"删除/危险操作"使用红色警示的通用设计规范。

6.8 推荐页面构建

  build() {
    Stack() {
      Column() {
        // Header
        Row() {
          Text('🔥 推荐美食')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text('🔍')
            .fontSize(20).fontColor('#666666')
            .margin({ right: 12 })
          Text('✍️')
            .fontSize(20).fontColor('#E53935')
            .padding({ left: 8, right: 8 })
            .backgroundColor('#33E53935')
            .borderRadius(16)
            .onClick(() => { this.showAddModal = true })
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        // Banner
        this.bannerCard()

        // Category Chips
        Scroll() {
          Row() {
            ForEach(CATEGORIES, (cat: string) => {
              this.categoryChip(cat)
            })
          }
          .padding({ left: 16, right: 16 })
        }
        .scrollable(ScrollDirection.Horizontal)
        .scrollBar(BarState.Off)
        .padding({ bottom: 8 })

        // Restaurant List
        Scroll() {
          Column() {
            this.restaurantCard(mockRestaurants[0])
            this.restaurantCard(mockRestaurants[1])
            // ... 共16条
          }
          .padding({ left: 16, right: 16, bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')

      if (this.showAddModal) {
        this.addReviewModal()
      }
      if (this.showDeleteModal) {
        this.deleteReviewModal()
      }
    }
    .width('100%').height('100%')
  }
}

技术解析:

推荐页面使用 Stack 作为根容器,这是实现模态弹窗的关键布局策略。Stack 允许子组件按声明顺序层叠排列,后声明的组件位于上层。因此,当 showAddModal 或 showDeleteModal 为 true 时,弹窗组件会被渲染在页面内容之上,形成正确的遮罩层级关系。

页面内容使用 Column 垂直布局,从上到下依次为:

  1. 顶部导航栏:左侧是页面标题"🔥 推荐美食",右侧是搜索图标和写评价按钮。Column().layoutWeight(1) 作为弹性占位,将右侧按钮推到最右边。写评价按钮使用了红色半透明圆形背景,点击时打开写评价弹窗。
  2. Banner 卡片:调用 bannerCard() Builder 展示平台统计数据。
  3. 分类标签栏:使用 Scroll 包裹 Row,实现水平滚动分类标签。scrollable(ScrollDirection.Horizontal) 明确指定水平滚动方向,scrollBar(BarState.Off) 隐藏滚动条以获得更干净的外观。ForEach(CATEGORIES, ...) 遍历所有分类生成对应的 Chip。
  4. 餐厅列表:使用 Scroll 包裹 Column,实现垂直滚动列表。layoutWeight(1) 确保列表区域占据 Banner 和分类栏之外的所有剩余垂直空间。列表中依次渲染 16 家餐厅的卡片(在实际应用中,这里应当配合 ForEach 和筛选逻辑动态渲染,但示例中为了简化而直接硬编码了全部卡片)。

弹窗的条件渲染位于 Stack 的最外层,确保它们始终覆盖在所有内容之上。


七、收藏页面:网格布局与状态复用

7.1 状态定义与模态弹窗

@Component
struct FavoriteContent {
  @State showEditModal: boolean = false
  @State showDeleteModal: boolean = false
  @State selectedItem: RestaurantItem | null = null

  @Builder
  modalOverlay(onClose: () => void) {
    Column()
      .width('100%').height('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .onClick(onClose)
  }

技术解析:

收藏页面的状态设计与推荐页面类似,但针对收藏场景进行了调整:showEditModal 用于控制编辑评价弹窗,showDeleteModal 用于控制取消收藏确认弹窗,selectedItem 记录当前选中的收藏项。

值得注意的是,收藏页面独立定义了自己的 modalOverlay Builder,虽然代码与推荐页面中的版本完全相同。这种重复在单文件示例中是可以接受的,但在大型工程中,通常会将此类公共 Builder 提取到公共工具库中。

7.2 编辑评价弹窗

  @Builder
  editReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showEditModal = false })

      Column() {
        Text('✏️ 编辑评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 16 })

        Column() {
          Text('餐厅')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text(this.selectedItem?.name ?? '')
            .fontSize(14).fontColor('#333333')
            .padding({ top: 8, bottom: 8, left: 12, right: 12 })
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评分')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F')
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评价内容')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('这家店味道很正宗,服务也不错,推荐!')
            .fontSize(13).fontColor('#333333')
            .padding(12)
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 16 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showEditModal = false })
          Text('保存修改')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#FF6F00').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showEditModal = false })
        }
        .width('100%')
      }
      .width('88%')
      .padding(20)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

技术解析:

编辑弹窗与推荐页面的写评价弹窗结构高度相似,但有几点关键差异:

  • 标题变为"编辑评价",使用橙色铅笔 Emoji 作为图标。
  • 餐厅名称区域使用 this.selectedItem?.name ?? '' 动态绑定当前选中项的名称,而非硬编码。
  • 评分区域显示五颗实心黄星(暗示 5 分评价),而非写评价弹窗中的 4 星。
  • 保存按钮使用橙色 #FF6F00 而非红色,形成操作类型上的视觉区分(新增用红,编辑用橙)。

7.3 取消收藏确认弹窗

  @Builder
  deleteReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showDeleteModal = false })

      Column() {
        Text('🗑️')
          .fontSize(40).margin({ bottom: 12 })
        Text('取消收藏')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 8 })
        Text(`确定要取消收藏「${this.selectedItem?.name ?? ''}」吗?`)
          .fontSize(13).fontColor('#999999')
          .textAlign(TextAlign.Center)
          .margin({ bottom: 20 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showDeleteModal = false })
          Text('确定')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showDeleteModal = false })
        }
        .width('100%')
      }
      .width('72%')
      .padding(24)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

技术解析:

取消收藏弹窗与推荐页面的删除评价弹窗几乎完全一致,仅标题和提示文案有所调整。这种设计上的统一性降低了用户的认知成本——相同结构的弹窗意味着相同的交互模式和操作预期。

7.4 收藏网格卡片 Builder

  @Builder
  favoriteGridCard(item: RestaurantItem) {
    Column() {
      Column()
        .width('100%').height(70)
        .borderRadius(12)
        .backgroundColor(item.imageColor)
        .linearGradient({
          angle: 135,
          colors: [[item.imageColor, 0.0], ['#FFE0B2', 1.0]]
        })

      Column() {
        Text(item.name)
          .fontSize(13).fontColor('#333333').fontWeight(FontWeight.Bold)
        Row() {
          Text('★'.repeat(Math.floor(item.rating)))
            .fontSize(10).fontColor('#FFD54F')
          Text(item.rating.toFixed(1))
            .fontSize(9).fontColor('#FF9800')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
        Row() {
          Text(CUISINE_CONFIG[item.cuisine].icon).fontSize(9)
          Text(CUISINE_CONFIG[item.cuisine].label)
            .fontSize(9).fontColor(CUISINE_CONFIG[item.cuisine].color)
            .margin({ left: 2 })
          Text(PRICE_LEVEL_CONFIG[item.priceLevel].symbol)
            .fontSize(10).fontColor(PRICE_LEVEL_CONFIG[item.priceLevel].color)
            .margin({ left: 6 })
          Text(`¥${item.avgPrice}`)
            .fontSize(9).fontColor('#999999')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
      }
      .width('100%')
      .padding(8)
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .backgroundColor('#FFFFFF')
    .borderRadius(12)
    .shadow({ radius: 4, color: '#15000000', offsetY: 2 })
    .margin({ bottom: 10 })
    .onClick(() => {
      this.selectedItem = item
      this.showEditModal = true
    })
  }

技术解析:

favoriteGridCard 是收藏页面专用的卡片组件,采用了与推荐页面完全不同的布局策略——垂直紧凑布局,更适合网格排列。

卡片顶部是一个宽 100%、高 70 的色块区域,使用从 item.imageColor 到浅橙色 #FFE0B2 的线性渐变。这种渐变效果让原本单调的纯色块产生了光影层次,视觉上更加丰富。色块圆角 12 与卡片圆角一致,形成了内部的视觉嵌套。

下方信息区垂直排列餐厅名称、评分行和菜系价格行。所有字体尺寸都比推荐页面的卡片小一号(13/10/9 vs 15/12/10),这是因为网格卡片的空间更为局促。alignItems(HorizontalAlign.Start) 确保所有信息左对齐,符合中文阅读习惯。

点击卡片会打开编辑评价弹窗,这与推荐页面点击卡片打开删除弹窗的业务逻辑不同,体现了不同场景下用户意图的差异:在推荐页,用户更可能浏览后删除;在收藏页,用户更可能查看并编辑已有评价。

7.5 收藏页面构建

  build() {
    Stack() {
      Column() {
        Row() {
          Text('⭐ 我的收藏')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text(`${getFavoritedCount()}家`)
            .fontSize(12).fontColor('#FF9800')
            .padding({ left: 8, right: 8, top: 4, bottom: 4 })
            .backgroundColor('#FFF3E0')
            .borderRadius(10)
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        Scroll() {
          Column() {
            Row() {
              this.favoriteGridCard(mockRestaurants[0])
              this.favoriteGridCard(mockRestaurants[1])
            }
            Row() {
              this.favoriteGridCard(mockRestaurants[3])
              this.favoriteGridCard(mockRestaurants[4])
            }
            // ... 多行
          }
          .padding({ left: 16, right: 16, bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')

      if (this.showEditModal) {
        this.editReviewModal()
      }
      if (this.showDeleteModal) {
        this.deleteReviewModal()
      }
    }
    .width('100%').height('100%')
  }
}

技术解析:

收藏页面的顶部导航栏右侧展示收藏数量徽章(${getFavoritedCount()}家),使用橙色文字配合浅橙色背景,与标题的红色形成同色系层次。

内容区域采用两列网格布局,通过 Column 包裹多组 Row 实现。每组 Row 中包含两个 favoriteGridCard 调用,自然形成左右两列的网格效果。由于 ArkTS 没有专门的 Grid 组件,这种基于 Row + Column 的组合布局是实现网格效果的常用技巧。

需要注意的是,示例中的网格数据是硬编码的(mockRestaurants[0]、mockRestaurants[1] 等),在实际应用中应当通过 filter 筛选出 isFavorited 为 true 的餐厅,然后使用嵌套循环动态渲染。


八、附近页面:地理位置与排序展示

8.1 状态与地图头部

@Component
struct NearbyContent {
  @State showAddModal: boolean = false
  @State showDeleteModal: boolean = false

  @Builder
  mapHeader() {
    Column() {
      Text('📍 当前位置: 南京路步行街')
        .fontSize(13).fontColor('#FFFFFF')
      Text('发现附近 16 家餐厅')
        .fontSize(11).fontColor('#FFD54F')
        .margin({ top: 4 })
    }
    .width('100%')
    .padding(16)
    .borderRadius(16)
    .linearGradient({
      angle: 135,
      colors: [['#E53935', 0.0], ['#FF6F00', 1.0]]
    })
    .shadow({ radius: 8, color: '#33E53935', offsetY: 3 })
    .margin({ left: 16, right: 16, bottom: 12 })
  }

技术解析:

附近页面的状态管理相对简单,仅包含两个弹窗控制状态。mapHeader Builder 模拟了地图/位置信息头部,使用与推荐页面 Banner 相同的红橙渐变配色方案,保持了视觉一致性。

头部展示当前定位位置和附近餐厅总数,黄色文字 #FFD54F 在红色渐变背景上具有极佳的可读性。虽然页面名称暗示了地图功能,但示例中仅用一个渐变色块头部来暗示位置信息,这种简化在原型开发中非常常见。

8.2 附近餐厅卡片

  @Builder
  nearbyCard(item: RestaurantItem) {
    Row() {
      Column() {
        Text(CUISINE_CONFIG[item.cuisine].icon)
          .fontSize(24)
      }
      .width(48).height(48)
      .justifyContent(FlexAlign.Center)
      .alignItems(HorizontalAlign.Center)
      .backgroundColor(CUISINE_CONFIG[item.cuisine].bg)
      .borderRadius(12)

      Column() {
        Text(item.name)
          .fontSize(14).fontColor('#333333').fontWeight(FontWeight.Bold)
        Text(item.address)
          .fontSize(10).fontColor('#AAAAAA')
          .margin({ top: 2 })
        Row() {
          Text('★')
            .fontSize(10).fontColor('#FFD54F')
          Text(item.rating.toFixed(1))
            .fontSize(10).fontColor('#FF9800')
            .margin({ left: 2 })
          Text('•')
            .fontSize(10).fontColor('#CCCCCC')
            .margin({ left: 4 })
          Text(`¥${item.avgPrice}/人`)
            .fontSize(10).fontColor('#999999')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
      }
      .layoutWeight(1)
      .margin({ left: 12 })
      .alignItems(HorizontalAlign.Start)

      Column() {
        Text(`${item.distance}km`)
          .fontSize(13).fontColor('#E53935').fontWeight(FontWeight.Bold)
        Text('步行')
          .fontSize(9).fontColor('#AAAAAA')
          .margin({ top: 2 })
        Text(`${Math.ceil(item.distance * 12)}min`)
          .fontSize(9).fontColor('#FF9800')
          .margin({ top: 2 })
      }
      .alignItems(HorizontalAlign.End)
    }
    .width('100%')
    .padding(12)
    .backgroundColor('#FFFFFF')
    .borderRadius(14)
    .shadow({ radius: 3, color: '#12000000', offsetY: 1 })
    .margin({ bottom: 8 })
  }

技术解析:

nearbyCard 是附近页面专用的卡片布局,采用了典型的水平三栏结构:左侧图标、中间信息、右侧距离。

左侧图标区:使用 48x48 的圆角矩形,背景色为菜系对应的半透明色(CUISINE_CONFIG[item.cuisine].bg),中央放置 24 号字的大尺寸 Emoji 图标。这种大尺寸图标在列表中具有很强的识别性,用户可以快速通过图标颜色和大小区分不同菜系。

中间信息区:包含餐厅名称、地址和评分价格行。评分价格行使用了 • 圆点作为分隔符,这是一种比竖线更柔和的信息分隔方式,在移动端设计中越来越流行。

右侧距离区:是附近页面最核心的信息展示区。除了显示直线距离(${item.distance}km)外,还估算并显示了步行时间(${Math.ceil(item.distance * 12)}min)。这里的 * 12 是一个简化的步行速度换算系数(约每小时 5 公里),Math.ceil 向上取整保证了估算时间总是为整数分钟,避免了小数带来的阅读负担。距离信息使用红色加粗字体突出显示,步行时间使用橙色作为次要信息,"步行"标签使用灰色表明其作为说明文字的定位。alignItems(HorizontalAlign.End) 让右侧信息右对齐,与中间区域的左对齐形成对比,创造了视觉上的平衡感。

8.3 附近页面构建

  build() {
    Stack() {
      Column() {
        Row() {
          Text('📍 附近美食')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text('🗺️')
            .fontSize(20).fontColor('#666666')
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        this.mapHeader()

        Scroll() {
          Column() {
            ForEach(mockRestaurants.sort((a: RestaurantItem, b: RestaurantItem) => a.distance - b.distance), (item: RestaurantItem) => {
              this.nearbyCard(item)
            })
          }
          .padding({ left: 16, right: 16, bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')
    }
    .width('100%').height('100%')
  }
}

技术解析:

附近页面的核心逻辑在于列表的排序。mockRestaurants.sort((a, b) => a.distance - b.distance) 按照 distance 属性升序排列餐厅,确保距离用户最近的餐厅显示在最前面。这是地理位置类应用最基本也最重要的排序逻辑。

ForEach 直接遍历排序后的数组渲染卡片,实现了真正意义上的"由近及远"展示。在真实应用中,这种排序通常由后端根据用户经纬度计算完成,前端只需负责展示;但在纯前端示例中,使用 Array.prototype.sort 是合理的模拟方案。

页面没有条件渲染的弹窗(虽然声明了 showAddModal 和 showDeleteModal 状态,但 build 方法中未使用),这在示例代码中是正常的——可能为后续功能预留了状态,或者仅作为占位。


九、排行页面:数据可视化与榜单展示

9.1 菜系分布条形图 Builder

@Component
struct RankingContent {
  @State selectedCuisine: string = '全部'

  @Builder
  cuisineBar(label: string, count: number, color: string) {
    Column() {
      Row() {
        Text(label)
          .fontSize(11).fontColor('#666666')
        Column().layoutWeight(1)
        Text(`${count}家`)
          .fontSize(11).fontColor(color).fontWeight(FontWeight.Bold)
      }
      .width('100%')

      Row() {
        Column()
          .layoutWeight(Math.max(count, 1))
          .height(8)
          .backgroundColor(color)
          .borderRadius(4)
        Column()
          .layoutWeight(Math.max(16 - count, 1))
          .height(8)
          .backgroundColor('#F0F0F0')
          .borderRadius(4)
      }
      .width('100%')
      .margin({ top: 6 })
    }
    .width('100%')
    .margin({ bottom: 8 })
  }

技术解析:

RankingContent 是排行页面的核心组件。它声明了一个 selectedCuisine 状态,虽然在本示例中并未实际使用(没有基于它的条件渲染或筛选逻辑),但这为后续实现"按菜系筛选排行"功能预留了扩展点。

cuisineBar Builder 实现了一个水平条形图(Bar Chart)组件,用于可视化展示各菜系的餐厅数量分布。这是一种轻量级的数据可视化方案,完全使用 ArkTS 的布局组件实现,无需引入图表库。

条形图由两部分组成:

  1. 标签行:左侧显示菜系名称,右侧显示具体数量(如"中餐"、“3家”)。Column().layoutWeight(1) 将数量文字推到最右侧。
  2. 条形行:使用 Row 包裹两个 Column,通过 layoutWeight 分配宽度比例。左侧彩色柱子的权重为 Math.max(count, 1),右侧灰色背景柱子的权重为 Math.max(16 - count, 1)。这里的 16 是餐厅总数,两个权重之和始终为 16,因此彩色部分的长度严格正比于该菜系的餐厅数量。

Math.max(count, 1) 的防御性处理确保即使 count 为 0,条形图仍然有最小可见宽度(1/16),避免了完全消失的极端情况。.borderRadius(4) 配合 .height(8) 创造了圆角胶囊形状的条形,视觉上非常精致。

9.2 排行卡片 Builder

  @Builder
  rankCard(item: RestaurantItem, rank: number) {
    Row() {
      Column() {
        Text(rank === 1 ? '🥇' : rank === 2 ? '🥈' : rank === 3 ? '🥉' : `${rank}`)
          .fontSize(rank <= 3 ? 28 : 16)
          .fontColor(rank <= 3 ? '#FFD54F' : '#999999')
          .fontWeight(FontWeight.Bold)
      }
      .width(40)
      .alignItems(HorizontalAlign.Center)

      Column()
        .width(48).height(48)
        .borderRadius(10)
        .backgroundColor(item.imageColor)
        .margin({ left: 8 })

      Column() {
        Text(item.name)
          .fontSize(14).fontColor('#333333').fontWeight(FontWeight.Bold)
        Row() {
          Text('★'.repeat(Math.floor(item.rating)))
            .fontSize(10).fontColor('#FFD54F')
          Text(item.rating.toFixed(1))
            .fontSize(10).fontColor('#FF9800')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
        Text(`${item.reviewCount}条评价 · ¥${item.avgPrice}/人`)
          .fontSize(10).fontColor('#AAAAAA')
          .margin({ top: 2 })
      }
      .layoutWeight(1)
      .margin({ left: 12 })
      .alignItems(HorizontalAlign.Start)

      Column() {
        Row() {
          Column()
            .width(Math.floor(item.rating / 5 * 60))
            .height(6)
            .backgroundColor('#FFD54F')
            .borderRadius(3)
          Column()
            .width(60 - Math.floor(item.rating / 5 * 60))
            .height(6)
            .backgroundColor('#FFF3E0')
            .borderRadius(3)
        }
        Text('评分')
          .fontSize(9).fontColor('#AAAAAA')
          .margin({ top: 4 })
      }
      .alignItems(HorizontalAlign.End)
    }
    .width('100%')
    .padding(12)
    .backgroundColor('#FFFFFF')
    .borderRadius(14)
    .shadow({ radius: 3, color: '#12000000', offsetY: 1 })
    .margin({ bottom: 8 })
  }

技术解析:

rankCard 是排行页面的核心展示组件,采用了水平四栏布局:排名、图片、信息、评分条。

排名区域:使用嵌套三元表达式为前三名分配金银铜奖牌 Emoji(🥇、🥈、🥉),其他名次显示数字。前三名使用 28 号大字体和金黄色,其他名次使用 16 号常规字体和灰色,这种差异化的视觉处理让前三名在列表中脱颖而出。

评分条区域:这是一个迷你的横向进度条,总宽度固定为 60,其中彩色部分长度为 Math.floor(item.rating / 5 * 60),即评分占满分 5 分的比例乘以总宽度。例如 4.8 分的餐厅,彩色部分宽度约为 57,灰色背景约为 3,几乎填满整个进度条。这种微型的可视化元素在有限的空间内直观传达了评分的高低差异,是非常精妙的 UX 设计。

9.3 排行页面构建

  build() {
    Column() {
      Row() {
        Text('🏆 美食排行')
          .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
        Column().layoutWeight(1)
        Text('📊')
          .fontSize(20).fontColor('#666666')
      }
      .width('100%')
      .padding({ left: 16, right: 16, top: 12, bottom: 8 })

      Scroll() {
        Column() {
          // Cuisine distribution
          Column() {
            Text('菜系分布')
              .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
              .alignSelf(ItemAlign.Start)
              .margin({ bottom: 12 })

            this.cuisineBar('中餐', getCuisineCount('中餐'), '#E53935')
            this.cuisineBar('日料', getCuisineCount('日料'), '#FF6F00')
            this.cuisineBar('西餐', getCuisineCount('西餐'), '#7B1FA2')
            this.cuisineBar('韩餐', getCuisineCount('韩餐'), '#D81B60')
            this.cuisineBar('火锅', getCuisineCount('火锅'), '#FF5722')
            this.cuisineBar('烧烤', getCuisineCount('烧烤'), '#BF360C')
          }
          .width('100%')
          .padding(16)
          .backgroundColor('#FFFFFF')
          .borderRadius(16)
          .shadow({ radius: 4, color: '#15000000', offsetY: 2 })
          .margin({ left: 16, right: 16, bottom: 12 })

          Text('Top 10 餐厅')
            .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
            .alignSelf(ItemAlign.Start)
            .margin({ left: 16, bottom: 8 })

          this.rankCard(mockRestaurants[0], 1)
          this.rankCard(mockRestaurants[1], 2)
          // ... Top 10
        }
        .padding({ bottom: 16 })
      }
      .layoutWeight(1)
      .scrollBar(BarState.Off)
    }
    .width('100%').height('100%')
  }
}

技术解析:

排行页面的布局清晰分为两大部分:菜系分布统计卡片和 Top 10 榜单。

菜系分布卡片:使用白色背景、16 圆角的卡片容器包裹,内部标题左对齐,下方依次调用六次 cuisineBar Builder,分别传入六大菜系的统计数据和对应颜色。这里直接硬编码了六种菜系的调用,而非使用 ForEach 循环遍历配置对象,虽然代码稍长,但保证了颜色与菜系的精确对应关系,且每种菜系的展示顺序可控。

Top 10 榜单:标题同样左对齐,下方依次渲染 10 个 rankCard。在真实应用中,这里应当先对餐厅数组按评分或评价数排序,然后取前 10 条动态渲染。示例中直接硬编码了前 10 条 Mock 数据,简化了排序逻辑。


十、个人中心页面:复合信息架构

10.1 状态定义与评价卡片

@Component
struct ProfileContent {
  @State showAddModal: boolean = false
  @State showEditModal: boolean = false
  @State showDeleteModal: boolean = false

技术解析:

个人中心页面声明了三个弹窗状态,分别控制写新评价、编辑评价和删除评价的弹窗。这是五个页面中弹窗类型最多的,因为个人中心承载了用户对评价进行完整 CRUD(增删改查)操作的功能。

10.2 评价卡片 Builder

  @Builder
  reviewCard(review: ReviewItem) {
    Column() {
      Row() {
        Column()
          .width(40).height(40)
          .borderRadius(20)
          .backgroundColor(CUISINE_CONFIG[review.cuisine].bg)
        Column() {
          Text(review.restaurantName)
            .fontSize(14).fontColor('#333333').fontWeight(FontWeight.Bold)
          Row() {
            Text('★'.repeat(review.rating))
              .fontSize(10).fontColor('#FFD54F')
            Text('☆'.repeat(5 - review.rating))
              .fontSize(10).fontColor('#CCCCCC')
          }
          .margin({ top: 2 })
        }
        .margin({ left: 10 })
        .layoutWeight(1)
        .alignItems(HorizontalAlign.Start)
        Text(review.date)
          .fontSize(10).fontColor('#AAAAAA')
      }
      .width('100%')

      Text(review.content)
        .fontSize(12).fontColor('#666666')
        .margin({ top: 8 })
        .alignSelf(ItemAlign.Start)

      Row() {
        Text('👍 12')
          .fontSize(10).fontColor('#999999')
          .margin({ right: 12 })
        Text('💬 3')
          .fontSize(10).fontColor('#999999')
        Column().layoutWeight(1)
        Text('✏️')
          .fontSize(14).fontColor('#FF6F00')
          .margin({ right: 8 })
          .onClick(() => { this.showEditModal = true })
        Text('🗑️')
          .fontSize(14).fontColor('#E53935')
          .onClick(() => { this.showDeleteModal = true })
      }
      .width('100%')
      .margin({ top: 8 })
    }
    .width('100%')
    .padding(14)
    .backgroundColor('#FFFFFF')
    .borderRadius(14)
    .shadow({ radius: 3, color: '#12000000', offsetY: 1 })
    .margin({ bottom: 8 })
  }

技术解析:

reviewCard 是个人中心页面中展示单条评价的组件。其结构与前面的餐厅卡片有所不同,更侧重于社交化信息的展示。

头部区域:左侧是 40x40 的圆形色块(borderRadius(20)),使用菜系对应的半透明背景色,作为餐厅头像的抽象表示。右侧上方是餐厅名称和星级评分,评分展示采用了实心星 + 空心星的组合('★'.repeat(review.rating) 和 '☆'.repeat(5 - review.rating)),这比单纯显示数字更具可读性。最右侧是评价日期,使用灰色小字。

内容区域:评价正文左对齐,12 号字的深灰色文字,保证了长文本的可读性。

底部操作栏:左侧是点赞数和评论数的模拟数据(硬编码的"👍 12"和"💬 3"),右侧是编辑和删除按钮,分别使用橙色铅笔和红色垃圾桶 Emoji。Column().layoutWeight(1) 将操作按钮推到最右侧。两个按钮的点击事件分别打开对应的弹窗。

10.3 设置项 Builder

  @Builder
  settingItem(setting: SettingMeta) {
    Row() {
      Text(setting.icon)
        .fontSize(20).margin({ right: 12 })
      Column() {
        Text(setting.title)
          .fontSize(14).fontColor('#333333')
        Text(setting.subtitle)
          .fontSize(10).fontColor('#AAAAAA')
          .margin({ top: 2 })
      }
      .alignItems(HorizontalAlign.Start)
      .layoutWeight(1)
      Text('›')
        .fontSize(18).fontColor('#CCCCCC')
    }
    .width('100%')
    .padding(14)
    .backgroundColor('#FFFFFF')
    .borderRadius(12)
    .margin({ bottom: 6 })
  }

技术解析:

settingItem 是实现设置列表的标准列表项组件。采用水平三栏布局:左侧图标、中间标题和副标题、右侧右箭头。

中间区域使用 Column 垂直排列标题和副标题,并通过 layoutWeight(1) 占据中间所有剩余空间,将右箭头推到最右侧。alignItems(HorizontalAlign.Start) 确保文字左对齐。右箭头使用 › 字符(Unicode 右单书名号),这是移动端表示"可点击进入下一级页面"的经典符号,比使用图片箭头更轻量。

每条设置项底部有 6 的外边距,形成列表项之间的间隙。白色背景配合 12 圆角,与页面背景形成微妙的层次感。

10.4 个人中心页面构建

  build() {
    Stack() {
      Column() {
        Row() {
          Text('👤 我的')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text('✍️')
            .fontSize(20).fontColor('#E53935')
            .padding({ left: 8, right: 8 })
            .backgroundColor('#33E53935')
            .borderRadius(16)
            .onClick(() => { this.showAddModal = true })
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        Scroll() {
          Column() {
            // Profile Card
            Column() {
              Row() {
                Column()
                  .width(60).height(60)
                  .borderRadius(30)
                  .backgroundColor('#E53935')
                  .justifyContent(FlexAlign.Center)
                  .alignItems(HorizontalAlign.Center)
                Column() {
                  Text('美食家小明')
                    .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
                  Text('美食探索者 · Lv.5')
                    .fontSize(11).fontColor('#999999')
                    .margin({ top: 4 })
                }
                .margin({ left: 16 }).layoutWeight(1)
                .alignItems(HorizontalAlign.Start)
              }
              .width('100%')

              Row() {
                Column() {
                  Text(`${mockReviews.length}`)
                    .fontSize(18).fontColor('#E53935').fontWeight(FontWeight.Bold)
                  Text('评价')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
                Column() {
                  Text(`${getFavoritedCount()}`)
                    .fontSize(18).fontColor('#FF6F00').fontWeight(FontWeight.Bold)
                  Text('收藏')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
                Column() {
                  Text(`${mockRestaurants.length}`)
                    .fontSize(18).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
                  Text('足迹')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
                Column() {
                  Text('128')
                    .fontSize(18).fontColor('#4CAF50').fontWeight(FontWeight.Bold)
                  Text('粉丝')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
              }
              .width('100%')
              .padding({ top: 16, bottom: 8 })
            }
            .width('100%')
            .padding(16)
            .backgroundColor('#FFFFFF')
            .borderRadius(20)
            .shadow({ radius: 8, color: '#22000000', offsetY: 3 })
            .margin({ left: 16, right: 16, bottom: 12 })
            .linearGradient({
              angle: 135,
              colors: [['#FFFFFF', 0.0], ['#FFF8E1', 1.0]]
            })

            Text('我的评价')
              .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
              .alignSelf(ItemAlign.Start)
              .margin({ left: 16, bottom: 8 })

            this.reviewCard(mockReviews[0])
            this.reviewCard(mockReviews[1])
            // ... 共8条

            Text('更多设置')
              .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
              .alignSelf(ItemAlign.Start)
              .margin({ left: 16, top: 8, bottom: 8 })

            ForEach(PROFILE_SETTINGS, (setting: SettingMeta) => {
              this.settingItem(setting)
            })
          }
          .padding({ bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')

      if (this.showAddModal) {
        this.addReviewModal()
      }
      if (this.showEditModal) {
        this.editReviewModal()
      }
      if (this.showDeleteModal) {
        this.deleteReviewModal()
      }
    }
    .width('100%').height('100%')
  }
}

技术解析:

个人中心页面的布局最为复杂,包含三个主要区块:个人信息卡、评价列表和设置列表。

个人信息卡:

  • 顶部是用户头像(60x60 红色圆形)和用户信息(昵称"美食家小明"、等级"美食探索者 · Lv.5")。
  • 下方是四列统计数据:评价数、收藏数、足迹数和粉丝数。每个数据使用不同的主题色(红、橙、黄、绿),既区分了数据类型,又形成了彩虹般的视觉丰富度。layoutWeight(1) 让四列均分宽度,保证了布局的对称性。
  • 整个卡片使用了从白色到米黄色的微弱渐变,配合阴影效果,让它从页面背景中微微浮起。

我的评价区:标题左对齐,下方依次渲染 8 条评价卡片。与前面的硬编码列表类似,实际应用中应当使用 ForEach 动态渲染。

更多设置区:标题左对齐,下方使用 ForEach(PROFILE_SETTINGS, ...) 遍历设置数组,动态渲染所有设置项。这是个人中心页面中唯一使用 ForEach 的地方,展示了数据驱动列表的优势——只需修改 PROFILE_SETTINGS 数组,即可增删设置项。

弹窗的条件渲染同样位于 Stack 中,覆盖在所有内容之上。


十一、各模块技术点横向对比总结

模块名称核心功能状态管理要点关键组件与 Builder设计模式数据操作
推荐 (Recommend)餐厅浏览、分类筛选、写评价、删除评价@State activeCategory 驱动分类筛选;showAddModal/showDeleteModal 控制弹窗;selectedRestaurant 记录选中项bannerCard(渐变 Banner)、categoryChip(胶囊标签)、restaurantCard(信息密集列表卡片)、modalOverlay(公共遮罩)、addReviewModal/deleteReviewModal(条件渲染弹窗)条件渲染 + 回调传参;配置化驱动视觉(CUISINE_CONFIG、PRICE_LEVEL_CONFIG)遍历 mockRestaurants 硬编码渲染;isFavorited 直接修改触发 @Observed 响应
收藏 (Favorite)收藏餐厅网格展示、编辑评价、取消收藏showEditModal/showDeleteModal 控制弹窗;selectedItem 记录当前收藏项favoriteGridCard(垂直紧凑网格卡片)、editReviewModal/deleteReviewModal(编辑/确认弹窗)两列网格布局(Row + Column 模拟);Builder 复用与差异化(保存按钮用橙色区分编辑场景)硬编码选取部分餐厅数据模拟收藏列表;未实现真实 isFavorited 筛选
附近 (Nearby)基于地理位置的餐厅排序与展示状态极简,仅预留弹窗状态未使用mapHeader(位置信息头部)、nearbyCard(三栏水平卡片:图标/信息/距离)数据排序驱动展示(sort((a,b) => a.distance - b.distance));步行时间估算(Math.ceil(distance * 12))对 mockRestaurants 按 distance 升序排序后 ForEach 渲染
排行 (Ranking)菜系分布统计、Top 10 榜单selectedCuisine 预留但未实际使用cuisineBar(layoutWeight 比例条形图)、rankCard(排名卡片,含金银铜奖牌和迷你评分进度条)轻量级数据可视化(纯布局组件模拟条形图);排名视觉差异化(前三名大字体+奖牌)调用 getCuisineCount() 统计各菜系数量;硬编码 Top 10 渲染
我的 (Profile)个人信息展示、评价 CRUD、设置列表三个弹窗状态完整覆盖增删改;无列表筛选状态reviewCard(社交化评价卡片,含点赞/评论/编辑/删除操作栏)、settingItem(标准设置列表项)复合信息架构(个人信息卡 + 评价列表 + 设置列表);ForEach 数据驱动设置列表读取 mockReviews.length、getFavoritedCount()、mockRestaurants.length 渲染统计;遍历 PROFILE_SETTINGS 动态渲染设置

安装DevEco Studio程序

在这里插入图片描述
选择目标安装目录:

在这里插入图片描述
设置环境变量,但是需要重启一下:

在这里插入图片描述
新建一个空白模板:

在这里插入图片描述
设置API为24的模板项目:
在这里插入图片描述
初始化项目,自动下载相关依赖:

在这里插入图片描述


完整代码:

// ============================================================
// 美食探店 Food Discovery - 98.ets
// ============================================================

// --- Interfaces ---
interface CuisineMeta {
  label: string
  icon: string
  color: string
  bg: string
}

interface PriceLevelMeta {
  label: string
  symbol: string
  color: string
}

interface ReviewItem {
  restaurantName: string
  rating: number
  content: string
  date: string
  cuisine: string
}

interface SettingMeta {
  icon: string
  title: string
  subtitle: string
  color: string
}

// --- Data Model ---
@Observed
export class RestaurantItem {
  id: number = 0
  name: string = ''
  cuisine: string = ''
  rating: number = 0
  priceLevel: number = 1
  address: string = ''
  tags: string[] = []
  reviewCount: number = 0
  avgPrice: number = 0
  isFavorited: boolean = false
  distance: number = 0
  imageColor: string = ''
  description: string = ''

  constructor(id: number, name: string, cuisine: string, rating: number,
    priceLevel: number, address: string, tags: string[], reviewCount: number,
    avgPrice: number, isFavorited: boolean, distance: number,
    imageColor: string, description: string) {
    this.id = id
    this.name = name
    this.cuisine = cuisine
    this.rating = rating
    this.priceLevel = priceLevel
    this.address = address
    this.tags = tags
    this.reviewCount = reviewCount
    this.avgPrice = avgPrice
    this.isFavorited = isFavorited
    this.distance = distance
    this.imageColor = imageColor
    this.description = description
  }
}

// --- Config ---
const CUISINE_CONFIG: Record<string, CuisineMeta> = {
  '中餐': { label: '中餐', icon: '🥘', color: '#E53935', bg: '#33E53935' },
  '日料': { label: '日料', icon: '🍣', color: '#FF6F00', bg: '#33FF6F00' },
  '西餐': { label: '西餐', icon: '🥩', color: '#7B1FA2', bg: '#337B1FA2' },
  '韩餐': { label: '韩餐', icon: '🍖', color: '#D81B60', bg: '#33D81B60' },
  '火锅': { label: '火锅', icon: '🍲', color: '#FF5722', bg: '#33FF5722' },
  '烧烤': { label: '烧烤', icon: '🔥', color: '#BF360C', bg: '#33BF360C' }
}

const PRICE_LEVEL_CONFIG: Record<number, PriceLevelMeta> = {
  1: { label: '便宜', symbol: '¥', color: '#4CAF50' },
  2: { label: '适中', symbol: '¥¥', color: '#FF9800' },
  3: { label: '较贵', symbol: '¥¥¥', color: '#FF5722' },
  4: { label: '昂贵', symbol: '¥¥¥¥', color: '#D81B60' }
}

const CATEGORIES: string[] = ['全部', '中餐', '日料', '西餐', '韩餐', '火锅', '烧烤']

// --- Mock Data ---
const mockRestaurants: RestaurantItem[] = [
  new RestaurantItem(1, '老王川菜馆', '中餐', 4.8, 2, '南京路步行街128号', ['正宗', '辣味', '性价比'], 356, 65, true, 0.5, '#E53935', '正宗川菜,麻辣鲜香'),
  new RestaurantItem(2, '樱花日料店', '日料', 4.9, 4, '淮海中路456号', ['新鲜', '精致', '包间'], 892, 280, true, 1.2, '#FF6F00', '怀石料理,匠心之作'),
  new RestaurantItem(3, '麦迪逊牛排', '西餐', 4.7, 4, '外滩33号', ['约会', '高端', '景观'], 1245, 580, false, 2.8, '#7B1FA2', '顶级牛排,绝佳江景'),
  new RestaurantItem(4, '首尔烤肉屋', '韩餐', 4.5, 2, '虹泉路88号', ['韩式', '小菜', '聚会'], 567, 98, true, 0.8, '#D81B60', '地道韩式烤肉'),
  new RestaurantItem(5, '蜀地火锅城', '火锅', 4.6, 2, '四川北路33号', ['辣', '新鲜', '排队'], 1089, 120, true, 1.5, '#FF5722', '正宗重庆火锅'),
  new RestaurantItem(6, '东北烧烤大院', '烧烤', 4.4, 1, '长寿路120号', ['大排档', '啤酒', '热闹'], 423, 55, false, 0.3, '#BF360C', '地摊烧烤,实惠美味'),
  new RestaurantItem(7, '小南国', '中餐', 4.6, 3, '南京西路111号', ['本帮', '精致', '商务'], 678, 220, true, 2.1, '#E53935', '上海本帮菜代表'),
  new RestaurantItem(8, '一兰拉面', '日料', 4.7, 2, '静安寺路66号', ['拉面', '一人食', '快速'], 934, 58, false, 0.6, '#FF6F00', '正宗豚骨拉面'),
  new RestaurantItem(9, '蓝蛙餐厅', '西餐', 4.3, 3, '新天地2号', ['汉堡', '周末', '休闲'], 445, 150, false, 1.8, '#7B1FA2', '美式汉堡与轻食'),
  new RestaurantItem(10, '春川铁板鸡', '韩餐', 4.5, 2, '韩国街12号', ['铁板', '芝士', '辣'], 312, 75, true, 1.0, '#D81B60', '韩式铁板鸡配芝士'),
  new RestaurantItem(11, '海底捞火锅', '火锅', 4.8, 3, '南京东路1号', ['服务好', '连锁', '24h'], 2345, 180, true, 3.2, '#FF5722', '服务至上的火锅连锁'),
  new RestaurantItem(12, '羊肉串大排档', '烧烤', 4.2, 1, '城隍庙50号', ['羊肉', '夜宵', '实惠'], 234, 38, false, 0.4, '#BF360C', '深夜烧烤好去处'),
  new RestaurantItem(13, '外婆家', '中餐', 4.3, 1, '陆家嘴100号', ['家常', '排队', '便宜'], 1567, 45, true, 2.5, '#E53935', '家常菜性价比之王'),
  new RestaurantItem(14, '寿司之乱', '日料', 4.4, 3, '古北新区8号', ['手握', 'omakase', '高端'], 456, 350, false, 4.0, '#FF6F00', '板前手握寿司'),
  new RestaurantItem(15, '法式铁板烧', '西餐', 4.6, 4, '外滩源5号', ['铁板', '法式', '约会'], 567, 680, true, 3.8, '#7B1FA2', '法式铁板烧料理'),
  new RestaurantItem(16, '部队锅之家', '韩餐', 4.1, 1, '龙柏地区22号', ['部队锅', '泡菜', '暖胃'], 189, 48, false, 0.9, '#D81B60', '韩式部队锅专门店')
]

const mockReviews: ReviewItem[] = [
  { restaurantName: '老王川菜馆', rating: 5, content: '水煮鱼太好吃了,麻辣鲜香!', date: '07-20', cuisine: '中餐' },
  { restaurantName: '樱花日料店', rating: 5, content: '三文鱼非常新鲜,环境也好', date: '07-18', cuisine: '日料' },
  { restaurantName: '蜀地火锅城', rating: 4, content: '火锅很正宗,就是排队太久', date: '07-15', cuisine: '火锅' },
  { restaurantName: '首尔烤肉屋', rating: 4, content: '烤肉味道不错,小菜免费续', date: '07-12', cuisine: '韩餐' },
  { restaurantName: '海底捞火锅', rating: 5, content: '服务真的太好了,美甲都做了', date: '07-10', cuisine: '火锅' },
  { restaurantName: '小南国', rating: 4, content: '本帮菜很正宗,环境优雅', date: '07-08', cuisine: '中餐' },
  { restaurantName: '一兰拉面', rating: 4, content: '汤头浓郁,就是份量少了点', date: '07-05', cuisine: '日料' },
  { restaurantName: '法式铁板烧', rating: 5, content: '约会绝佳选择,氛围浪漫', date: '07-02', cuisine: '西餐' }
]

const PROFILE_SETTINGS: SettingMeta[] = [
  { icon: '📝', title: '我的评价', subtitle: '共8条评价', color: '#E53935' },
  { icon: '❤️', title: '我的收藏', subtitle: '共8家餐厅', color: '#FF6F00' },
  { icon: '🎟️', title: '优惠券', subtitle: '3张可用', color: '#FFD54F' },
  { icon: '📍', title: '我的足迹', subtitle: '已探索15家店', color: '#4CAF50' },
  { icon: '🌙', title: '夜间模式', subtitle: '跟随系统', color: '#7B1FA2' },
  { icon: '🔔', title: '消息通知', subtitle: '已开启', color: '#D81B60' },
  { icon: '❓', title: '关于我们', subtitle: 'v3.1.0', color: '#9E9E9E' }
]

// --- Utility Functions ---
function getAverageRating(): number {
  return Math.round(mockRestaurants.reduce((sum: number, r: RestaurantItem) => sum + r.rating, 0) / mockRestaurants.length * 10) / 10
}

function getFavoritedCount(): number {
  return mockRestaurants.filter((r: RestaurantItem) => r.isFavorited).length
}

function getAveragePrice(): number {
  return Math.round(mockRestaurants.reduce((sum: number, r: RestaurantItem) => sum + r.avgPrice, 0) / mockRestaurants.length)
}

function getCuisineCount(cuisine: string): number {
  return mockRestaurants.filter((r: RestaurantItem) => r.cuisine === cuisine).length
}

// --- Enum ---
enum AppTab {
  RECOMMEND = 0,
  FAVORITE = 1,
  NEARBY = 2,
  RANKING = 3,
  PROFILE = 4
}

// ============================================================
// Main Entry Component
// ============================================================
@Entry
@Component
struct MainApp {
  @State activeTab: AppTab = AppTab.RECOMMEND

  @Builder
  contentArea() {
    Column() {
      if (this.activeTab === AppTab.RECOMMEND) {
        RecommendContent()
      } else if (this.activeTab === AppTab.FAVORITE) {
        FavoriteContent()
      } else if (this.activeTab === AppTab.NEARBY) {
        NearbyContent()
      } else if (this.activeTab === AppTab.RANKING) {
        RankingContent()
      } else if (this.activeTab === AppTab.PROFILE) {
        ProfileContent()
      }
    }
    .layoutWeight(1)
  }

  @Builder
  bottomTabItem(icon: string, label: string, tab: AppTab) {
    Column() {
      Text(icon)
        .fontSize(20)
        .opacity(this.activeTab === tab ? 1.0 : 0.45)
      Text(label)
        .fontSize(9)
        .fontColor(this.activeTab === tab ? '#E53935' : '#999999')
        .margin({ top: 1 })
      if (this.activeTab === tab) {
        Column()
          .width(18).height(3)
          .backgroundColor('#E53935')
          .borderRadius(2)
          .margin({ top: 2 })
      }
    }
    .layoutWeight(1)
    .alignItems(HorizontalAlign.Center)
    .padding({ top: 5, bottom: 5 })
    .onClick(() => { this.activeTab = tab })
  }

  build() {
    Column() {
      this.contentArea()
      Row() {
        this.bottomTabItem('🔥', '推荐', AppTab.RECOMMEND)
        this.bottomTabItem('⭐', '收藏', AppTab.FAVORITE)
        this.bottomTabItem('📍', '附近', AppTab.NEARBY)
        this.bottomTabItem('🏆', '排行', AppTab.RANKING)
        this.bottomTabItem('👤', '我的', AppTab.PROFILE)
      }
      .width('100%')
      .backgroundColor('#FFFFFF')
      .padding({ top: 4, bottom: 6 })
      .shadow({ radius: 8, color: '#1A000000', offsetY: -2 })
    }
    .width('100%').height('100%')
    .backgroundColor('#FFF8E1')
  }
}

// ============================================================
// Tab 1: Recommend Content (推荐)
// ============================================================
@Component
struct RecommendContent {
  @State activeCategory: string = '全部'
  @State showAddModal: boolean = false
  @State showDeleteModal: boolean = false
  @State selectedRestaurant: RestaurantItem | null = null

  @Builder
  bannerCard() {
    Column() {
      Text('🍜 发现美食')
        .fontSize(24).fontColor('#FFFFFF').fontWeight(FontWeight.Bold)
      Text('探索城市中最好的餐厅')
        .fontSize(12).fontColor('#FFD54F')
        .margin({ top: 4 })
      Row() {
        Column() {
          Text(`${mockRestaurants.length}`)
            .fontSize(20).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
          Text('餐厅')
            .fontSize(9).fontColor('#FFE8B0')
        }
        .alignItems(HorizontalAlign.Center).margin({ right: 20 })
        Column() {
          Text(`${getAverageRating()}`)
            .fontSize(20).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
          Text('平均评分')
            .fontSize(9).fontColor('#FFE8B0')
        }
        .alignItems(HorizontalAlign.Center).margin({ right: 20 })
        Column() {
          Text(`¥${getAveragePrice()}`)
            .fontSize(20).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
          Text('人均')
            .fontSize(9).fontColor('#FFE8B0')
        }
        .alignItems(HorizontalAlign.Center)
      }
      .margin({ top: 12 })
    }
    .width('100%')
    .padding(20)
    .borderRadius(20)
    .linearGradient({
      angle: 135,
      colors: [['#E53935', 0.0], ['#FF6F00', 1.0]]
    })
    .shadow({ radius: 8, color: '#33E53935', offsetY: 4 })
    .margin({ left: 16, right: 16, bottom: 12 })
  }

  @Builder
  categoryChip(cat: string) {
    Text(cat)
      .fontSize(12)
      .fontColor(this.activeCategory === cat ? '#FFFFFF' : '#666666')
      .padding({ left: 14, right: 14, top: 6, bottom: 6 })
      .backgroundColor(this.activeCategory === cat ? CUISINE_CONFIG[cat]?.color ?? '#E53935' : '#FFFFFF')
      .borderRadius(16)
      .shadow(this.activeCategory === cat ? { radius: 4, color: '#33E53935', offsetY: 2 } : { radius: 2, color: '#10000000', offsetY: 1 })
      .onClick(() => { this.activeCategory = cat })
  }

  @Builder
  restaurantCard(item: RestaurantItem) {
    Column() {
      Row() {
        Column()
          .width(60).height(60)
          .borderRadius(12)
          .backgroundColor(item.imageColor)

        Column() {
          Text(item.name)
            .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Bold)
          Row() {
            Text('★'.repeat(Math.floor(item.rating)))
              .fontSize(12).fontColor('#FFD54F')
            Text(item.rating.toFixed(1))
              .fontSize(11).fontColor('#FF9800')
              .margin({ left: 4 })
            Text(`${item.reviewCount}条评价`)
              .fontSize(10).fontColor('#999999')
              .margin({ left: 8 })
          }
          .margin({ top: 4 })
          Row() {
            Text(CUISINE_CONFIG[item.cuisine].icon)
              .fontSize(10)
            Text(CUISINE_CONFIG[item.cuisine].label)
              .fontSize(10).fontColor(CUISINE_CONFIG[item.cuisine].color)
              .margin({ left: 2 })
            Text(PRICE_LEVEL_CONFIG[item.priceLevel].symbol)
              .fontSize(11).fontColor(PRICE_LEVEL_CONFIG[item.priceLevel].color)
              .margin({ left: 8 })
            Text(`人均¥${item.avgPrice}`)
              .fontSize(10).fontColor('#999999')
              .margin({ left: 8 })
          }
          .margin({ top: 4 })
          Text(item.address)
            .fontSize(10).fontColor('#AAAAAA')
            .margin({ top: 4 })
        }
        .layoutWeight(1)
        .margin({ left: 12 })
        .alignItems(HorizontalAlign.Start)

        Column() {
          Text(item.isFavorited ? '❤️' : '🤍')
            .fontSize(20)
            .onClick(() => { item.isFavorited = !item.isFavorited })
          Text(`${item.distance}km`)
            .fontSize(9).fontColor('#E53935')
            .margin({ top: 6 })
            .padding({ left: 4, right: 4, top: 2, bottom: 2 })
            .backgroundColor('#33E53935')
            .borderRadius(4)
        }
        .alignItems(HorizontalAlign.Center)
      }
      .width('100%')

      Row() {
        ForEach(item.tags, (tag: string) => {
          Text(tag)
            .fontSize(9).fontColor('#FF6F00')
            .padding({ left: 6, right: 6, top: 2, bottom: 2 })
            .backgroundColor('#FFF3E0')
            .borderRadius(4)
            .margin({ right: 6 })
        })
        Column().layoutWeight(1)
        Text(item.description)
          .fontSize(9).fontColor('#BBBBBB')
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      }
      .width('100%')
      .margin({ top: 8 })
    }
    .width('100%')
    .padding(14)
    .backgroundColor('#FFFFFF')
    .borderRadius(16)
    .shadow({ radius: 4, color: '#15000000', offsetY: 2 })
    .margin({ bottom: 10 })
    .onClick(() => {
      this.selectedRestaurant = item
      this.showDeleteModal = true
    })
  }

  @Builder
  modalOverlay(onClose: () => void) {
    Column()
      .width('100%').height('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .onClick(onClose)
  }

  @Builder
  addReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showAddModal = false })

      Column() {
        Text('✍️ 写评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 16 })

        Column() {
          Text('餐厅名称')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('老王川菜馆')
            .fontSize(14).fontColor('#333333')
            .padding({ top: 8, bottom: 8, left: 12, right: 12 })
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评分')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('☆').fontSize(28).fontColor('#CCCCCC')
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评价内容')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('这家店味道很正宗,服务也不错...')
            .fontSize(13).fontColor('#AAAAAA')
            .padding(12)
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('标签')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('好吃').fontSize(11).fontColor('#FF6F00')
              .padding({ left: 8, right: 8, top: 4, bottom: 4 })
              .backgroundColor('#FFF3E0').borderRadius(6).margin({ right: 6 })
            Text('实惠').fontSize(11).fontColor('#4CAF50')
              .padding({ left: 8, right: 8, top: 4, bottom: 4 })
              .backgroundColor('#E8F5E9').borderRadius(6).margin({ right: 6 })
            Text('环境好').fontSize(11).fontColor('#7B1FA2')
              .padding({ left: 8, right: 8, top: 4, bottom: 4 })
              .backgroundColor('#F3E5F5').borderRadius(6)
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 16 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showAddModal = false })
          Text('发布评价')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showAddModal = false })
        }
        .width('100%')
      }
      .width('88%')
      .padding(20)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

  @Builder
  deleteReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showDeleteModal = false })

      Column() {
        Text('🗑️')
          .fontSize(40).margin({ bottom: 12 })
        Text('删除评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 8 })
        Text(`确定要删除对「${this.selectedRestaurant?.name ?? ''}」的评价吗?`)
          .fontSize(13).fontColor('#999999')
          .textAlign(TextAlign.Center)
          .margin({ bottom: 20 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showDeleteModal = false })
          Text('删除')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showDeleteModal = false })
        }
        .width('100%')
      }
      .width('72%')
      .padding(24)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

  build() {
    Stack() {
      Column() {
        // Header
        Row() {
          Text('🔥 推荐美食')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text('🔍')
            .fontSize(20).fontColor('#666666')
            .margin({ right: 12 })
          Text('✍️')
            .fontSize(20).fontColor('#E53935')
            .padding({ left: 8, right: 8 })
            .backgroundColor('#33E53935')
            .borderRadius(16)
            .onClick(() => { this.showAddModal = true })
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        // Banner
        this.bannerCard()

        // Category Chips
        Scroll() {
          Row() {
            ForEach(CATEGORIES, (cat: string) => {
              this.categoryChip(cat)
            })
          }
          .padding({ left: 16, right: 16 })
        }
        .scrollable(ScrollDirection.Horizontal)
        .scrollBar(BarState.Off)
        .padding({ bottom: 8 })

        // Restaurant List
        Scroll() {
          Column() {
            this.restaurantCard(mockRestaurants[0])
            this.restaurantCard(mockRestaurants[1])
            this.restaurantCard(mockRestaurants[2])
            this.restaurantCard(mockRestaurants[3])
            this.restaurantCard(mockRestaurants[4])
            this.restaurantCard(mockRestaurants[5])
            this.restaurantCard(mockRestaurants[6])
            this.restaurantCard(mockRestaurants[7])
            this.restaurantCard(mockRestaurants[8])
            this.restaurantCard(mockRestaurants[9])
            this.restaurantCard(mockRestaurants[10])
            this.restaurantCard(mockRestaurants[11])
            this.restaurantCard(mockRestaurants[12])
            this.restaurantCard(mockRestaurants[13])
            this.restaurantCard(mockRestaurants[14])
            this.restaurantCard(mockRestaurants[15])
          }
          .padding({ left: 16, right: 16, bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')

      if (this.showAddModal) {
        this.addReviewModal()
      }
      if (this.showDeleteModal) {
        this.deleteReviewModal()
      }
    }
    .width('100%').height('100%')
  }
}

// ============================================================
// Tab 2: Favorite Content (收藏)
// ============================================================
@Component
struct FavoriteContent {
  @State showEditModal: boolean = false
  @State showDeleteModal: boolean = false
  @State selectedItem: RestaurantItem | null = null

  @Builder
  modalOverlay(onClose: () => void) {
    Column()
      .width('100%').height('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .onClick(onClose)
  }

  @Builder
  editReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showEditModal = false })

      Column() {
        Text('✏️ 编辑评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 16 })

        Column() {
          Text('餐厅')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text(this.selectedItem?.name ?? '')
            .fontSize(14).fontColor('#333333')
            .padding({ top: 8, bottom: 8, left: 12, right: 12 })
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评分')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F')
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评价内容')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('这家店味道很正宗,服务也不错,推荐!')
            .fontSize(13).fontColor('#333333')
            .padding(12)
            .backgroundColor('#FFF8E1')
            .borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 16 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showEditModal = false })
          Text('保存修改')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#FF6F00').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showEditModal = false })
        }
        .width('100%')
      }
      .width('88%')
      .padding(20)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

  @Builder
  deleteReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showDeleteModal = false })

      Column() {
        Text('🗑️')
          .fontSize(40).margin({ bottom: 12 })
        Text('取消收藏')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 8 })
        Text(`确定要取消收藏「${this.selectedItem?.name ?? ''}」吗?`)
          .fontSize(13).fontColor('#999999')
          .textAlign(TextAlign.Center)
          .margin({ bottom: 20 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showDeleteModal = false })
          Text('确定')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showDeleteModal = false })
        }
        .width('100%')
      }
      .width('72%')
      .padding(24)
      .backgroundColor('#FFFFFF')
      .borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }

  @Builder
  favoriteGridCard(item: RestaurantItem) {
    Column() {
      Column()
        .width('100%').height(70)
        .borderRadius(12)
        .backgroundColor(item.imageColor)
        .linearGradient({
          angle: 135,
          colors: [[item.imageColor, 0.0], ['#FFE0B2', 1.0]]
        })

      Column() {
        Text(item.name)
          .fontSize(13).fontColor('#333333').fontWeight(FontWeight.Bold)
        Row() {
          Text('★'.repeat(Math.floor(item.rating)))
            .fontSize(10).fontColor('#FFD54F')
          Text(item.rating.toFixed(1))
            .fontSize(9).fontColor('#FF9800')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
        Row() {
          Text(CUISINE_CONFIG[item.cuisine].icon).fontSize(9)
          Text(CUISINE_CONFIG[item.cuisine].label)
            .fontSize(9).fontColor(CUISINE_CONFIG[item.cuisine].color)
            .margin({ left: 2 })
          Text(PRICE_LEVEL_CONFIG[item.priceLevel].symbol)
            .fontSize(10).fontColor(PRICE_LEVEL_CONFIG[item.priceLevel].color)
            .margin({ left: 6 })
          Text(`¥${item.avgPrice}`)
            .fontSize(9).fontColor('#999999')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
      }
      .width('100%')
      .padding(8)
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .backgroundColor('#FFFFFF')
    .borderRadius(12)
    .shadow({ radius: 4, color: '#15000000', offsetY: 2 })
    .margin({ bottom: 10 })
    .onClick(() => {
      this.selectedItem = item
      this.showEditModal = true
    })
  }

  build() {
    Stack() {
      Column() {
        Row() {
          Text('⭐ 我的收藏')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text(`${getFavoritedCount()}家`)
            .fontSize(12).fontColor('#FF9800')
            .padding({ left: 8, right: 8, top: 4, bottom: 4 })
            .backgroundColor('#FFF3E0')
            .borderRadius(10)
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        Scroll() {
          Column() {
            Row() {
              this.favoriteGridCard(mockRestaurants[0])
              this.favoriteGridCard(mockRestaurants[1])
            }
            Row() {
              this.favoriteGridCard(mockRestaurants[3])
              this.favoriteGridCard(mockRestaurants[4])
            }
            Row() {
              this.favoriteGridCard(mockRestaurants[6])
              this.favoriteGridCard(mockRestaurants[9])
            }
            Row() {
              this.favoriteGridCard(mockRestaurants[10])
              this.favoriteGridCard(mockRestaurants[12])
            }
            Row() {
              this.favoriteGridCard(mockRestaurants[14])
              this.favoriteGridCard(mockRestaurants[0])
            }
          }
          .padding({ left: 16, right: 16, bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')

      if (this.showEditModal) {
        this.editReviewModal()
      }
      if (this.showDeleteModal) {
        this.deleteReviewModal()
      }
    }
    .width('100%').height('100%')
  }
}

// ============================================================
// Tab 3: Nearby Content (附近)
// ============================================================
@Component
struct NearbyContent {
  @State showAddModal: boolean = false
  @State showDeleteModal: boolean = false

  @Builder
  modalOverlay(onClose: () => void) {
    Column()
      .width('100%').height('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .onClick(onClose)
  }

  @Builder
  mapHeader() {
    Column() {
      Text('📍 当前位置: 南京路步行街')
        .fontSize(13).fontColor('#FFFFFF')
      Text('发现附近 16 家餐厅')
        .fontSize(11).fontColor('#FFD54F')
        .margin({ top: 4 })
    }
    .width('100%')
    .padding(16)
    .borderRadius(16)
    .linearGradient({
      angle: 135,
      colors: [['#E53935', 0.0], ['#FF6F00', 1.0]]
    })
    .shadow({ radius: 8, color: '#33E53935', offsetY: 3 })
    .margin({ left: 16, right: 16, bottom: 12 })
  }

  @Builder
  nearbyCard(item: RestaurantItem) {
    Row() {
      Column() {
        Text(CUISINE_CONFIG[item.cuisine].icon)
          .fontSize(24)
      }
      .width(48).height(48)
      .justifyContent(FlexAlign.Center)
      .alignItems(HorizontalAlign.Center)
      .backgroundColor(CUISINE_CONFIG[item.cuisine].bg)
      .borderRadius(12)

      Column() {
        Text(item.name)
          .fontSize(14).fontColor('#333333').fontWeight(FontWeight.Bold)
        Text(item.address)
          .fontSize(10).fontColor('#AAAAAA')
          .margin({ top: 2 })
        Row() {
          Text('★')
            .fontSize(10).fontColor('#FFD54F')
          Text(item.rating.toFixed(1))
            .fontSize(10).fontColor('#FF9800')
            .margin({ left: 2 })
          Text('•')
            .fontSize(10).fontColor('#CCCCCC')
            .margin({ left: 4 })
          Text(`¥${item.avgPrice}/人`)
            .fontSize(10).fontColor('#999999')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
      }
      .layoutWeight(1)
      .margin({ left: 12 })
      .alignItems(HorizontalAlign.Start)

      Column() {
        Text(`${item.distance}km`)
          .fontSize(13).fontColor('#E53935').fontWeight(FontWeight.Bold)
        Text('步行')
          .fontSize(9).fontColor('#AAAAAA')
          .margin({ top: 2 })
        Text(`${Math.ceil(item.distance * 12)}min`)
          .fontSize(9).fontColor('#FF9800')
          .margin({ top: 2 })
      }
      .alignItems(HorizontalAlign.End)
    }
    .width('100%')
    .padding(12)
    .backgroundColor('#FFFFFF')
    .borderRadius(14)
    .shadow({ radius: 3, color: '#12000000', offsetY: 1 })
    .margin({ bottom: 8 })
  }

  build() {
    Stack() {
      Column() {
        Row() {
          Text('📍 附近美食')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text('🗺️')
            .fontSize(20).fontColor('#666666')
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        this.mapHeader()

        Scroll() {
          Column() {
            ForEach(mockRestaurants.sort((a: RestaurantItem, b: RestaurantItem) => a.distance - b.distance), (item: RestaurantItem) => {
              this.nearbyCard(item)
            })
          }
          .padding({ left: 16, right: 16, bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')
    }
    .width('100%').height('100%')
  }
}

// ============================================================
// Tab 4: Ranking Content (排行)
// ============================================================
@Component
struct RankingContent {
  @State selectedCuisine: string = '全部'

  @Builder
  cuisineBar(label: string, count: number, color: string) {
    Column() {
      Row() {
        Text(label)
          .fontSize(11).fontColor('#666666')
        Column().layoutWeight(1)
        Text(`${count}家`)
          .fontSize(11).fontColor(color).fontWeight(FontWeight.Bold)
      }
      .width('100%')

      Row() {
        Column()
          .layoutWeight(Math.max(count, 1))
          .height(8)
          .backgroundColor(color)
          .borderRadius(4)
        Column()
          .layoutWeight(Math.max(16 - count, 1))
          .height(8)
          .backgroundColor('#F0F0F0')
          .borderRadius(4)
      }
      .width('100%')
      .margin({ top: 6 })
    }
    .width('100%')
    .margin({ bottom: 8 })
  }

  @Builder
  rankCard(item: RestaurantItem, rank: number) {
    Row() {
      Column() {
        Text(rank === 1 ? '🥇' : rank === 2 ? '🥈' : rank === 3 ? '🥉' : `${rank}`)
          .fontSize(rank <= 3 ? 28 : 16)
          .fontColor(rank <= 3 ? '#FFD54F' : '#999999')
          .fontWeight(FontWeight.Bold)
      }
      .width(40)
      .alignItems(HorizontalAlign.Center)

      Column()
        .width(48).height(48)
        .borderRadius(10)
        .backgroundColor(item.imageColor)
        .margin({ left: 8 })

      Column() {
        Text(item.name)
          .fontSize(14).fontColor('#333333').fontWeight(FontWeight.Bold)
        Row() {
          Text('★'.repeat(Math.floor(item.rating)))
            .fontSize(10).fontColor('#FFD54F')
          Text(item.rating.toFixed(1))
            .fontSize(10).fontColor('#FF9800')
            .margin({ left: 4 })
        }
        .margin({ top: 4 })
        Text(`${item.reviewCount}条评价 · ¥${item.avgPrice}/人`)
          .fontSize(10).fontColor('#AAAAAA')
          .margin({ top: 2 })
      }
      .layoutWeight(1)
      .margin({ left: 12 })
      .alignItems(HorizontalAlign.Start)

      Column() {
        Row() {
          Column()
            .width(Math.floor(item.rating / 5 * 60))
            .height(6)
            .backgroundColor('#FFD54F')
            .borderRadius(3)
          Column()
            .width(60 - Math.floor(item.rating / 5 * 60))
            .height(6)
            .backgroundColor('#FFF3E0')
            .borderRadius(3)
        }
        Text('评分')
          .fontSize(9).fontColor('#AAAAAA')
          .margin({ top: 4 })
      }
      .alignItems(HorizontalAlign.End)
    }
    .width('100%')
    .padding(12)
    .backgroundColor('#FFFFFF')
    .borderRadius(14)
    .shadow({ radius: 3, color: '#12000000', offsetY: 1 })
    .margin({ bottom: 8 })
  }

  build() {
    Column() {
      Row() {
        Text('🏆 美食排行')
          .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
        Column().layoutWeight(1)
        Text('📊')
          .fontSize(20).fontColor('#666666')
      }
      .width('100%')
      .padding({ left: 16, right: 16, top: 12, bottom: 8 })

      Scroll() {
        Column() {
          // Cuisine distribution
          Column() {
            Text('菜系分布')
              .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
              .alignSelf(ItemAlign.Start)
              .margin({ bottom: 12 })

            this.cuisineBar('中餐', getCuisineCount('中餐'), '#E53935')
            this.cuisineBar('日料', getCuisineCount('日料'), '#FF6F00')
            this.cuisineBar('西餐', getCuisineCount('西餐'), '#7B1FA2')
            this.cuisineBar('韩餐', getCuisineCount('韩餐'), '#D81B60')
            this.cuisineBar('火锅', getCuisineCount('火锅'), '#FF5722')
            this.cuisineBar('烧烤', getCuisineCount('烧烤'), '#BF360C')
          }
          .width('100%')
          .padding(16)
          .backgroundColor('#FFFFFF')
          .borderRadius(16)
          .shadow({ radius: 4, color: '#15000000', offsetY: 2 })
          .margin({ left: 16, right: 16, bottom: 12 })

          Text('Top 10 餐厅')
            .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
            .alignSelf(ItemAlign.Start)
            .margin({ left: 16, bottom: 8 })

          this.rankCard(mockRestaurants[0], 1)
          this.rankCard(mockRestaurants[1], 2)
          this.rankCard(mockRestaurants[2], 3)
          this.rankCard(mockRestaurants[3], 4)
          this.rankCard(mockRestaurants[4], 5)
          this.rankCard(mockRestaurants[5], 6)
          this.rankCard(mockRestaurants[6], 7)
          this.rankCard(mockRestaurants[7], 8)
          this.rankCard(mockRestaurants[8], 9)
          this.rankCard(mockRestaurants[9], 10)
        }
        .padding({ bottom: 16 })
      }
      .layoutWeight(1)
      .scrollBar(BarState.Off)
    }
    .width('100%').height('100%')
  }
}

// ============================================================
// Tab 5: Profile Content (我的)
// ============================================================
@Component
struct ProfileContent {
  @State showAddModal: boolean = false
  @State showEditModal: boolean = false
  @State showDeleteModal: boolean = false

  @Builder
  modalOverlay(onClose: () => void) {
    Column()
      .width('100%').height('100%')
      .backgroundColor('rgba(0,0,0,0.5)')
      .onClick(onClose)
  }

  @Builder
  addReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showAddModal = false })

      Column() {
        Text('✍️ 写新评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 16 })

        Column() {
          Text('选择餐厅')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('樱花日料店')
            .fontSize(14).fontColor('#333333')
            .padding({ top: 8, bottom: 8, left: 12, right: 12 })
            .backgroundColor('#FFF8E1').borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评分')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F')
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评价内容')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('分享你的用餐体验...')
            .fontSize(13).fontColor('#AAAAAA')
            .padding(12).backgroundColor('#FFF8E1').borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 16 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showAddModal = false })
          Text('发布')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showAddModal = false })
        }
        .width('100%')
      }
      .width('88%').padding(20)
      .backgroundColor('#FFFFFF').borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
  }

  @Builder
  editReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showEditModal = false })

      Column() {
        Text('✏️ 编辑评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 16 })

        Column() {
          Text('餐厅')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('老王川菜馆')
            .fontSize(14).fontColor('#333333')
            .padding({ top: 8, bottom: 8, left: 12, right: 12 })
            .backgroundColor('#FFF8E1').borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评分')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Row() {
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('★').fontSize(28).fontColor('#FFD54F').margin({ right: 4 })
            Text('☆').fontSize(28).fontColor('#CCCCCC')
          }
          .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 12 })

        Column() {
          Text('评价内容')
            .fontSize(12).fontColor('#999999').alignSelf(ItemAlign.Start)
          Text('水煮鱼太好吃了,麻辣鲜香!')
            .fontSize(13).fontColor('#333333')
            .padding(12).backgroundColor('#FFF8E1').borderRadius(8)
            .margin({ top: 6 })
        }
        .width('100%').margin({ bottom: 16 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showEditModal = false })
          Text('保存')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#FF6F00').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showEditModal = false })
        }
        .width('100%')
      }
      .width('88%').padding(20)
      .backgroundColor('#FFFFFF').borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
  }

  @Builder
  deleteReviewModal() {
    Column() {
      this.modalOverlay(() => { this.showDeleteModal = false })

      Column() {
        Text('🗑️')
          .fontSize(40).margin({ bottom: 12 })
        Text('删除评价')
          .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
          .margin({ bottom: 8 })
        Text('确定要删除这条评价吗?删除后不可恢复。')
          .fontSize(13).fontColor('#999999')
          .textAlign(TextAlign.Center)
          .margin({ bottom: 20 })

        Row() {
          Text('取消')
            .fontSize(14).fontColor('#999999')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#F5F5F5').borderRadius(10)
            .onClick(() => { this.showDeleteModal = false })
          Text('删除')
            .fontSize(14).fontColor('#FFFFFF')
            .layoutWeight(1).textAlign(TextAlign.Center)
            .padding({ top: 10, bottom: 10 })
            .backgroundColor('#E53935').borderRadius(10)
            .margin({ left: 12 })
            .onClick(() => { this.showDeleteModal = false })
        }
        .width('100%')
      }
      .width('72%').padding(24)
      .backgroundColor('#FFFFFF').borderRadius(20)
      .shadow({ radius: 20, color: '#33000000', offsetY: 4 })
    }
    .width('100%').height('100%')
    .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
  }

  @Builder
  reviewCard(review: ReviewItem) {
    Column() {
      Row() {
        Column()
          .width(40).height(40)
          .borderRadius(20)
          .backgroundColor(CUISINE_CONFIG[review.cuisine].bg)
        Column() {
          Text(review.restaurantName)
            .fontSize(14).fontColor('#333333').fontWeight(FontWeight.Bold)
          Row() {
            Text('★'.repeat(review.rating))
              .fontSize(10).fontColor('#FFD54F')
            Text('☆'.repeat(5 - review.rating))
              .fontSize(10).fontColor('#CCCCCC')
          }
          .margin({ top: 2 })
        }
        .margin({ left: 10 })
        .layoutWeight(1)
        .alignItems(HorizontalAlign.Start)
        Text(review.date)
          .fontSize(10).fontColor('#AAAAAA')
      }
      .width('100%')

      Text(review.content)
        .fontSize(12).fontColor('#666666')
        .margin({ top: 8 })
        .alignSelf(ItemAlign.Start)

      Row() {
        Text('👍 12')
          .fontSize(10).fontColor('#999999')
          .margin({ right: 12 })
        Text('💬 3')
          .fontSize(10).fontColor('#999999')
        Column().layoutWeight(1)
        Text('✏️')
          .fontSize(14).fontColor('#FF6F00')
          .margin({ right: 8 })
          .onClick(() => { this.showEditModal = true })
        Text('🗑️')
          .fontSize(14).fontColor('#E53935')
          .onClick(() => { this.showDeleteModal = true })
      }
      .width('100%')
      .margin({ top: 8 })
    }
    .width('100%')
    .padding(14)
    .backgroundColor('#FFFFFF')
    .borderRadius(14)
    .shadow({ radius: 3, color: '#12000000', offsetY: 1 })
    .margin({ bottom: 8 })
  }

  @Builder
  settingItem(setting: SettingMeta) {
    Row() {
      Text(setting.icon)
        .fontSize(20).margin({ right: 12 })
      Column() {
        Text(setting.title)
          .fontSize(14).fontColor('#333333')
        Text(setting.subtitle)
          .fontSize(10).fontColor('#AAAAAA')
          .margin({ top: 2 })
      }
      .alignItems(HorizontalAlign.Start)
      .layoutWeight(1)
      Text('›')
        .fontSize(18).fontColor('#CCCCCC')
    }
    .width('100%')
    .padding(14)
    .backgroundColor('#FFFFFF')
    .borderRadius(12)
    .margin({ bottom: 6 })
  }

  build() {
    Stack() {
      Column() {
        Row() {
          Text('👤 我的')
            .fontSize(20).fontColor('#E53935').fontWeight(FontWeight.Bold)
          Column().layoutWeight(1)
          Text('✍️')
            .fontSize(20).fontColor('#E53935')
            .padding({ left: 8, right: 8 })
            .backgroundColor('#33E53935')
            .borderRadius(16)
            .onClick(() => { this.showAddModal = true })
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 8 })

        Scroll() {
          Column() {
            // Profile Card
            Column() {
              Row() {
                Column()
                  .width(60).height(60)
                  .borderRadius(30)
                  .backgroundColor('#E53935')
                  .justifyContent(FlexAlign.Center)
                  .alignItems(HorizontalAlign.Center)
                Column() {
                  Text('美食家小明')
                    .fontSize(18).fontColor('#333333').fontWeight(FontWeight.Bold)
                  Text('美食探索者 · Lv.5')
                    .fontSize(11).fontColor('#999999')
                    .margin({ top: 4 })
                }
                .margin({ left: 16 }).layoutWeight(1)
                .alignItems(HorizontalAlign.Start)
              }
              .width('100%')

              Row() {
                Column() {
                  Text(`${mockReviews.length}`)
                    .fontSize(18).fontColor('#E53935').fontWeight(FontWeight.Bold)
                  Text('评价')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
                Column() {
                  Text(`${getFavoritedCount()}`)
                    .fontSize(18).fontColor('#FF6F00').fontWeight(FontWeight.Bold)
                  Text('收藏')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
                Column() {
                  Text(`${mockRestaurants.length}`)
                    .fontSize(18).fontColor('#FFD54F').fontWeight(FontWeight.Bold)
                  Text('足迹')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
                Column() {
                  Text('128')
                    .fontSize(18).fontColor('#4CAF50').fontWeight(FontWeight.Bold)
                  Text('粉丝')
                    .fontSize(9).fontColor('#999999')
                }
                .layoutWeight(1).alignItems(HorizontalAlign.Center)
              }
              .width('100%')
              .padding({ top: 16, bottom: 8 })
            }
            .width('100%')
            .padding(16)
            .backgroundColor('#FFFFFF')
            .borderRadius(20)
            .shadow({ radius: 8, color: '#22000000', offsetY: 3 })
            .margin({ left: 16, right: 16, bottom: 12 })
            .linearGradient({
              angle: 135,
              colors: [['#FFFFFF', 0.0], ['#FFF8E1', 1.0]]
            })

            Text('我的评价')
              .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
              .alignSelf(ItemAlign.Start)
              .margin({ left: 16, bottom: 8 })

            this.reviewCard(mockReviews[0])
            this.reviewCard(mockReviews[1])
            this.reviewCard(mockReviews[2])
            this.reviewCard(mockReviews[3])
            this.reviewCard(mockReviews[4])
            this.reviewCard(mockReviews[5])
            this.reviewCard(mockReviews[6])
            this.reviewCard(mockReviews[7])

            Text('更多设置')
              .fontSize(15).fontColor('#333333').fontWeight(FontWeight.Medium)
              .alignSelf(ItemAlign.Start)
              .margin({ left: 16, top: 8, bottom: 8 })

            ForEach(PROFILE_SETTINGS, (setting: SettingMeta) => {
              this.settingItem(setting)
            })
          }
          .padding({ bottom: 16 })
        }
        .layoutWeight(1)
        .scrollBar(BarState.Off)
      }
      .width('100%').height('100%')

      if (this.showAddModal) {
        this.addReviewModal()
      }
      if (this.showEditModal) {
        this.editReviewModal()
      }
      if (this.showDeleteModal) {
        this.deleteReviewModal()
      }
    }
    .width('100%').height('100%')
  }
}

十二、整体架构设计思想总结

12.1 声明式 UI 与状态驱动

整个应用完全遵循 ArkTS 的声明式编程范式。所有的 UI 变化都源于状态(@State、@Observed)的变化,开发者无需直接操作 DOM 或调用刷新方法。这种"数据驱动视图"的模式,让代码的逻辑主线非常清晰:定义状态 -> 构建基于状态的 UI -> 用户交互修改状态 -> 框架自动更新 UI。

12.2 配置化与可维护性

CUISINE_CONFIG 和 PRICE_LEVEL_CONFIG 两个配置对象是本应用设计中最值得借鉴的部分。它们将视觉属性从组件代码中彻底抽离,实现了"一改全改"的维护效率。新增一种菜系或价格等级,只需在配置对象和常量数组中添加一行数据,所有引用处都会自动生效。

12.3 Builder 组件化

@Builder 装饰器在本应用中被大量使用,几乎每个可复用的 UI 片段都被封装为 Builder。Builder 相比独立的 @Component 组件更加轻量,适合在同一父组件内复用 UI 结构。对于更复杂的跨组件复用场景,可以进一步提取为独立的 @Component 组件。

12.4 模态弹窗的统一模式

所有弹窗都遵循统一的实现模式:Stack 根容器 + 条件渲染 + modalOverlay 遮罩层 + 居中内容卡片。这种一致性不仅降低了代码理解成本,也让用户可以建立稳定的交互预期。

12.5 类型安全

在这里插入图片描述

从接口定义、类属性到函数参数,整个应用都使用了严格的 TypeScript 类型注解。Record<string, CuisineMeta>、RestaurantItem | null、枚举类型等高级类型的运用,让代码在编译期就能捕获大量潜在错误,为项目的长期维护提供了坚实的类型保障。

深度学习之Pytorch---本人踩过的坑(报错总结)第十篇 深度学习之Pytorch安装时遇到的问题—本人踩过的坑(报错总结) 报错一:pip install torchvision 出现“由于目标计算机积极拒绝,无法连接”的问题 解决方案:找到自己浏览器的工具或选项,我的是火狐,具体解决方案如下图 然后找到桌面左下角,点击搜索internet,然后点击internet选项 最后回到Anaconda Prompt命令行输入pip install... 阅读详情

相关推荐

保姆级教程:手把手教你从NASA Earthdata下载ASTER L2地表温度数据(附shp文件拖拽技巧)

本文提供了一份详细的NASA Earthdata平台ASTER L2地表温度数据下载教程,涵盖账号注册、数据筛选、shp文件拖拽技巧及质量检查等关键步骤。特别适合遥感领域初学者,帮助快速掌握高效获取ASTER L2T数据的方法,适用于城市热岛效应和农业监测等研究。

weixin_30736301的博客 475

HarmonyOS 7 多端适配实战:旅行手记应用从直板单屏到三折叠与半折悬停

本文基于HarmonyOS Stage架构的旅行手记应用Travio,实战解析如何在不依赖设备型号判断的前提下,通过五路窗口监听、局部参照系(ComponentSize)、折痕避让、双栏路由守恒与锚点恢复等技术,实现三折叠、双折叠、分屏等六种形态下布局自适应。核心在于统一由EntryAbility采集窗口尺寸、断点与实体折痕,全局广播状态,各页面响应式渲染,避免挤崩、跳帧与返回栈异常,达成“一套代码,全形态兼容”的工程化闭环。

leon_teacher 7447

Elasticsearch连接中断:深入解析Connection reset by peer问题及优化策略

本文深入解析Elasticsearch连接中断问题,特别是Connection reset by peer错误的成因与解决方案。从TCP层的心跳机制到客户端与服务端的配置差异,详细介绍了网络不稳定、防火墙拦截等常见原因,并提供了六种优化策略,包括KeepAlive调整、TCP参数调优和智能重试机制等,帮助提升Elasticsearch连接的稳定性。

weixin_42546392的博客 414

鸿蒙 PC 适配:把 wxFormBuilder 的设计器引擎做成可安装应用

传统 C++ 桌面框架的鸿蒙适配,难点不在业务代码,而在「界面层拿不到」这件事。把模型与生成逻辑从 GUI 里剥出来,配合一层类型替身,能让大部分上游代码原样编译;代价是设计器界面需要另写。。

2202_76097976的博客 4588

论文阅读------Exposure Fusion: A Simple and Practical Alternative to High Dynamic Range Photography

Mertens T, Kautz J, Van Reeth F. Exposure fusion: A simple and practical alternative to high dynamic range photography[C]//Computer graphics forum. Oxford, UK: Blackwell Publishing Ltd, 2009, 28(1): 161-171. 摘要 本文提出了一种将包围曝光的序列融合到高质量图像中的技术,而无需先转换为高动态范围(HD.

qq_25283239的博客 3400

RK3588 , 基于mpp 解码 Jpeg To RGB

本文介绍了在RK3588开发板上使用Rockchip MPP(Media Process Platform)硬件加速接口将JPEG图像解码为RGB格式的完整流程。主要包括三个关键步骤:1) 读取JPEG文件数据;2) 配置MPP解码器,包括初始化上下文、设置RGB888输出格式和解码参数;3) 执行解码任务并将结果保存为图像文件。通过硬件加速,该方法能高效完成JPEG到RGB的转换,适用于嵌入式多媒体处理场景。

J。 1217

开源鸿蒙PC适配教程:把小熊猫C++(RedPanda-CPP)搬到鸿蒙桌面端

小熊猫C++(RedPanda-CPP)是一个用 C++17 和 Qt Widgets 写的轻量 C/C++ IDE,自带词法分析、预处理、补全、编译运行和 GDB 调试。本项目把它迁移到鸿蒙 PC,目标不是“能打开一个窗口”,而是让编辑、文件工作流、符号索引、补全和导航在鸿蒙桌面端真正可用。项目选择保留与 UI 解耦的上游解析器,用 ArkUI 重建外壳,这样工作量可控,解析器行为也不会和桌面版分叉。这样解析器看到的是“展开后的真实代码”,不会把。,先编译,看链接器缺什么,再只补缺失的声明和最少实现。

忆~遂愿的博客 3351

ABAP CDS SELECT, parameters 全面解析:把参数化视图玩明白(含可运行示例)

这篇技术博客介绍了ABAP CDS中参数化视图的用法与实践。文章从业务需求出发,解释了参数化视图的必要性,并详细说明了语法规则、参数类型匹配、视图内部参数引用等关键点。重点展示了在JOIN和ASSOCIATION中传递参数的写法,提供了航线分析的完整业务案例和可运行代码示例。文章还总结了视图实体与DDIC视图的差异、会话变量使用技巧等实践要点,特别强调了JOIN中必须显式传参的语法要求。通过真实场景演示,帮助开发者掌握参数化CDS的高效应用。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 371

【鸿蒙】把 TuxGuitar 搬上鸿蒙 PC:一份 GP 谱面从解析到出声的完整迁移

本文记录将开源吉他谱软件TuxGuitar 1.6.6完整移植至HarmonyOS PC/2-in-1设备的实战项目。突破三重壁垒:无JVM/SWT运行时、二进制GP3/GP4/GP5格式复杂性、桌面交互适配。采用Electron-on-OHOS架构,核心引擎以JS原生重写,实现从GP文件解析、六线谱渲染到MIDI播放与导出的全链路闭环。通过防御式游标读取、版本兼容处理、Web Audio合成与ArkTS壳层对接,构建可验证、可复现的PC端音乐编辑体验,验证了跨平台桌面应用在鸿蒙生态中的可行性。

杰西笔记 5131

【零基础入门unity游戏开发——3D篇】3D物理关节 —— Joint相关组件

【零基础入门unity游戏开发——3D篇】3D物理关节 —— Joint相关组件

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1209

【从零开始学习 UVM】1.2、UVM 概述 —— UVM Installation(UVM 安装)

【从零开始学习 UVM】1.2、UVM 概述 —— UVM Installation(UVM 安装)

努力,加油每一天鸭~ 1955

鸿蒙电脑开发ArkUI应用:自由窗口、尺寸约束与折叠电脑适配

鸿蒙PC上,页面不是直接铺在屏幕里,而是装在一个可以被拖动、拉伸、贴边、最大化、同时开好几份的窗口里。手机开发里那套“屏幕宽度是固定的几个档位”的经验,在这里全部失效。窗口宽度从 720vp 到 1800vp 连续变化,中间每一档都可能被用户停在上面。窗口相关的 API 集中在 的 命名空间,屏幕和折叠态在 命名空间。很多问题不是 API 不会调,而是单位、层级和生效顺序没搞清楚。这篇按“窗口是谁管的、能变成什么样、尺寸怎么算、子窗口怎么开、多屏和折叠怎么拿数据”这条线讲透。接口签名以本机 SDK 的

漫步云端 916

SAP S/4HANA Public Cloud 配置全景实践

本文提供SAP S/4HANA Public Cloud配置的实用指南,重点介绍三大核心工具:CBC(范围界定与配置)、SAP Activate(方法论与路线图)和Cloud ALM(项目执行与运维)。文章详解了不同系统架构(2SL/3SL)的配置路径,并以制造企业案例说明端到端实施流程。同时提供了工具协同操作方法、初始访问安全建议和常见问题的专家意见,帮助项目团队高效完成实施工作。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 575

把 Ghostscript 迁到鸿蒙 PC:从 Linux 命令行到 libgs + NAPI + ArkUI

Ghostscript 原本是 Linux/Unix 下的命令行程序。它没有独立 GUI,真正的核心是 PostScript/PDF 解释器、PDF writer、图像设备、字体和 Resource。本文按实际迁移顺序记录工程环境、原生库编译、NAPI 桥接、系统能力替换、应用构建和验证结果。

池央的博客 5004

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

AI 三方库适配工具之 HarmonyOS Dev Assistant:从 mailto 到鸿蒙 Want,flutter_email_sender 适配实录

本文记录用 HarmonyOS Dev Assistant 在 VS Code 中将 Flutter 联邦插件 flutter_email_sender 迁移到鸿蒙的完整过程:按「分析→编码→验证」三阶段产出可核验报告,用 mailto 隐式 Want 交接替代原生撰写器,补齐 Dart 侧 TargetPlatform.ohos 门禁与 ArkTS 实现,并覆盖签名、真机运行与 10 个常见问题,最终 94/94 API 路径通过、构建零错误。

白天写代码,晚上追AI大模型,偶尔搞点八卦 开源动态|技术干货|实习机会|生态活动 我负责分享,你负责变强 57

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

把 Apache SkyWalking 搬上鸿蒙 PC:从 Java OAP 到原生 OAP-lite 的完整移植实践

把 Apache SkyWalking 搬上鸿蒙 PC:从 Java OAP 到原生 OAP-lite 的完整移植实践

qq_2778465975的博客 8599

React Native 鸿蒙应用接续实战:基于 TurboModule 桥接跨设备迁移,让编辑现场在手机与电脑之间无缝流转

本文基于React Native 0.84.1与RNOH 0.84.4,实测实现鸿蒙跨设备应用接续功能。针对RN生态无现成接续库的痛点,自研react-native-ohos-app-continuation三方库,通过TurboModule桥接,实现手机与电脑间草稿内容双向无缝迁移。核心突破包括:利用onContinue返回Promise支持异步数据收集、设计冷启动暂存/热启动推送双策略、规避系统页面栈自动恢复对RN失效问题,并验证100KB传输限制与版本兼容性处理。

白天写代码,晚上追AI大模型,偶尔搞点八卦 开源动态|技术干货|实习机会|生态活动 我负责分享,你负责变强 65

开源鸿蒙 PC 适配教程:把 Cytoscape 搬到鸿蒙桌面端

JavaFX 原生管线仍需继续适配,应保留为独立问题,避免与已经通过的 Swing 工作流混为一项结论。

Zero_VPN的博客 4941

KMP-CMP 鸿蒙冷启动与包体积归因实战:源码级移植 Koin 4.1.1,把依赖图初始化与双 ABI so 从首屏关键路径上拆下来

本文基于KMP-CMP架构,针对鸿蒙跨平台应用冷启动慢、包体积大、首屏白屏等问题,提出“启动与包体预算台”实验方案。通过移植Koin 4.1.1至鸿蒙(坐标com.ohos.koin:koin-core:4.1.1-ohos.1),实现对依赖注入初始化耗时、libkn.so体积及首屏白屏的统一量化测量。实测显示:释放包体积减少34.1%,双ABI libkn.so减小36.2%,首屏依赖解析耗时降低50%以上。所有数据来自同一模拟器环境,支持可复现验证,为跨平台启动优化提供可信基准。

白天写代码,晚上追AI大模型,偶尔搞点八卦 开源动态|技术干货|实习机会|生态活动 我负责分享,你负责变强 127

HarmonyOS 7 ContainerReader实战:康复计划在手机单列与平板双栏间切换

解决的不是“识别手机还是平板”,而是“知道当前组件到底分到了多少空间”。康复随行用360和680两个单调递增阈值决定单列或双栏,把容器尺寸绑定交给ArkUI,把当前动作和完成进度留在稳定的业务状态层。这样一来,同一份计划可以在手机、平板全屏和平板分屏之间自然重排,选择状态不会因为布局分支变化而丢失。真正的多端适配也不再是把手机页面拉宽,而是让信息结构随着可用空间改变,同时保持用户任务连续。真机这一轮只走通了窄容器那半边:手机竖屏读到的容器宽度和断点,正好印证了断点判断按的是组件实际空间,而不是设备名字。

nanxuan521的专栏 105

把三方库适配经验写成 Skill:从一次 ohosArm64 踩坑到可复用配方

本文复盘一次KMP适配OpenHarmony的踩坑全过程:从误判“加ohosArm64()即能适配”到通过grep取证发现标准Kotlin无此方法,最终定位到需使用社区版Kotlin发行版(2.2.21-1.0.0)。文章提炼出可复用配方——含Maven仓库前置、gradle.properties开关、pinOhosDependencies严格锁版本及补全目标文档,并沉淀为ohos-kmp-adaptation Skill。强调:技能应固化确定性知识,人工判断留白边界,明确哪些库不可适配(如JVM-only

wdracky的专栏 1640

HarmonyOS 7 FAST Kit向量运算实战:用包络残差核对康复节拍音频

向量 API 的价值不在于把八次减法写短,而在于给内容审计留一个可以复测的量化入口。这一篇用Vsub保住每个窗口的有符号偏差,用Svesq聚合整体误差,再靠长度合同、归一化区间、批次修订、双阈值和人工回执把结果关进流程里。经 NAPI 桥正常回包,标准曲线读数与手算预期一致;记录不完整时,结果不生成、保存按钮不开放。没测的部分仍然没测——点数梯度、耗时与内存、前后台、大字和读屏,以及真实音频到包络这一段。所以它是一段可复现的产品流程验证,不是一份性能报告。

nanxuan521的专栏 35

HarmonyOS 7.0 商户端实战:组件生命周期 aboutToAppear/Disappear

列表页在里发网络请求,快速进退几次后开始出现「页面已销毁但回调还在改@State」的告警;还有一次把埋点放在,结果切后台也触发了,数据直接翻倍。生命周期看着基础,但「哪个回调在什么时机触发、异步任务该在哪儿收」这两件事,翻一遍随包 SDK 的声明会清楚很多。里其实有一整套可核查的细粒度生命周期观察接口,很多人没用过。

nanxuan521的专栏 194

KMP/CMP 接入鸿蒙数字盾:对账单导出前的文案预检

本文介绍在鸿蒙系统中接入数字盾进行可信确认前的预检机制,通过检查系统能力、文案格式与最高安全级别三步,提前发现导出对账单等操作中的潜在问题。示例项目使用Kotlin多平台技术,结合Device Security Kit实现接口调用,支持真机与模拟器测试,帮助开发者在联调阶段定位环境、文案或权限问题,提升接入效率与用户体验。

白天写代码,晚上追AI大模型,偶尔搞点八卦 开源动态|技术干货|实习机会|生态活动 我负责分享,你负责变强 195

React Native 鸿蒙碰一碰分享实战:两台手机一碰就传(手机对手机)

两部鸿蒙手机顶部一碰,文件就传了过去,但接收端应用里的接收会话落盘 0 个文件:手机对手机时应用只能站在发起方,接收由系统卡片完成,沙箱直收不支持手机。本文用一轮真机实录讲清三件事:发起端和发给电脑是同一套代码、发送结果只有一个含义、能力快照全是勾为什么却收不到。

康宝的博客 1万+

Cordova 鸿蒙原生 Markdown 引擎交叉编译与直连实践:用 Dev Assistant 把 C99 库装进 CDVPlugin

@cordova-ohos/cordova-plugin-cmark 通过将 C99 兼容的 cmark 引擎交叉编译为鸿蒙动态库,并接入 Cordova 插件后台线程,解决长篇 Markdown 解析卡顿问题。相比纯 JS 在主线程阻塞 100+ms,原生解析将计算移出主线程,仅在跨层传输时产生 6–7 帧波动,显著提升流畅度。插件默认过滤危险标签,保障安全,完整代码开源于 abaoge/cordova-plugin-cmark,支持在 CPF-Cordova 与 OpenHarmony 三方库中心交流。

白天写代码,晚上追AI大模型,偶尔搞点八卦 开源动态|技术干货|实习机会|生态活动 我负责分享,你负责变强 2万+

轻远屏诞生记:把 x2goclient 搬到鸿蒙 PC 的编译链与踩坑清单

摘要:轻远屏将X2Go客户端4.1.2.3移植至鸿蒙PC,基于ArkWeb运行时实现Web应用承载,通过boot.js桥接文件选择、窗口控制等本地能力,保留会话配置与VNC显示功能。项目全仓362文件(上游342个),已上架华为应用市场。适配核心难点包括后端依赖替换、触摸适配及包名一致性,采用单进程kiosk形态,真机验证通过,完整走通配置管理流程。

摘星编程的博客 5815
上一篇: 健身运动管理应用对象内部的属性在 ArkTS 运行时可能被修改,使用 Object.freeze() 或只读类型 Readonly<Record<...>> 来强化
下一篇: 理财投资组合应用基于HarmonyOS API 24实战解析以单一 @Entry @Component 作为主入口,内部通过 @Builder 方法将庞杂的界面拆解为多个独立的构建单元
吉祥870
博客等级 码龄3年 9073粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值