1# Custom Dialog Box 2 3A custom dialog box is a dialog box you customize by using APIs of the **CustomDialogController** class. You can set the style and content to your preference for a custom dialog box. 4 5> **NOTE** 6> 7> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10 11 12## APIs 13 14CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean, gridCount?: number, maskColor?: ResourceColor, openAnimation?: AnimateParam, closeAniamtion?: AnimateParam, showInSubWindow?: boolean}) 15 16 17**Parameters** 18 19| Name | Type | Mandatory | Description | 20| ----------------------------- | ---------------------------------------- | ---- | ---------------------------------------- | 21| builder | CustomDialog | Yes | Constructor of the custom dialog box content. | 22| cancel | () => void | No | Callback invoked when the dialog box is closed after the overlay exits. | 23| autoCancel | boolean | No | Whether to allow users to click the overlay to exit.<br>Default value: **true** | 24| alignment | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment) | No | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**| 25| offset | [Offset](ts-types.md#offset) | No | Offset of the dialog box relative to the alignment position. | 26| customStyle | boolean | No | Whether to use a custom style for the dialog box.<br>Default value: **false**, which means that the dialog box automatically adapts its width to the grid system and its height to the child components; the maximum height is 90% of the container height; the rounded corner is 24 vp.| 27| gridCount<sup>8+</sup> | number | No | Number of [grid columns](../../ui/arkts-layout-development-grid-layout.md) occupied by the dialog box.<br>The default value is 4, and the maximum value is the maximum number of columns supported by the system. If this parameter is set to an invalid value, the default value is used.| 28 29## CustomDialogController 30 31### Objects to Import 32 33```ts 34dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean}) 35``` 36> **NOTE** 37> 38> **CustomDialogController** is valid only when it is a member variable of the **@CustomDialog** and **@Component** decorated struct and is defined in the **@Component** decorated struct. For details, see the following example. 39 40### open() 41open(): void 42 43 44Opens the content of the custom dialog box. If the content has been displayed, this API does not take effect. 45 46 47### close 48close(): void 49 50 51Closes the custom dialog box. If the dialog box is closed, this API does not take effect. 52 53 54## Example 55 56```ts 57// xxx.ets 58@CustomDialog 59struct CustomDialogExample { 60 @Link textValue: string 61 @Link inputValue: string 62 controller: CustomDialogController 63 cancel: () => void 64 confirm: () => void 65 66 build() { 67 Column() { 68 Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) 69 TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%') 70 .onChange((value: string) => { 71 this.textValue = value 72 }) 73 Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 }) 74 Flex({ justifyContent: FlexAlign.SpaceAround }) { 75 Button('cancel') 76 .onClick(() => { 77 this.controller.close() 78 this.cancel() 79 }).backgroundColor(0xffffff).fontColor(Color.Black) 80 Button('confirm') 81 .onClick(() => { 82 this.inputValue = this.textValue 83 this.controller.close() 84 this.confirm() 85 }).backgroundColor(0xffffff).fontColor(Color.Red) 86 }.margin({ bottom: 10 }) 87 } 88 } 89} 90 91@Entry 92@Component 93struct CustomDialogUser { 94 @State textValue: string = '' 95 @State inputValue: string = 'click me' 96 dialogController: CustomDialogController = new CustomDialogController({ 97 builder: CustomDialogExample({ 98 cancel: this.onCancel, 99 confirm: this.onAccept, 100 textValue: $textValue, 101 inputValue: $inputValue 102 }), 103 cancel: this.existApp, 104 autoCancel: true, 105 alignment: DialogAlignment.Default, 106 offset: { dx: 0, dy: -20 }, 107 gridCount: 4, 108 customStyle: false 109 }) 110 111 aboutToDisappear() { 112 delete this.dialogController, 113 this.dialogController = undefined 114 } 115 116 onCancel() { 117 console.info('Callback when the first button is clicked') 118 } 119 120 onAccept() { 121 console.info('Callback when the second button is clicked') 122 } 123 124 existApp() { 125 console.info('Click the callback in the blank area') 126 } 127 128 build() { 129 Column() { 130 Button(this.inputValue) 131 .onClick(() => { 132 if (this.dialogController != undefined) { 133 this.dialogController.open() 134 } 135 }).backgroundColor(0x317aff) 136 }.width('100%').margin({ top: 5 }) 137 } 138} 139``` 140 141 142