目录

Creating an Atomic Service Widget

  1. Create a service widget for an atomic service.
    • In the Project window, right-click entry and choose New > Service Widget > Dynamic Widget from the shortcut menu. A page is displayed for you to choose a service widget template, as shown in the following figure.

    • Select the Hello World template and click Next. The widget configuration page is displayed.

      • Service widget name: widget name, which must be unique in an app/service and can contain only uppercase letters, lowercase letters, digits, and underscores (_).
      • Display name: widget name displayed on the preview panel. This field can be configured only for stage model projects of API version 11 or later.
      • Description: description of the widget.
      • Language: programming language of the widget, which can be ArkTS or JS.

        An atomic service supports only ArkTS service widgets. JS service widgets are not supported.

      • Support dimensions: dimensions of the widget. Certain types of widgets allow for multiple dimensions options. When a service widget is created for the first time, an EntryCard folder is automatically generated for storing widget snapshots.
      • Default dimension: default dimension of the widget.
      • Ability name: FormAbility bound to the widget. You can select an existing one or create a new one.
      • Module name: module to which the widget belongs.
    • Retain the default settings and click Finish.
  2. Add a button to the user interface (UI) of the default widget and configure animation effects for the button.

    In the WidgetCard.ets file, delete the widget code generated by default and add the following sample code:

    @Entry
    @Component
    struct WidgetCard {
      @State x: number = 1
      @State y: number = 1
    
      build() {
        Column() {
          Button('Click to enlarge')
            .onClick(() => {
              this.x = 1.1
              this.y = 1.1
            })
            .scale({ x: this.x, y: this.y })
            .animation({
              curve: Curve.EaseOut,
              playMode: PlayMode.AlternateReverse,
              duration: 200,
              onFinish: () => {
                this.x = 1
                this.y = 1
              }
            })
        }
        .padding('10vp')
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
      }
    }
  3. Preview the animation effect of the service widget.
    Open the WidgetCard.ets file and click in the Previewer to refresh the preview. The figure below shows the effect.
    Figure 1 Animation effect
  4. Click the widget to go to the home page and access the atomic service. Add the following sample code to the widget so that you can click the blank area on the widget to access the atomic service.
    @Entry
    @Component
    struct WidgetCard {
      @State x: number = 1
      @State y: number = 1
    
      build() {
        Column() {
          Button('Click to enlarge')
            .onClick(() => {
              this.x = 1.1
              this.y = 1.1
            })
            .scale({ x: this.x, y: this.y })
            .animation({
              curve: Curve.EaseOut,
              playMode: PlayMode.AlternateReverse,
              duration: 200,
              onFinish: () => {
                this.x = 1
                this.y = 1
              }
            })
        }
        .padding('10vp')
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(() => {
          postCardAction(this, {
            "action": 'router',
            "abilityName": 'EntryAbility',
            "params": {
              "message": 'router test'
            }
          });
        })
      }
    }
  5. Replace widget-2x2.png in EntryCard > entry > base > snapshot with the custom snapshot of the atomic service widget.