1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| Tabs(value?: {barPosition?: BarPosition, index?: number, controller?: TabsController})
@Entry @Component struct TabsExample { @State fontColor: string = '#182431' @State selectedFontColor: string = '#007DFF' @State currentIndex: number = 0 @State tabArr: Array<object> = [ { bgColor: '#00CB87', color: 'green' }, { bgColor: '#007DFF', color: 'blue' }, { bgColor: '#FFBF00', color: 'yellow'}, { bgColor: '#E67C92', color: 'pink' }] private controller: TabsController = new TabsController()
@Builder TabBuilder(index: number, name: string) { Column() { Text(name) .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor) .fontSize(16) .fontWeight(this.currentIndex === index ? 500 : 400) .lineHeight(22) .margin({ top: 17, bottom: 7 }) Divider() .strokeWidth(2) .color('#007DFF') .opacity(this.currentIndex === index ? 1 : 0) }.width('100%') }
build() { Column() { Tabs({ barPosition: BarPosition.Start, controller: this.controller }) { ForEach(this.tabArr,({bgColor,color},index)=>{ TabContent() { Column().width('100%').height('100%').backgroundColor(bgColor) }.tabBar(this.TabBuilder(index, color)) }) } .vertical(false) .barMode(BarMode.Fixed) .barWidth(360) .barHeight(56) .animationDuration(0) .onChange((index: number) => { this.currentIndex = index }) .width(360) .height(296) .margin({ top: 52 }) .backgroundColor('#F1F3F5') }.width('100%') } }
|