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 RenderControlWithStack { 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 Stack() { 33 Scroll() { 34 Column() { 35 Stack() { // 在条件渲染外套一层容器,限制刷新范围 36 if (this.isVisible) { 37 Text('New item') 38 .fontSize($r('app.integer.font_size_20')) 39 } 40 } 41 42 ForEach(this.data, (item: number) => { 43 Text(`Item value: ${item}`) 44 .fontSize($r('app.integer.font_size_20')) 45 .width($r('app.string.layout_100_percent')) 46 .textAlign(TextAlign.Center) 47 }, (item: number) => item.toString()) 48 } 49 } 50 }.height($r('app.string.layout_90_percent')) 51 52 Button('Switch Hidden and Show') 53 .onClick(() => { 54 this.isVisible = !(this.isVisible); 55 }) 56 } 57 } 58} 59