目录

@ohos.arkui.dialog (Dialog Box)

To meet the requirements of flexibly configuring and managing dialog boxes in applications, this module provides unified Dialog type declarations, including dialog box options, button configuration, worksheet items, and enums such as the dialog box controller, alignment, and state. To call specific APIs, use the DialogPresenter object in UIContext.

To show, update, and close a dialog box, obtain the DialogPresenter object through the getDialogPresenter() API in UIContext, and then call the related API.

Since: 26.0.1

Modules to Import

import { dialog } from '@kit.ArkUI';

DialogTextStyleOptions

Defines text style options in a dialog box, which can be used as the text style of the message content.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
wordBreak WordBreak No Yes

Word breaking type.

Default value: WordBreak.BREAK_ALL

DialogButton

Defines the button configuration of a fixed-style dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
value ResourceStr No No Text content of the button.
fontColor ResourceColor No Yes

Font color of the button.

Default value: follows the system theme color.

backgroundColor ResourceColor No Yes

Background color of the button.

Default value: follows the system theme color.

enabled boolean No Yes

Whether to respond when the button is clicked. The value true means to respond, and false means not to respond.

Default value: true

primary boolean No Yes

Whether the button responds to the Enter/Space key by default. The value true means to respond by default, and false means not to respond by default.

Default value: false

style DialogButtonStyle No Yes

Style of the button.

Default value: DialogButtonStyle.DEFAULT

action VoidCallback No No Callback invoked when the button is clicked.

DialogSheet

Defines the configuration item of a dialog box in the ActionSheet style.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
title ResourceStr No No Title content.
icon ResourceStr No Yes

Icon content.

Default value: the value is empty.

action VoidCallback No No Callback invoked when the item is clicked.

DialogBaseOptions

Provides basic options shared by all dialog boxes, defining common attributes such as the background, border, alignment, mask, and avoidance of a dialog box. Both DialogStyleOptions and DialogCustomOptions are inherited from this API.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
controller DialogBaseController No Yes Dialog box controller.
width Dimension No Yes

Width of the dialog box.

Default value: adaptive to the content.

height Dimension No Yes

Height of the dialog box.

Default value: adaptive to the content.

backgroundColor ResourceColor No Yes

Background color of the dialog box.

Default value: Color.Transparent

Note: When backgroundColor is set to a non-transparent color, backgroundBlurStyle must be set to BlurStyle.NONE.

backgroundBlurStyle BlurStyle No Yes

Background blur style of the dialog box.

Default value: BlurStyle.COMPONENT_ULTRA_THICK

Note: Set this parameter to BlurStyle.NONE to disable the background blur. When backgroundBlurStyle is set to a value other than BlurStyle.NONE, do not set backgroundColor; otherwise, the color display will not meet expectations. When systemMaterial is set, backgroundBlurStyle does not take effect.

backgroundBlurStyleOptions BackgroundBlurStyleOptions No Yes Background blur style with items. The default value is the same as that in the BackgroundBlurStyleOptions type description.
backgroundEffect BackgroundEffectOptions No Yes Background effect with items. The default value is the same as that in the BackgroundEffectOptions type description.
borderRadius Dimension | BorderRadiuses | LocalizedBorderRadiuses No Yes

Corner radius of the background border.

Default value: { topLeft: '32vp', topRight: '32vp', bottomLeft: '32vp', bottomRight: '32vp' }

borderWidth Dimension | EdgeWidths | LocalizedEdgeWidths No Yes

Border width of the dialog box.

Default value: 0

borderColor ResourceColor | EdgeColors | LocalizedEdgeColors No Yes

Border color of the dialog box.

Default value: Color.Black

borderStyle BorderStyle | EdgeStyles No Yes

Border style of the dialog box.

Default value: BorderStyle.Solid

shadow ShadowOptions | ShadowStyle No Yes

Shadow of the dialog box.

On PCs/2-in-1 devices, the default shadow value is ShadowStyle.OUTER_FLOATING_MD when the component gains focus and ShadowStyle.OUTER_FLOATING_SM when the component loses focus. Other devices have no shadow by default.

alignment DialogBaseAlignment No Yes

Alignment mode of the dialog box.

Default value: DialogBaseAlignment.DEFAULT

offset Offset No Yes

Offset of the dialog box relative to the alignment position.

Default value: { dx: 0, dy: 0 }

maskRect Rectangle No Yes

Mask area of the dialog box.

Default value: { x: 0, y: 0, width: '100%', height: '100%' }

maskColor ResourceColor No Yes

Mask color of the dialog box.

Default value: follows the system theme color.

isModal boolean No Yes

