import { Dim, FontSize, InsetKey, Insets, floatingBarMaterial } from '../common/Theme'; /** * 通用小组件:官方符号图标、分段选择器、设置行、空状态、主按钮、悬浮工具栏。 * 需要随父组件状态刷新的数据一律使用 @Prop 传递。 */ /** 官方 HarmonyOS Symbol 图标(sys.symbol.*),单色渲染 */ @Component export struct SymIcon { @Prop sym: Resource = $r('sys.symbol.circle'); @Prop iconSize: number = 18; @Prop colorRes: ResourceColor = $r('app.color.font_ter'); build() { SymbolGlyph(this.sym) .fontSize(this.iconSize) .fontColor([this.colorRes]) } } @Component export struct SegmentedControl { options: string[] = []; @Prop activeIndex: number = 0; onSelect: (index: number) => void = (_: number) => {}; build() { Row({ space: 4 }) { ForEach(this.options, (opt: string, idx: number) => { Text(opt) .fontSize(this.activeIndex === idx ? FontSize.sub : FontSize.cap) .fontWeight(this.activeIndex === idx ? FontWeight.Medium : FontWeight.Normal) .fontColor(this.activeIndex === idx ? $r('app.color.on_brand') : $r('app.color.font_sec')) .textAlign(TextAlign.Center) .layoutWeight(1) .height(32) .borderRadius(8) .backgroundColor(this.activeIndex === idx ? $r('app.color.brand') : Color.Transparent) .onClick(() => { if (this.activeIndex !== idx) { this.onSelect(idx); } }) }, (opt: string) => opt) } .padding(4) .borderRadius(12) .backgroundColor($r('app.color.chip_bg')) .width('100%') } } @Component export struct SectionCard { @Prop title: string = ''; @BuilderParam content: () => void = emptyBuilder; build() { Column({ space: 8 }) { if (this.title.length > 0) { Text(this.title) .fontSize(FontSize.sub) .fontWeight(FontWeight.Medium) .fontColor($r('app.color.font_sec')) .width('100%') .padding({ left: 12 }) } Column() { this.content() } .width('100%') .padding({ top: 4, bottom: 8 }) .borderRadius(Dim.cardRadius) .backgroundColor($r('app.color.card_bg')) } .width('100%') .alignItems(HorizontalAlign.Start) } } @Component export struct SettingRow { @Prop title: string = ''; @Prop desc: string = ''; @Prop valueText: string = ''; @Prop showToggle: boolean = false; @Prop isOn: boolean = false; onToggle: (v: boolean) => void = (_: boolean) => {}; build() { Row() { Column({ space: 2 }) { Text(this.title) .fontSize(FontSize.body) .fontColor($r('app.color.font_pri')) .maxLines(2) if (this.desc.length > 0) { Text(this.desc) .fontSize(FontSize.cap) .fontColor($r('app.color.font_ter')) } } .layoutWeight(1) .alignItems(HorizontalAlign.Start) Blank() if (this.showToggle) { Toggle({ type: ToggleType.Switch, isOn: this.isOn }) .selectedColor($r('app.color.brand')) .onChange((v: boolean) => { this.onToggle(v); }) } else if (this.valueText.length > 0) { Text(this.valueText) .fontSize(FontSize.body) .fontWeight(FontWeight.Medium) .fontColor($r('app.color.brand')) } } .width('100%') .constraintSize({ minHeight: 48 }) .padding({ top: 10, bottom: 10 }) } } @Component export struct EmptyView { @Prop sym: Resource = $r('sys.symbol.circle'); @Prop title: string = '暂无内容'; @Prop sub: string = ''; build() { Column({ space: 10 }) { SymIcon({ sym: this.sym, iconSize: 44, colorRes: $r('app.color.font_ter') }) Text(this.title) .fontSize(FontSize.sub) .fontColor($r('app.color.font_sec')) if (this.sub.length > 0) { Text(this.sub) .fontSize(FontSize.cap) .fontColor($r('app.color.font_ter')) .textAlign(TextAlign.Center) } } .width('100%') .padding({ top: 56, bottom: 56 }) } } @Component export struct PrimaryButton { @Prop label: string = ''; @Prop isEnabled: boolean = true; onTap: () => void = () => {}; build() { Button(this.label) .fontSize(FontSize.body) .fontWeight(FontWeight.Medium) .fontColor(this.isEnabled ? $r('app.color.on_brand') : $r('app.color.font_ter')) .backgroundColor(this.isEnabled ? $r('app.color.brand') : $r('app.color.chip_bg')) .height(Dim.btnHeight) .borderRadius(Dim.btnRadius) .width('100%') .enabled(this.isEnabled) .onClick(() => { if (this.isEnabled) { this.onTap(); } }) } } @Builder function emptyBuilder() { } /** * 悬浮工具栏(HarmonyOS 6.1+ 悬浮工具栏样式):胶囊容器悬浮于页面底部, * 容器使用沉浸光感 THIN 材质,并在其下方铺一层与页面背景同色的渐变蒙层(高出栏顶 16vp)。 * 需放在页面根 Stack 的底部对齐位置,内容区自行预留 Insets.contentBottom() 的底部空间。 */ @Component export struct FloatingToolbar { @StorageProp(InsetKey.BOTTOM) bottomInset: number = 0; @BuilderParam content: () => void = emptyBuilder; build() { Stack({ alignContent: Alignment.Bottom }) { // 渐变颜色蒙层:不拦截触摸,内容滚动至栏下时自然衰减 Column() .width('100%') .height(Insets.contentBottom(this.bottomInset)) .linearGradient({ direction: GradientDirection.Bottom, colors: [[$r('app.color.page_bg_clear'), 0], [$r('app.color.page_bg_mask'), 0.7]] }) .hitTestBehavior(HitTestMode.None) Column() { Row({ space: 4 }) { this.content() } .width('100%') .height(Dim.floatBarH) .constraintSize({ maxWidth: 480 }) .padding({ left: 8, right: 8 }) .borderRadius(Dim.floatBarH / 2) .systemMaterial(floatingBarMaterial()) } .width('100%') .padding({ left: Dim.pagePad, right: Dim.pagePad, bottom: Insets.floatBarBottom(this.bottomInset) }) } .width('100%') .hitTestBehavior(HitTestMode.Transparent) } } /** 悬浮工具栏中的操作项:普通项为文本 + 图标,primary 为品牌色胶囊主操作 */ @Component export struct ToolbarAction { @Prop label: string = ''; @Prop sym: Resource | undefined = undefined; @Prop primary: boolean = false; @Prop danger: boolean = false; @Prop isEnabled: boolean = true; onTap: () => void = () => {}; private fg(): ResourceColor { if (!this.isEnabled) { return $r('app.color.font_ter'); } if (this.primary) { return $r('app.color.on_brand'); } return this.danger ? $r('app.color.err') : $r('app.color.font_pri'); } build() { Button({ type: ButtonType.Capsule, stateEffect: this.isEnabled }) { Row({ space: 6 }) { if (this.sym !== undefined) { SymbolGlyph(this.sym) .fontSize(18) .fontColor([this.fg()]) } Text(this.label) .fontSize(FontSize.sub) .fontWeight(this.primary ? FontWeight.Medium : FontWeight.Normal) .fontColor(this.fg()) .maxLines(1) .textOverflow({ overflow: TextOverflow.Ellipsis }) } .padding({ left: 14, right: 14 }) } .height(40) .layoutWeight(this.primary ? 1.4 : 1) .backgroundColor(this.primary ? (this.isEnabled ? $r('app.color.brand') : $r('app.color.chip_bg')) : Color.Transparent) .enabled(this.isEnabled) .onClick(() => { if (this.isEnabled) { this.onTap(); } }) } }