1、简介
2、代码示例
class Item{
name : String
image:ResourceStr
price:number
constructor(name:String,image:ResourceStr,price:number) {
this.name = name
this.image =image
this.price = price
}
}
@Entry
@Component
struct ItemPage {
//商品数据
private items: Array<Item> = [
new Item('华为Mate60',$r('app.media.phone1'),6999),
new Item('华为Mate60',$r('app.media.phone1'),7999),
new Item('华为Mate60',$r('app.media.phone1'),8999),
new Item('华为Mate60',$r('app.media.phone1'),5999),
new Item('华为Mate60',$r('app.media.phone1'),6569),
]
build(){
Column({space:8}){
Row(){
Text('商品列表')
.fontSize(30)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.margin({bottom:20})
ForEach(
this.items,
(item:Item )=>{
Row({space:10}){
Image(item.image)
.width(100)
Column({space:4}){
Text('华为Mater40')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text('¥'+item.price)
.fontColor('#F36')
.fontSize(18)
}
}
.width('100%')
.height(120)
.backgroundColor('#FFF')
.borderRadius(20)
.padding(10)
}
)
}
.width('100%')
.height('100%')
.backgroundColor('#EFEFEFF')
.padding(20)
}
}
3、效果展示
4、需求升级
4.1 简介
4.2 代码示例
class Item{
name : String
image:ResourceStr
price:number
discount:number
constructor(name:String,image:ResourceStr,price:number,discount:number =0) {
this.name = name
this.image =image
this.price = price
this.discount = discount
}
}
@Entry
@Component
struct ItemPage {
//商品数据
private items: Array<Item> = [
new Item('华为Mate60',$r('app.media.phone1'),6999,500),
new Item('华为Mate60',$r('app.media.phone1'),7999),
new Item('华为Mate60',$r('app.media.phone1'),8999),
new Item('华为Mate60',$r('app.media.phone1'),5999),
new Item('华为Mate60',$r('app.media.phone1'),6569),
]
build(){
Column({space:8}){
Row(){
Text('百亿补贴')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.fontColor('red')
}
.width('100%')
.margin({bottom:20})
ForEach(
this.items,
(item:Item )=>{
Row({space:10}){
Image(item.image)
.width(100)
Column({space:4}){
if(item.discount){
Text('华为Mater40')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text('原价:¥'+item.price)
.fontColor('#ccc')
.fontSize(14)
.decoration({type:TextDecorationType.LineThrough})
Text('折扣价:¥'+(item.price-item.discount))
.fontColor('#F36')
.fontSize(18)
Text('补贴¥'+item.discount)
.fontColor('#F36')
.fontSize(18)
}else{
Text('华为Mater40')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text('¥'+item.price)
.fontColor('#F36')
.fontSize(18)
}
}
}
.width('100%')
.height(120)
.backgroundColor('#FFF')
.borderRadius(20)
.padding(10)
}
)
}
.width('100%')
.height('100%')
.backgroundColor('#EFEFEFF')
.padding(20)
}
}
4.3 效果展示