• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2024 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@CustomDialog
16struct CustomDialogExampleTwo {
17  controllerTwo?: CustomDialogController
18  controllerThree?: CustomDialogController
19  build() {
20    Column() {
21      Text('我是第二个弹窗')
22        .fontSize(30)
23        .height(100)
24      Button('点我关闭第二个弹窗')
25        .onClick(() => {
26          if (this.controllerTwo != undefined) {
27            this.controllerTwo.close()
28          }
29        })
30        .margin(20)
31    }
32  }
33}
34
35@CustomDialog
36@Component
37struct CustomDialogExample {
38  @Link textValue: string
39  @Link inputValue: string
40  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({
41    builder: CustomDialogExampleTwo(),
42    alignment: DialogAlignment.Bottom,
43    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
44      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
45      console.log("dialog onWillDismiss")
46      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
47        dismissDialogAction.dismiss()
48      }
49      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
50        dismissDialogAction.dismiss()
51      }
52    },
53    offset: { dx: 0, dy: -25 } })
54  controller?: CustomDialogController
55
56  build() {
57    Column() {
58      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
59      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
60        .onChange((value: string) => {
61          this.textValue = value
62        })
63      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
64      Button('点我打开第二个弹窗')
65        .onClick(() => {
66          if (this.dialogControllerTwo != null) {
67            this.dialogControllerTwo.open()
68          }
69        })
70        .margin(20)
71    }.borderRadius(10)
72  }
73}
74
75@Entry
76@Component
77struct CustomDialogUser {
78  @State textValue: string = ''
79  @State inputValue: string = 'click me'
80  dialogController: CustomDialogController | null = new CustomDialogController({
81    builder: CustomDialogExample({
82      cancel: ()=> { this.onCancel() },
83      confirm: ()=> { this.onAccept() },
84      textValue: $textValue,
85      inputValue: $inputValue
86    }),
87  })
88
89  aboutToDisappear() {
90    this.dialogController = null // 将dialogController置空
91  }
92
93  build() {
94    Column() {
95      Button(this.inputValue)
96        .onClick(() => {
97          if (this.dialogController != null) {
98            this.dialogController.open()
99          }
100        }).backgroundColor(0x317aff)
101    }.width('100%').margin({ top: 5 })
102  }
103}