Whether the dialog box is modal. The value true indicates that it is modal and has a mask, and the value false indicates that it is non-modal and has no mask.

Default value: true

showInSubWindow boolean No Yes

Whether to display the dialog box in a subwindow. The value true indicates that it is displayed in a subwindow, and the value false indicates that it is displayed in the application.

Default value: false

Note: isModal set to true and showInSubWindow set to true cannot be used at the same time.

displayModeInSubWindow DialogDisplayMode No Yes

Display mode of the dialog box when displayed in a subwindow.

Default value: DialogDisplayMode.SCREEN_BASED

autoCancel boolean No Yes

Whether to allow exiting by touching the mask or pressing the Back key. The value true indicates to allow the exiting, and false indicates the opposite.

Default value: true

focusable boolean No Yes

Whether the dialog box is focusable. The value true indicates that the dialog box is focusable, and false indicates the opposite.

Default value: true

dialogTransition TransitionEffect No Yes

Transition effect for opening/closing the content area of the dialog box.

Default value: the default transition effect of the system.

maskTransition TransitionEffect No Yes

Transition effect for opening/closing the mask.

Default value: the default mask transition effect of the system.

keyboardAvoidMode KeyboardAvoidMode No Yes

Keyboard avoidance mode.

Default value: KeyboardAvoidMode.DEFAULT

keyboardAvoidDistance LengthMetrics No Yes

Distance between the dialog box and the system keyboard.

Default value: the default avoidance distance of the system.

onWillAppear VoidCallback No Yes Callback invoked before the dialog box opening animation starts.
onDidAppear VoidCallback No Yes Callback invoked when the dialog box appears.
onWillDisappear VoidCallback No Yes Callback invoked before the dialog box closing animation starts.
onDidDisappear VoidCallback No Yes Callback invoked when the dialog box disappears.
onWillDismiss Callback<DialogDismissal> No Yes

Callback invoked when the dialog box is dismissed by interaction.

Note: If this callback is registered, the dialog box will not be closed immediately after the user taps the mask or presses the Back button. The reason parameter in the callback is used to determine whether the dialog box can be closed.

enableHoverMode boolean No Yes

Whether to enable the hover mode. The value true indicates to enable the hover mode, and false indicates the opposite.

Default value: false

hoverModeArea HoverModeAreaType No Yes

Display area of the dialog box in hover mode.

Default value: HoverModeAreaType.BOTTOM_SCREEN

levelMode LevelMode No Yes

Display level of the dialog box.

Default value: LevelMode.OVERLAY

levelUniqueId number No Yes

Unique ID of the node under the display layer of the page-level dialog box.

Value range: an integer greater than or equal to 0.

Note: This parameter takes effect only when levelMode is set to LevelMode.EMBEDDED.

immersiveMode ImmersiveMode No Yes

Mask effect of the page-level dialog box.

Default value: ImmersiveMode.DEFAULT

Note: This parameter takes effect only when levelMode is set to LevelMode.EMBEDDED.

levelOrder LevelOrder No Yes

Display order of the dialog box.

Default value: the value returned by LevelOrder.clamp(0).

systemMaterial SystemUiMaterial No Yes

System material for the dialog box. Different materials have different effects and affect the background color, border, shadow, and other visual attributes of the dialog box.

Note:

- Default value: the ImmersiveMaterial object whose style of [ImmersiveOptions] is set to ImmersiveStyle.ULTRA_THICK. When this parameter is set to undefined, it has the same effect as the default value.

- This parameter affects the backgroundColor, backgroundBlurStyle, backgroundEffect, borderColor, borderWidth, and shadow attributes. When the system material is set, the preceding attributes do not take effect.

DialogMessage

Defines the message content and text style of the dialog box. This API is inherited from DialogTextStyleOptions.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
content ResourceStr No No Message content of the dialog box.

DialogStyleOptions

Defines the options for a dialog box with a fixed style. This API is inherited from DialogBaseOptions. For specific usage, see the present API example of DialogPresenter.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
title ResourceStr No Yes

Title of the dialog box.

Default value: no title is displayed when not set.

subtitle ResourceStr No Yes

Subtitle of the dialog box.

Default value: no subtitle is displayed when not set.

message DialogMessage No Yes

Message content and text style of the dialog box.

Default value: no message content is displayed when not set.

buttons Array<DialogButton> No Yes

Array of buttons in the dialog box. When provided, the dialog box is displayed as an alert-style dialog box with buttons; when used together with sheets, the buttons are displayed below the worksheet item list.

Default value: no buttons are displayed when not set.

buttonDirection DialogButtonOrientation No Yes

Arrangement of the buttons.

