1/* 2 * Copyright (c) 2023 Huawei Device 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 16import Constants from '../../util/Constants'; 17 18@Preview 19@Component 20export struct BetterUseVisibility { 21 @State isVisible: boolean = true; 22 private data: number[] = []; 23 24 aboutToAppear() { 25 for (let i: number = 0; i < Constants.IMAGE_TOTAL_NUM; i++) { 26 this.data.push(i); 27 } 28 } 29 30 build() { 31 Column() { 32 Button($r('app.string.switch_button_text')) 33 .onClick(() => { 34 this.isVisible = !(this.isVisible); 35 }) 36 .width($r('app.string.layout_100_percent')) 37 Scroll() { 38 Column() { 39 ForEach(this.data, (item: number) => { 40 Image($r('app.media.icon')) 41 .width($r('app.string.foreach_image_width')) 42 .height($r('app.string.foreach_image_height')) 43 }, (item: number) => item.toString()) 44 } 45 }.visibility(this.isVisible ? Visibility.Visible : Visibility.None) // 使用显隐控制切换,不会频繁创建与销毁组件 46 } 47 } 48} 49