• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *  http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * 按钮组件,背景/图标可根据use切换展示状态
18 */
19@Component
20export default struct ComponentOption {
21  private useResource: Resource = $r('app.media.icon_openvalley_hangup');
22  private unUseResource: Resource = $r('app.media.icon_openvalley_hangup');
23  private useBgColorResource?: Resource = $r('app.color.color_fff');
24  @Link use: boolean;
25  private callback: ((use: boolean) => void) | null = null;
26  private canChange: (() => boolean) | null = null;
27  private mSize = 72;
28  private btnId = 'btnId';
29
30  build() {
31    Button() {
32      Image(this.use ? this.useResource : this.unUseResource)
33        .width('60%')
34        .objectFit(ImageFit.Contain)
35        .height('60%')
36    }
37    .id(this.btnId)
38    .align(Alignment.Center)
39    .width(this.mSize)
40    .height(this.mSize)
41    .type(ButtonType.Circle)
42    .backgroundColor(this.use ? this.useBgColorResource : $r('app.color.color_01_fff'))
43    .onClick(() => {
44      if (this.canChange && this.canChange()) {
45        this.use = !this.use
46      }
47      if (this.callback) {
48        this.callback(this.use)
49      }
50    })
51  }
52}