鸿蒙 ArkTS 实战:床品收纳容量的状态建模、算法与交互实现

前言

床品收纳容量用于按被子、枕头和床单占位估算收纳袋是否足够。
项目通过 ArkTSArkUI@State 管理 capacity、quilts、pillows、sheets。
核心是 床品权重、容量公式和分袋提示,默认数据为80L、2 床被子、3 个枕头、4 床单。
页面展示已用容量和容量是否合适。
在这里插入图片描述
图 1:DevEco Studio 中的 ArkTS 开发环境。

页面结果应能从当前状态完整复算。
边界条件需要落到真实代码路径。
被子占位权重最高。
本文只讨论源码已实现能力。

一、业务对象

1.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑业务对象。
被子占位权重最高。

1.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

1.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

1.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

业务对象维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

二、状态字段

2.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑状态字段。
三类床品数量独立调整。

2.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

2.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

2.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

三、核心规则

3.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑核心规则。
超过容量提示分袋。

3.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

3.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

3.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

  1. 读取输入。
  2. 执行业务规则。
  3. 处理边界。
  4. 刷新界面。

四、输入控件

4.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑输入控件。
被子占位权重最高。

4.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

4.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

4.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

输入控件维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

五、事件回调

5.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑事件回调。
三类床品数量独立调整。

5.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

5.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

5.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

六、边界保护

6.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑边界保护。
超过容量提示分袋。

6.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

6.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

6.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

  • 床品收纳容量保存真实状态。
  • used参与核心逻辑。
  • 已用容量和容量是否合适形成反馈。
  • 床品权重、容量公式和分袋提示保护输出。

七、状态反馈

7.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑状态反馈。
被子占位权重最高。

7.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

7.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

7.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

八、默认运行

8.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑默认运行。
三类床品数量独立调整。

8.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

8.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

8.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

默认运行维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

九、连续迁移

9.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑连续迁移。
超过容量提示分袋。

9.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

9.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

9.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

  1. 读取输入。
  2. 执行业务规则。
  3. 处理边界。
  4. 刷新界面。

十、数组或数值更新

10.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑数组或数值更新。
被子占位权重最高。

10.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

10.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

10.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

十一、声明式刷新

11.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑声明式刷新。
三类床品数量独立调整。

11.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

11.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

11.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

  • 床品收纳容量保存真实状态。
  • used参与核心逻辑。
  • 已用容量和容量是否合适形成反馈。
  • 床品权重、容量公式和分袋提示保护输出。

十二、布局层级

12.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑布局层级。
超过容量提示分袋。

12.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

12.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

12.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

布局层级维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

十三、颜色语义

13.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑颜色语义。
被子占位权重最高。

13.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

13.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

13.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

十四、异常输入

14.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑异常输入。
三类床品数量独立调整。

14.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

14.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

14.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

十五、验证场景

15.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑验证场景。
超过容量提示分袋。

15.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

15.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

15.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

  1. 读取输入。
  2. 执行业务规则。
  3. 处理边界。
  4. 刷新界面。

十六、性能表现

16.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑性能表现。
被子占位权重最高。

16.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

16.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

16.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

性能表现维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

十七、维护方式

17.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑维护方式。
三类床品数量独立调整。

17.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

17.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

17.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

十八、代码职责

18.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑代码职责。
超过容量提示分袋。

18.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

18.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

18.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

  • 床品收纳容量保存真实状态。
  • used参与核心逻辑。
  • 已用容量和容量是否合适形成反馈。
  • 床品权重、容量公式和分袋提示保护输出。

十九、运行结论

19.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑运行结论。
被子占位权重最高。

19.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

19.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

19.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

二十、项目启示

20.1 数据结构

床品收纳容量使用 capacity、quilts、pillows、sheets 支撑项目启示。
三类床品数量独立调整。

20.2 业务规则

被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较
used 及相关表达式承担核心行为。

20.3 交互刷新

控件回调更新状态并显示已用容量和容量是否合适。
ArkUI 根据依赖关系刷新。