Default value: DialogButtonOrientation.AUTO

sheets Array<DialogSheet> No Yes

Array of worksheet items in the action-sheet style. When provided, the dialog box displays worksheet items for the user to select.

Default value: no worksheet items are displayed when not set.

gridCount number No Yes

Number of grid columns for the worksheet items in the dialog box, used to control the column layout of worksheet items in the grid.

Default value: 4

Value range: an integer greater than 0.

DialogCustomOptions

Defines the options for a custom-style dialog box. This API is inherited from DialogBaseOptions.

The content of the dialog box is provided by the first parameter of the DialogPresenter.present API, not in this option object. For specific usage, see the present API example of DialogPresenter.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
customStyle boolean No Yes

Whether to enable the custom style. The value true means to enable the custom style, and false means the opposite.

Default value: false

DialogBaseController

A class used to control a dialog box. It can be bound to a dialog box through the controller attribute in DialogBaseOptions. For specific usage, see the present API example of DialogPresenter.

[h2]constructor

constructor()

A constructor of the controller.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

[h2]close

close(): void

Closes the corresponding dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

[h2]getState

getState(): DialogState

Obtains the state of the dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
DialogState State of the dialog box.

Example

This example demonstrates how to show a dialog box by binding DialogBaseController to the dialog box, close the dialog box through the controller, and obtain the state of the dialog box.

import { DialogBaseController, DialogPresenter, DialogState } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  private ctx: UIContext = this.getUIContext();
  private dialogPresenter: DialogPresenter = this.ctx.getDialogPresenter();
  private controller: DialogBaseController = new DialogBaseController();
  @State dialogState: DialogState = DialogState.UNINITIALIZED;

  @Builder
  customDialogComponent() {
    Column() {
      Text('Dialog').fontSize(20)
      Column({ space: 10 }) {
        Button('Close dialog via controller').onClick(() => {
          try {
            this.controller.close();
          } catch (error) {
            let message = (error as BusinessError).message;
            let code = (error as BusinessError).code;
            console.error(`Failed to close dialog. Code: ${code}, message: ${message}`);
          }
        })
        Button('Get dialog state')
          .onClick(() => {
            this.dialogState = this.controller.getState();
            console.info('dialog state: ' + this.dialogState);
          })
      }
    }.height(150).padding(20).justifyContent(FlexAlign.SpaceBetween)
  }

  build() {
    Column({ space: 50 }) {
      Text(`Dialog state: ${this.dialogState}`)
      Button('Present dialog')
        .onClick(() => {
          this.dialogPresenter.present(() => { this.customDialogComponent() },
          {
            controller: this.controller,
          })
            .catch((error: BusinessError) => {
              console.error(`Failed to present dialog. Code: ${error.code}, message: ${error.message}`);
            })
        })
      Button('Get dialog state')
        .onClick(() => {
          this.dialogState = this.controller.getState();
          console.info('dialog state: ' + this.dialogState);
        })
    }.width('100%').height('100%').justifyContent(FlexAlign.Start)
  }
}

DialogResult

Provides the response result of the dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
dialogId number No No

ID of the dialog box.

Value range: an integer greater than or equal to 0.

DialogDismissal

Provides the information and API for dismissing a dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read-Only Optional Description
dismiss VoidCallback No No Callback for dismissing the dialog box. This callback is invoked only when the dialog box needs to be dismissed.
reason DismissReason No No Type of the reason that triggers the dialog box dismissal.

DialogBaseAlignment

Defines the alignment mode of the dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
TOP 0 Vertically top aligned.
CENTER 1 Vertically centered.
BOTTOM 2 Vertically bottom aligned.
DEFAULT 3 Default alignment.
TOP_START 4 Top-left aligned.
TOP_END 5 Top-right aligned.
CENTER_START 6 Left-centered.
CENTER_END 7 Right-centered.
BOTTOM_START 8 Bottom-left aligned.
BOTTOM_END 9 Bottom-right aligned.

DialogButtonOrientation

Defines the arrangement of buttons in a dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
AUTO 0 Buttons are arranged horizontally when there are two or fewer buttons, and vertically when there are more than two buttons.
HORIZONTAL 1 Buttons are arranged horizontally.
VERTICAL 2 Buttons are arranged vertically.

DialogState

Enumerates the states of a dialog box.

Since: 26.0.1

Atomic service API: This API can be used in atomic services since API version 26.0.1.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
UNINITIALIZED 0 Not initialized.
INITIALIZED 1 Initialized.
APPEARING 2 Appearing.
APPEARED 3 Appeared.
DISAPPEARING 4 Disappearing.
DISAPPEARED 5 Disappeared.