• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Dialog Controller
2The dialog controller in ArkUI provides the capability to operate on a bound dialog box. Currently, it supports the close functionality. You can pass the controller into the dialog box content area to perform operations.
3
4Since API version 18, you can set the [controller](../reference/apis-arkui/js-apis-promptAction.md#dialogcontroller18) parameter to bind the controller, and operate the dialog box through the controller.
5
6## Constraints
7
8Currently, the [openCustomDialogWithController](../reference/apis-arkui/js-apis-arkui-UIContext.md#opencustomdialogwithcontroller18) and [presentCustomDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#presentcustomdialog18) APIs support binding dialog controllers through the **controller** parameter. The [getDialogController](../reference/apis-arkui/arkui-ts/ts-custom-component-api.md#getdialogcontroller18) API can obtain the dialog controller for the custom component where the dialog box is located.
9
10> **NOTE**
11>
12> A dialog controller can be bound to only one dialog box, and operations only affect that dialog box.
13> When [getDialogController](../reference/apis-arkui/arkui-ts/ts-custom-component-api.md#getdialogcontroller18) is used to obtain the dialog controller, if the current custom component is not displayed in a dialog box, the result will be **undefined**.
14
15## Creating a Dialog Box Controller with ComponentContent as Custom Content
16
17> **NOTE**
18>
19> For details about the variables, see [Example](#example).
20
211. Initialize a parameter class for the custom dialog box content area, which includes the dialog controller.
22
23  ```ts
24  class Params {
25    public text: string = ''
26    public dialogController: promptAction.CommonController = new promptAction.DialogController()
27    constructor(text: string, dialogController: promptAction.CommonController) {
28      this.text = text
29      this.dialogController = dialogController
30    }
31  }
32  ```
33
342. Initialize a custom dialog box content area that includes a button, which closes the dialog box using the dialog controller provided by the custom component.
35
36  ```ts
37  @Component
38  struct MyComponent {
39    build() {
40      Column({ space: 5 }) {
41        Button('Close Dialog Box: Using Built-in DialogController')
42          .onClick(() => {
43            let dialogController: promptAction.DialogController = this.getDialogController()
44            if (dialogController !== undefined) {
45              dialogController.close()
46            }
47          })
48      }
49    }
50  }
51  ```
52
533. Initialize another custom dialog box content area that includes a **Text** component and a button, which closes the dialog box using the externally passed dialog controller. This content area also includes the previous custom dialog box content area.
54
55  ```ts
56  @Builder
57  function buildText(params: Params) {
58    Column({ space: 5 }) {
59      Text(params.text)
60        .fontSize(30)
61      if (params.dialogController !== undefined) {
62        Button('Close Dialog Box: Using Externally Passed DialogController')
63          .onClick(() => {
64            params.dialogController.close()
65          })
66      }
67      MyComponent()
68    }
69    .width(300)
70    .height(200)
71    .backgroundColor('#FFF0F0F0')
72  }
73  ```
74
754. Initialize a dialog controller and create a dialog box content entity object by setting the **controller** parameter. Finally, obtain a [PromptAction](../reference/apis-arkui/js-apis-arkui-UIContext.md#promptaction) object through the [getPromptAction](../reference/apis-arkui/js-apis-arkui-UIContext.md#getpromptaction) API of [UIContext](../reference/apis-arkui/js-apis-arkui-UIContext.md#uicontext), and call the [openCustomDialogWithController](../reference/apis-arkui/js-apis-arkui-UIContext.md#opencustomdialogwithcontroller18) API with the initialized content entity object and **controller** parameter to create the dialog box.
76
77  ```ts
78  let dialogController: promptAction.CommonController = new promptAction.DialogController()
79  let contentNode: ComponentContent<Object> =
80    new ComponentContent(this.getUIContext(), wrapBuilder(buildText), new Params(this.message, dialogController))
81  this.getUIContext().getPromptAction().openCustomDialogWithController(
82    contentNode, dialogController, this.baseDialogOptions).catch((err: BusinessError) => {
83    console.error('openCustomDialogWithController error: ' + err.code + ' ' + err.message)
84  })
85  ```
86
87## Creating a Dialog Box Controller with CustomBuilder as Custom Content
88
89> **NOTE**
90>
91> For details about the variables, see [Example](#example).
92
931. Initialize a custom dialog box content area that includes a **Text** component and a button, which closes the dialog box using the externally passed dialog controller.
94
95  ```ts
96  @Builder customDialogComponent(dialogController: promptAction.DialogController) {
97    Column({ space: 5 }) {
98      Text(this.message)
99        .fontSize(30)
100      if (dialogController !== undefined) {
101        Button('Close Dialog Box: Using Externally Passed DialogController')
102          .onClick(() => {
103            dialogController.close()
104          })
105      }
106    }
107    .height(200)
108    .padding(5)
109    .justifyContent(FlexAlign.SpaceBetween)
110    .backgroundColor('#FFF0F0F0')
111  }
112  ```
113
1142. Initialize a dialog box controller and obtain a [PromptAction](../reference/apis-arkui/js-apis-arkui-UIContext.md#promptaction) object through the [getPromptAction](../reference/apis-arkui/js-apis-arkui-UIContext.md#getpromptaction) API of [UIContext](../reference/apis-arkui/js-apis-arkui-UIContext.md#uicontext), and call the [presentCustomDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#presentcustomdialog18) API with the initialized content entity object and **controller** parameter to create the dialog box.
115
116  ```ts
117  let dialogController: promptAction.CommonController = new promptAction.DialogController()
118  this.getUIContext().getPromptAction().presentCustomDialog(() => {
119    this.customDialogComponent(dialogController)
120  }, dialogController, this.dialogOptions).catch((err: BusinessError) => {
121    console.error('presentCustomDialog error: ' + err.code + ' ' + err.message)
122  })
123  ```
124
125## Creating a Dialog Box Controller with CustomBuilderWithId as Custom Content
126
127> **NOTE**
128>
129> For details about the variables, see [Example](#example).
130
1311. Initialize a dialog box content area that includes a **Text** component, a button that closes the dialog box using the externally passed dialog ID, and a button that closes the dialog box using the externally passed dialog controller.
132
133  ```ts
134  @Builder customDialogComponentWithId(dialogId: number, dialogController: promptAction.DialogController) {
135    Column({ space: 5 }) {
136      Text(this.message)
137        .fontSize(30)
138      if (dialogId !== undefined) {
139        Button('Close Dialog Box: Using DialogID')
140          .onClick(() => {
141            this.getUIContext().getPromptAction().closeCustomDialog(dialogId)
142          })
143      }
144      if (dialogController !== undefined) {
145        Button('Close Dialog Box: Using Externally Passed DialogController')
146          .onClick(() => {
147            dialogController.close()
148          })
149      }
150    }
151  }
152  ```
153
1542. Initialize a dialog box controller and obtain a [PromptAction](../reference/apis-arkui/js-apis-arkui-UIContext.md#promptaction) object through the [getPromptAction](../reference/apis-arkui/js-apis-arkui-UIContext.md#getpromptaction) API of [UIContext](../reference/apis-arkui/js-apis-arkui-UIContext.md#uicontext), and call the [presentCustomDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#presentcustomdialog18) API with the initialized content entity object and **controller** parameter to create the dialog box.
155
156  ```ts
157  let dialogController: promptAction.CommonController = new promptAction.DialogController()
158  this.getUIContext().getPromptAction().presentCustomDialog((dialogId: number) => {
159    this.customDialogComponentWithId(dialogId, dialogController)
160  }, dialogController, this.dialogOptions).catch((err: BusinessError) => {
161    console.error('presentCustomDialog error: ' + err.code + ' ' + err.message)
162  })
163  ```
164
165## Directly Obtaining the Dialog Box Controller in the CustomDialogController Content Area
166
167> **NOTE**
168>
169> For details about the variables, see [Example](#example).
170
1711. Initialize a custom dialog box content area that includes a **Text** component and a button, which closes the dialog box using the dialog controller.
172
173```ts
174@CustomDialog
175@Component
176struct CustomDialogExample {
177  controller?: CustomDialogController
178
179  build() {
180    Column({ space: 5 }) {
181      Text('I am content')
182        .fontSize(30)
183      Button('Close Dialog Box: Using Built-in DialogController')
184        .onClick(() => {
185          let dialogController: PromptActionDialogController = this.getDialogController()
186          if (dialogController !== undefined) {
187            dialogController.close()
188          }
189        })
190    }
191    .height(200)
192    .backgroundColor('#FFF0F0F0')
193  }
194}
195```
196
1972. Initialize a custom dialog box constructor and associate it with the custom dialog box content area.
198
199  ```ts
200  let customDialogController: CustomDialogController = new CustomDialogController({
201    builder: CustomDialogExample(),
202  })
203  customDialogController.open()
204  ```
205
206## Example
207The example demonstrates how to use both the externally passed dialog controller and the dialog controller provided by the custom component to close the dialog box within the custom dialog box content area.
208  ```ts
209  import { ComponentContent, promptAction } from '@kit.ArkUI'
210  import { BusinessError } from '@kit.BasicServicesKit'
211
212  class Params {
213    public text: string = ''
214    public dialogController: promptAction.CommonController = new promptAction.DialogController()
215    constructor(text: string, dialogController: promptAction.CommonController) {
216      this.text = text
217      this.dialogController = dialogController
218    }
219  }
220
221  @Component
222  struct MyComponent {
223    build() {
224      Column({ space: 5 }) {
225        Button('Close Dialog Box: Using Built-in DialogController')
226          .onClick(() => {
227            let dialogController: promptAction.DialogController = this.getDialogController()
228            if (dialogController !== undefined) {
229              dialogController.close()
230            }
231          })
232      }
233    }
234  }
235
236  @Builder
237  function buildText(params: Params) {
238    Column({ space: 5 }) {
239      Text(params.text)
240        .fontSize(30)
241      if (params.dialogController !== undefined) {
242        Button('Close Dialog Box: Using Externally Passed DialogController')
243          .onClick(() => {
244            params.dialogController.close()
245          })
246      }
247      MyComponent()
248    }
249    .width(300)
250    .height(200)
251    .backgroundColor('#FFF0F0F0')
252  }
253
254  @CustomDialog
255  @Component
256  struct CustomDialogExample {
257    controller?: CustomDialogController
258
259    build() {
260      Column({ space: 5 }) {
261        Text('I am content')
262          .fontSize(30)
263        Button('Close Dialog Box: Using Built-in DialogController')
264          .onClick(() => {
265            let dialogController: PromptActionDialogController = this.getDialogController()
266            if (dialogController !== undefined) {
267              dialogController.close()
268            }
269          })
270      }
271      .height(200)
272      .backgroundColor('#FFF0F0F0')
273    }
274  }
275
276  @Entry
277  @Component
278  export struct Index {
279    private message = 'Dialog box'
280    @Builder customDialogComponent(dialogController: promptAction.DialogController) {
281      Column({ space: 5 }) {
282        Text(this.message)
283          .fontSize(30)
284        if (dialogController !== undefined) {
285          Button('Close Dialog Box: Using Externally Passed DialogController')
286            .onClick(() => {
287              dialogController.close()
288            })
289        }
290      }
291      .height(200)
292      .padding(5)
293      .justifyContent(FlexAlign.SpaceBetween)
294      .backgroundColor('#FFF0F0F0')
295    }
296
297    @Builder customDialogComponentWithId(dialogId: number, dialogController: promptAction.DialogController) {
298      Column({ space: 5 }) {
299        Text(this.message)
300          .fontSize(30)
301        if (dialogId !== undefined) {
302          Button('Close Dialog Box: Using DialogID')
303            .onClick(() => {
304              this.getUIContext().getPromptAction().closeCustomDialog(dialogId)
305            })
306        }
307        if (dialogController !== undefined) {
308          Button('Close Dialog Box: Using Externally Passed DialogController')
309            .onClick(() => {
310              dialogController.close()
311            })
312        }
313      }
314    }
315
316    private baseDialogOptions: promptAction.BaseDialogOptions = {
317      isModal: false,
318      autoCancel: false
319    }
320
321    private dialogOptions: promptAction.DialogOptions = {
322      isModal: false,
323      autoCancel: false
324    }
325
326    build() {
327      Column({ space: 5 }) {
328        Button('openCustomDialogWithController')
329          .onClick(() => {
330            let dialogController: promptAction.CommonController = new promptAction.DialogController()
331            let contentNode: ComponentContent<Object> =
332              new ComponentContent(this.getUIContext(), wrapBuilder(buildText), new Params(this.message, dialogController))
333            this.getUIContext().getPromptAction().openCustomDialogWithController(
334              contentNode, dialogController, this.baseDialogOptions).catch((err: BusinessError) => {
335              console.error('openCustomDialogWithController error: ' + err.code + ' ' + err.message)
336            })
337          })
338        Button('presentCustomDialog+CustomBuilder')
339          .onClick(() => {
340            let dialogController: promptAction.CommonController = new promptAction.DialogController()
341            this.getUIContext().getPromptAction().presentCustomDialog(() => {
342              this.customDialogComponent(dialogController)
343            }, dialogController, this.dialogOptions).catch((err: BusinessError) => {
344              console.error('presentCustomDialog error: ' + err.code + ' ' + err.message)
345            })
346          })
347        Button('presentCustomDialog+CustomBuilderWithId')
348          .onClick(() => {
349            let dialogController: promptAction.CommonController = new promptAction.DialogController()
350            this.getUIContext().getPromptAction().presentCustomDialog((dialogId: number) => {
351              this.customDialogComponentWithId(dialogId, dialogController)
352            }, dialogController, this.dialogOptions).catch((err: BusinessError) => {
353              console.error('presentCustomDialog error: ' + err.code + ' ' + err.message)
354            })
355          })
356        Button('CustomDialogController')
357          .onClick(() => {
358            let customDialogController: CustomDialogController = new CustomDialogController({
359              builder: CustomDialogExample(),
360            })
361            customDialogController.open()
362          })
363      }.width('100%')
364    }
365  }
366  ```
367
368