20.4 边界结论

床品权重、容量公式和分袋提示
结果因此保持稳定。

项目启示维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

二十一、真实源码分段

21.1 源码片段 1

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.2 源码片段 2

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.3 源码片段 3

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.4 源码片段 4

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.5 源码片段 5

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.6 源码片段 6

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.7 源码片段 7

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

21.8 源码片段 8

以下代码来自当前项目。

@Entry @Component struct Index{@State capacity:number=80;@State quilts:number=2;@State pillows:number=3;@State sheets:number=4;used():number{return this.quilts*30+this.pillows*12+this.sheets*4}build(){Column(){Text('床品收纳容量').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('按床品占位估算收纳袋容量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.used()+'/'+this.capacity+' L').fontSize(42).fontWeight(FontWeight.Bold).fontColor(this.used()>this.capacity?'#B42318':'#175CD3');Text('收纳袋容量:'+this.capacity+' L').width('100%').margin({top:22});Slider({value:this.capacity,min:40,max:200,step:10}).onChange((v:number)=>this.capacity=Math.round(v)).width('100%');Text('被子 '+this.quilts+' · 枕头 '+this.pillows+' · 床单 '+this.sheets).width('100%').margin({top:18});Slider({value:this.quilts,min:0,max:6,step:1}).onChange((v:number)=>this.quilts=Math.round(v)).width('100%');Slider({value:this.pillows,min:0,max:8,step:1}).onChange((v:number)=>this.pillows=Math.round(v)).width('100%');Slider({value:this.sheets,min:0,max:12,step:1}).onChange((v:number)=>this.sheets=Math.round(v)).width('100%');Text(this.used()>this.capacity?'容量不足,建议分袋':'容量合适').width('100%').textAlign(TextAlign.Center).padding(18).margin({top:18}).backgroundColor(this.used()>this.capacity?'#FFF1F0':'#F0FAF4').borderRadius(9)}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}

该片段参与按被子、枕头和床单占位估算收纳袋是否足够。

二十二、运行案例

22.1 默认数据

使用80L、2 床被子、3 个枕头、4 床单。
观察 capacity、quilts、pillows、sheets 与结果。
确认已用容量和容量是否合适可复算。

22.2 最小边界

床品权重、容量公式和分袋提示
观察 capacity、quilts、pillows、sheets 与结果。
确认已用容量和容量是否合适可复算。

22.3 临界输入

围绕床品权重、容量公式和分袋提示验证阈值。
观察 capacity、quilts、pillows、sheets 与结果。
确认已用容量和容量是否合适可复算。

22.4 连续操作

连续执行 used。
观察 capacity、quilts、pillows、sheets 与结果。
确认已用容量和容量是否合适可复算。

验证维度 当前实现 作用
状态 capacity、quilts、pillows、sheets 保存数据
规则 被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较 生成反馈
边界 床品权重、容量公式和分袋提示 保护结果
结果 已用容量和容量是否合适 呈现结论

二十三、技术取舍

23.1 本地状态

床品收纳容量围绕床品权重、容量公式和分袋提示选择本地状态。
当前职责集中且可追踪。

23.2 具名函数

床品收纳容量围绕床品权重、容量公式和分袋提示选择具名函数。
当前职责集中且可追踪。

23.3 声明式组件

床品收纳容量围绕床品权重、容量公式和分袋提示选择声明式组件。
当前职责集中且可追踪。

23.4 清晰边界

床品收纳容量围绕床品权重、容量公式和分袋提示选择清晰边界。
当前职责集中且可追踪。

二十四、总结

床品收纳容量使用鸿蒙 ArkTS 完成了“按被子、枕头和床单占位估算收纳袋是否足够”闭环。
核心规则是:被子乘 30L、枕头乘 12L、床单乘 4L,used 与容量比较。
状态字段为 capacity、quilts、pillows、sheets。
状态建模业务函数声明式界面 共同保证结果稳定。


相关资源:

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