1# Slider 2 3The **Slider** component is used to quickly adjust settings, such as the volume and brightness. 4 5> **NOTE** 6> 7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12Not supported 13 14 15## APIs 16 17Slider(options?: SliderOptions) 18 19**Widget capability**: This API can be used in ArkTS widgets since API version 9. 20 21**Atomic service API**: This API can be used in atomic services since API version 11. 22 23**System capability**: SystemCapability.ArkUI.ArkUI.Full 24 25**Parameters** 26 27| Name | Type | Mandatory | Description | 28| ------- | --------------------------------------- | ---- | ------------------ | 29| options | [SliderOptions](#slideroptions) | No | Parameters of the slider. | 30 31## SliderOptions 32 33**Widget capability**: This API can be used in ArkTS widgets since API version 9. 34 35**Atomic service API**: This API can be used in atomic services since API version 11. 36 37| Name | Type | Mandatory | Description | 38| -------- | -------- | -------- | -------- | 39| value | number | No | Current progress.<br>Default value: same as the value of **min**<br>Since API version 10, this parameter supports two-way binding through [$$](../../../quick-start/arkts-two-way-sync.md). | 40| min | number | No | Minimum value.<br>Default value: **0** | 41| max | number | No | Maximum value.<br>Default value: **100**<br>**NOTE**<br>If the value of **min** is greater than or equal to the value of **max**, the **min** value defaults to **0**, and the **max** value defaults to **100**.<br>If the value is not within the [min, max] range, the value of **min** or **max** is used, whichever is closer. | 42| step | number | No | Step of the slider.<br>Default value: **1**<br>Value range: [0.01, max]<br>**NOTE**<br>If this parameter is set to a value less than 0 or greater than the value of **max**, the default value is used. | 43| style | [SliderStyle](#sliderstyle) | No | Style of the slider thumb and track.<br>Default value: **SliderStyle.OutSet** | 44| direction<sup>8+</sup> | [Axis](ts-appendix-enums.md#axis) | No | Whether the slider moves horizontally or vertically.<br>Default value: **Axis.Horizontal** | 45| reverse<sup>8+</sup> | boolean | No | Whether the slider values are reversed. By default, the values increase from left to right for a horizontal slider and from top to bottom for a vertical slider.<br>Default value: **false** | 46 47## SliderStyle 48 49**Widget capability**: This API can be used in ArkTS widgets since API version 9. 50 51| Name | Description | 52| -------- | -------- | 53| OutSet | The thumb is on the track.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 54| InSet | The thumb is in the track.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 55| NONE<sup>12+</sup> | There is no thumb.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 56 57## Attributes 58 59All the [universal attributes](ts-universal-attributes-size.md) except **responseRegion** are supported 60 61### blockColor 62 63blockColor(value: ResourceColor) 64 65Color of the thumb. 66 67When **SliderBlockType.DEFAULT** is used, **blockColor** sets the color of the round thumb. 68 69When **SliderBlockType.IMAGE** is used, **blockColor** does not work as the thumb has no fill color. 70 71When **SliderBlockType.SHAPE** is used, **blockColor** sets the color of the thumb in a custom shape. 72 73**Widget capability**: This API can be used in ArkTS widgets since API version 9. 74 75**Atomic service API**: This API can be used in atomic services since API version 11. 76 77**System capability**: SystemCapability.ArkUI.ArkUI.Full 78 79**Parameters** 80 81| Name | Type | Mandatory | Description | 82| ------ | ------------------------------------------ | ---- | ----------------------------------- | 83| value | [ResourceColor](ts-types.md#resourcecolor) | Yes | Color of the thumb.<br>Default value: **'#ffffff'** | 84 85### trackColor 86 87trackColor(value: ResourceColor | LinearGradient) 88 89Sets the background color of the track. 90 91**LinearGradient** is supported since API version 12. 92 93**Widget capability**: This API can be used in ArkTS widgets since API version 9. 94 95**Atomic service API**: This API can be used in atomic services since API version 11. 96 97**System capability**: SystemCapability.ArkUI.ArkUI.Full 98 99**Parameters** 100 101| Name | Type | Mandatory | Description | 102| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 103| value | [ResourceColor](ts-types.md#resourcecolor) \| [LinearGradient<sup>12+</sup>](ts-basic-components-datapanel.md#lineargradient10) | Yes | Background color of the track.<br>**NOTE**<br>With gradient color settings, if the color stop values are invalid or if the color stops are empty, the gradient effect will not be applied.<br>Default value: **'#19182431'**<br>The LinearGradient type cannot be used in atomic services. | 104 105### selectedColor 106 107selectedColor(value: ResourceColor) 108 109Sets the color of the portion of the track between the minimum value and the thumb. 110 111**Widget capability**: This API can be used in ArkTS widgets since API version 9. 112 113**Atomic service API**: This API can be used in atomic services since API version 11. 114 115**System capability**: SystemCapability.ArkUI.ArkUI.Full 116 117**Parameters** 118 119| Name | Type | Mandatory | Description | 120| ------ | ------------------------------------------ | ---- | --------------------------------------------- | 121| value | [ResourceColor](ts-types.md#resourcecolor) | Yes | Color of the portion of the track between the minimum value and the thumb.<br>Default value: **'#007dff'** | 122 123### showSteps 124 125showSteps(value: boolean) 126 127Sets whether to display the current step. 128 129**Widget capability**: This API can be used in ArkTS widgets since API version 9. 130 131**Atomic service API**: This API can be used in atomic services since API version 11. 132 133**System capability**: SystemCapability.ArkUI.ArkUI.Full 134 135**Parameters** 136 137| Name | Type | Mandatory | Description | 138| ------ | ------- | ---- | ------------------------------------------ | 139| value | boolean | Yes | Whether to display the current step.<br>Default value: **false** | 140 141### showTips 142 143showTips(value: boolean, content?: ResourceStr) 144 145Sets whether to display a tooltip when the user drags the slider. 146 147When **direction** is set to **Axis.Horizontal**, the tooltip is displayed right above the slider; if there is insufficient space above, it will be displayed below. When **direction** is set to **Axis.Vertical**, the tooltip is displayed on the left of the slider; if there is insufficient space on the left, it will be displayed on the right. If the margins are not set or are set to small values, the tooltip may be truncated. 148 149The drawing area of the tooltip is the overlay of the slider. 150 151**Widget capability**: This API can be used in ArkTS widgets since API version 9. 152 153**Atomic service API**: This API can be used in atomic services since API version 11. 154 155**System capability**: SystemCapability.ArkUI.ArkUI.Full 156 157**Parameters** 158 159| Name | Type | Mandatory | Description | 160| --------------------- | -------------------------------------- | ---- | ------------------------------------------ | 161| value | boolean | Yes | Whether to display a tooltip when the user drags the slider.<br>Default value: **false** | 162| content<sup>10+</sup> | [ResourceStr](ts-types.md#resourcestr) | No | Content of the tooltip. By default, the tooltip shows the current percentage value. | 163 164### trackThickness<sup>8+</sup> 165 166trackThickness(value: Length) 167 168Sets the thickness of the track. If the value is less than or equal to 0, the default value is used. 169 170To ensure [SliderStyle](#sliderstyle) works as expected for the thumb and track, [blockSize](#blocksize10) should increase or decrease proportionally with **trackThickness**. 171 172Specially, when **style** is **[SliderStyle](#sliderstyle).OutSet**, trackThickness: [blockSize](#blocksize10) = 1:4; when **style** is **[SliderStyle](#sliderstyle).InSet**, trackThickness: [blockSize](#blocksize10) = 5:3. 173 174In changing the value of **trackThickness**, if it or the value of [blockSize](#blocksize10) exceeds the width or height of the slider, the default value is used. (When **style** is **[SliderStyle](#sliderstyle).OutSet**, it is possible that only the value of [blockSize](#blocksize10) exceeds the height of the slider.) 175 176**Widget capability**: This API can be used in ArkTS widgets since API version 9. 177 178**Atomic service API**: This API can be used in atomic services since API version 11. 179 180**System capability**: SystemCapability.ArkUI.ArkUI.Full 181 182**Parameters** 183 184| Name | Type | Mandatory | Description | 185| ------ | ---------------------------- | ---- | ------------------------------------------------------------ | 186| value | [Length](ts-types.md#length) | Yes | Thickness of the track.<br>Default value: 4.0vp when **style** is set to **[SliderStyle](#sliderstyle).OutSet**; 20.0vp when **style** is set to **[SliderStyle](#sliderstyle).InSet** | 187 188### blockBorderColor<sup>10+</sup> 189 190blockBorderColor(value: ResourceColor) 191 192Border color of the slider in the block direction. 193 194When **SliderBlockType.DEFAULT** is used, **blockBorderColor** sets the border color of the round slider. 195 196When **SliderBlockType.IMAGE** is used, **blockBorderColor** does not work as the slider has no border. 197 198When **SliderBlockType.SHAPE** is used, **blockBorderColor** sets the border color of the slider in a custom shape. 199 200**Atomic service API**: This API can be used in atomic services since API version 11. 201 202**System capability**: SystemCapability.ArkUI.ArkUI.Full 203 204**Parameters** 205 206| Name | Type | Mandatory | Description | 207| ------ | ------------------------------------------ | ---- | -------------------------------------- | 208| value | [ResourceColor](ts-types.md#resourcecolor) | Yes | Border color of the slider in the block direction.<br>Default value: **'#00000000'** | 209 210### blockBorderWidth<sup>10+</sup> 211 212blockBorderWidth(value: Length) 213 214Border width of the slider in the block direction. 215 216When **SliderBlockType.DEFAULT** is used, **blockBorderWidth** sets the border width of the round slider. 217 218When **SliderBlockType.IMAGE** is used, **blockBorderWidth** does not work as the slider has no border. 219 220WWhen **SliderBlockType.SHAPE** is used, **blockBorderWidth** sets the border width of the slider in a custom shape. 221 222**Atomic service API**: This API can be used in atomic services since API version 11. 223 224**System capability**: SystemCapability.ArkUI.ArkUI.Full 225 226**Parameters** 227 228| Name | Type | Mandatory | Description | 229| ------ | ---------------------------- | ---- | -------------- | 230| value | [Length](ts-types.md#length) | Yes | Border width of the slider in the block direction. | 231 232### stepColor<sup>10+</sup> 233 234stepColor(value: ResourceColor) 235 236Step color. 237 238**Atomic service API**: This API can be used in atomic services since API version 11. 239 240**System capability**: SystemCapability.ArkUI.ArkUI.Full 241 242**Parameters** 243 244| Name | Type | Mandatory | Description | 245| ------ | ------------------------------------------ | ---- | ---------------------------------- | 246| value | [ResourceColor](ts-types.md#resourcecolor) | Yes | Step color.<br>Default value: **'#19182431'** | 247 248### trackBorderRadius<sup>10+</sup> 249 250trackBorderRadius(value: Length) 251 252Sets the radius of the rounded corner of the track. 253 254**Atomic service API**: This API can be used in atomic services since API version 11. 255 256**System capability**: SystemCapability.ArkUI.ArkUI.Full 257 258**Parameters** 259 260| Name | Type | Mandatory | Description | 261| ------ | ---------------------------- | ---- | -------------------------------- | 262| value | [Length](ts-types.md#length) | Yes | Radius of the rounded corner of the track.<br>Default value: **'2vp'** | 263 264### selectedBorderRadius<sup>12+</sup> 265 266selectedBorderRadius(value: Dimension) 267 268Set the corner radius of the selected (highlighted) part of the slider. 269 270**Atomic service API**: This API can be used in atomic services since API version 12. 271 272**System capability**: SystemCapability.ArkUI.ArkUI.Full 273 274**Parameters** 275 276| Name | Type | Mandatory | Description | 277| ------ | ---------------------------- | ---- | -------------------------------- | 278| value | [Dimension](ts-types.md#dimension10)| Yes | Corner radius of the selected part of the slider.<br>Default value: same as the corner radius of the background when **style** is set to **SliderStyle.InSet** or **SliderStyle.OutSet**; 0 when **style** is set to **SliderStyle.NONE**| 279 280### blockSize<sup>10+</sup> 281 282blockSize(value: SizeOptions) 283 284Size of the slider in the block direction. 285 286**Atomic service API**: This API can be used in atomic services since API version 11. 287 288**System capability**: SystemCapability.ArkUI.ArkUI.Full 289 290**Parameters** 291 292| Name | Type | Mandatory | Description | 293| ------ | -------------------------------------- | ---- | ------------------------------------------------------------ | 294| value | [SizeOptions](ts-types.md#sizeoptions) | Yes | Size of the slider in the block direction.<br>Default value: {width: 16, height: 16} when **style** is set to **[SliderStyle](#sliderstyle).OutSet**; {width: 12, height: 12} when **style** is set to **[SliderStyle](#sliderstyle).InSet**<br>If the set **blockSize** has different width and height values, the smaller value is taken. If one or both of the width and height values are less than or equal to 0, the default value is used instead. | 295 296### blockStyle<sup>10+</sup> 297 298blockStyle(value: SliderBlockStyle) 299 300Style of the slider in the block direction. 301 302**Atomic service API**: This API can be used in atomic services since API version 11. 303 304**System capability**: SystemCapability.ArkUI.ArkUI.Full 305 306**Parameters** 307 308| Name | Type | Mandatory | Description | 309| ------ | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 310| value | [SliderBlockStyle](#sliderblockstyle10) | Yes | Style of the slider in the block direction.<br>Default value: **SliderBlockType.DEFAULT**, indicating the round slider. | 311 312### stepSize<sup>10+</sup> 313 314stepSize(value: Length) 315 316Sets the step size (diameter). If the value is 0, the step size is not displayed. If the value is less than 0, the default value is used. 317 318**Atomic service API**: This API can be used in atomic services since API version 11. 319 320**System capability**: SystemCapability.ArkUI.ArkUI.Full 321 322**Parameters** 323 324| Name | Type | Mandatory | Description | 325| ------ | ---------------------------- | ---- | ------------------------------------- | 326| value | [Length](ts-types.md#length) | Yes | Step size (diameter).<br>Default value: **'4vp'** | 327 328### minLabel<sup>(deprecated)</sup> 329 330minLabel(value: string) 331 332Sets the minimum value. 333 334This API is deprecated since API version 9. You are advised to use **min** instead. 335 336**System capability**: SystemCapability.ArkUI.ArkUI.Full 337 338**Parameters** 339 340| Name | Type | Mandatory | Description | 341| ------ | ------ | ---- | -------- | 342| value | string | Yes | Minimum value. | 343 344### maxLabel<sup>(deprecated)</sup> 345 346maxLabel(value: string) 347 348Sets the maximum value. 349 350This API is deprecated since API version 9. You are advised to use **max** instead. 351 352**System capability**: SystemCapability.ArkUI.ArkUI.Full 353 354**Parameters** 355 356| Name | Type | Mandatory | Description | 357| ------ | ------ | ---- | -------- | 358| value | string | Yes | Maximum value. | 359 360> **NOTE** 361> 362> - By default, the slider has no padding. 363> - A horizontal slider is displayed in the center with the following default settings: 40 vp height, width equal with that of the parent container, 10 vp left and right margins not covered by any padding (if set). 364> - A vertical slider is displayed in the center with the following default settings: 40 vp width, height equal with that of the parent container, 6 vp top and bottom margins not covered by any padding (if set). 365 366### sliderInteractionMode<sup>12+</sup> 367 368sliderInteractionMode(value: SliderInteraction) 369 370Sets the interaction mode between the user and the slider. 371 372**Atomic service API**: This API can be used in atomic services since API version 12. 373 374**System capability**: SystemCapability.ArkUI.ArkUI.Full 375 376**Parameters** 377 378| Name | Type | Mandatory | Description | 379| ------ | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 380| value | [SliderInteraction](#sliderinteraction12) | Yes | Interaction mode between the user and the slider.<br> Default value: **SliderInteraction.SLIDE_AND_CLICK** | 381 382### minResponsiveDistance<sup>12+</sup> 383 384minResponsiveDistance(value: number) 385 386Sets the minimum distance required for the slider to respond. 387 388**Atomic service API**: This API can be used in atomic services since API version 12. 389 390**System capability**: SystemCapability.ArkUI.ArkUI.Full 391 392**Parameters** 393 394| Name | Type | Mandatory | Description | 395| ------ | ------- | ---- | ------------------------------------------ | 396| value | number | Yes | Minimum distance required for the slider to respond. The slider will only respond and move when the sliding distance exceeds this threshold.<br>**NOTE**<br>The unit is the same as that of **min** and **max**.<br>If the value is less than 0, greater than the result of (max - min), or invalid, the default value is used.<br>Default value: **0** | 397 398### contentModifier<sup>12+</sup> 399 400contentModifier(modifier: ContentModifier\<SliderConfiguration>) 401 402Creates a content modifier. 403 404**Atomic service API**: This API can be used in atomic services since API version 12. 405 406**System capability**: SystemCapability.ArkUI.ArkUI.Full 407 408**Parameters** 409 410| Name | Type | Mandatory | Description | 411| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 412| modifier | [ContentModifier\<SliderConfiguration>](#sliderconfiguration12) | Yes | Content modifier to apply to the slider.<br>**modifier**: content modifier. You need a custom class to implement the **ContentModifier** API. | 413 414> **NOTE** 415> 416> - If **contentModifier** is set, then clicks and swipes within the custom area will not trigger the **onChange** event of the original slider. 417> - The **onChange** event of the original slider can only be triggered when the **triggerChange** function is called with the correct parameter value. 418 419### slideRange<sup>12+</sup> 420 421slideRange(value: SlideRange) 422 423Sets the slide range. 424 425**Atomic service API**: This API can be used in atomic services since API version 12. 426 427**System capability**: SystemCapability.ArkUI.ArkUI.Full 428 429**Parameters** 430 431| Name | Type | Mandatory | Description | 432| ------ |-------------------------------------------| ---- |-------------| 433| value | SlideRange | Yes | Slide range. | 434 435## SliderBlockStyle<sup>10+</sup> 436 437Describes the style of the slider in the block direction. 438 439**Atomic service API**: This API can be used in atomic services since API version 11. 440 441| Name | Type | Mandatory | Description | 442| ----- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 443| type | [SliderBlockType](#sliderblocktype10) | Yes | Type of the slider in the block direction.<br>Default value: **SliderBlockType.DEFAULT**, indicating the round slider. | 444| image | [ResourceStr](ts-types.md#resourcestr) | No | Image resource of the slider.<br>The area size for displaying the image is subject to the **blockSize** attribute. Be mindful of the image size when selecting an image. | 445| shape | [Circle](ts-drawing-components-circle.md) \| [Ellipse](ts-drawing-components-ellipse.md) \| [Path](ts-drawing-components-path.md) \| [Rect](ts-drawing-components-rect.md) | No | Custom shape of the slider. | 446 447## SliderBlockType<sup>10+</sup> 448 449Enumerates the types of the slider in the block direction. 450 451**Atomic service API**: This API can be used in atomic services since API version 11. 452 453| Name | Description | 454| ------- | ---------------------- | 455| DEFAULT | Round slider. | 456| IMAGE | Slider with an image background. | 457| SHAPE | Slider in a custom shape. | 458 459## SliderInteraction<sup>12+</sup> 460 461Enumerates the interaction modes between the user and the slider. 462 463**Atomic service API**: This API can be used in atomic services since API version 12. 464 465| Name | Description | 466| ------ | ----------------------------- | 467| SLIDE_AND_CLICK | Users can drag the slider or touch the track to move the slider. The slider moves as soon as the mouse or finger is pressed.| 468| SLIDE_ONLY | Users are not allowed to move the slider by touching the slider.| 469| SLIDE_AND_CLICK_UP | Users can drag the slider or touch the track to move the slider. The slider moves when the mouse is released or finger is lifted, if the release/lift position coincides with the screen press position.| 470 471## SlideRange<sup>12+</sup> 472 473Defines the callback type used in **SlideRange**. 474 475**Atomic service API**: This API can be used in atomic services since API version 12. 476 477| Name | Type | Mandatory | Description | 478|----|--------| ---- |--------------| 479| from | number | Yes | Start of the slide range. | 480| to | number | Yes | End of the slide range. | 481 482> **NOTE** 483> 484> - Currently, this API takes effect only when **MIN** ≤ **from** ≤ **to** ≤ **MAX** (the values of **MIN** and **MAX** do not depend on the values set, but on the actual values that take effect). 485> - You can set only **from** or **to**, or you can set both**from** and **to** at the same time. 486> - When the API is effective, if the set **from** value is between the adjacent multiples of **step**, then **from** takes the value of the left interval multiple of **step** or **MIN** as the corrected value. 487> - When the API is effective, if the set **to** value is between the adjacent multiples of **step**, then **to** takes the value of the right interval multiple of **step** or **MAX** as the corrected value. 488> - After **from** and **to** have taken their corrected values, when **value** is **undefined** or **null**, it takes the same value as **from**; when **value** is a number type, and if **value** ≤ **from**, then it takes **from**; if **value** > **to**, then it takes **to**. 489 490## Events 491 492In addition to the [universal events](ts-universal-events-click.md), the following attributes are supported. 493 494### onChange 495 496onChange(callback: (value: number, mode: SliderChangeMode) => void) 497 498Invoked when the slider is dragged or clicked. 499 500The **Begin** and **End** states are triggered when the slider is clicked with a gesture. The **Moving** and **Click** states are triggered when the value of **value** changes. 501 502If the coherent action is a drag action, the **Click** state will not be triggered. 503 504**Widget capability**: This API can be used in ArkTS widgets since API version 9. 505 506**Atomic service API**: This API can be used in atomic services since API version 11. 507 508**System capability**: SystemCapability.ArkUI.ArkUI.Full 509 510**Parameters** 511 512| Name | Type | Mandatory | Description | 513| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 514| value | number | Yes | Current sliding progress. Its range of changes corresponds to the array of step sizes. If the return value contains decimals, you can use the **number.toFixed()** API to process the data to the expected precision. | 515| mode | [SliderChangeMode](#sliderchangemode) | Yes | State triggered by the event. | 516 517## SliderChangeMode 518 519**Widget capability**: This API can be used in ArkTS widgets since API version 9. 520 521**Atomic service API**: This API can be used in atomic services since API version 11. 522 523| Name | Value | Description | 524| -------- | -------- | -------- | 525| Begin | 0 | The user touches or clicks the thumb. | 526| Moving | 1 | The user is dragging the slider. | 527| End | 2 | The user stops dragging the slider by lifting their finger or releasing the mouse device.<br>**NOTE**<br>The trigger occurs when an invalid value is restored to the default value, that is, when the value is set to less than **min** or greater than **max**. | 528| Click | 3 | The user moves the thumb by touching or clicking the track. | 529 530## SliderConfiguration<sup>12+</sup> 531 532You need a custom class to implement the **ContentModifier** API. 533 534**Atomic service API**: This API can be used in atomic services since API version 12. 535 536| Name | Type | Description | 537| ------ | ------ | ---------------- | 538| value | number | Current progress. | 539| min | number | Minimum value. | 540| max | number | Maximum value. | 541| step | number | Step of the slider. | 542| triggerChange | [SliderTriggerChangeCallback](#slidertriggerchangecallback12) | Triggers slider changes. | 543 544## SliderTriggerChangeCallback<sup>12+</sup> 545 546Defines the callback type used in **SliderConfiguration**. 547 548**Atomic service API**: This API can be used in atomic services since API version 12. 549 550| Name | Type | Mandatory | Description | 551| ------ | ------ | ---- | ---------------- | 552| value | number | Yes | Current progress. | 553| mode | [SliderChangeMode](#sliderchangemode) | Yes | State triggered by the event. | 554 555## Example 556 557### Example 1 558 559```ts 560// xxx.ets 561@Entry 562@Component 563struct SliderExample { 564 @State outSetValueOne: number = 40 565 @State inSetValueOne: number = 40 566 @State noneValueOne: number = 40 567 @State outSetValueTwo: number = 40 568 @State inSetValueTwo: number = 40 569 @State vOutSetValueOne: number = 40 570 @State vInSetValueOne: number = 40 571 @State vOutSetValueTwo: number = 40 572 @State vInSetValueTwo: number = 40 573 574 build() { 575 Column({ space: 8 }) { 576 Text('outset slider').fontSize(9).fontColor(0xCCCCCC).width('90%').margin(15) 577 Row() { 578 Slider({ 579 value: this.outSetValueOne, 580 min: 0, 581 max: 100, 582 style: SliderStyle.OutSet 583 }) 584 .showTips(true) 585 .onChange((value: number, mode: SliderChangeMode) => { 586 this.outSetValueOne = value 587 console.info('value:' + value + 'mode:' + mode.toString()) 588 }) 589 // toFixed(0) converts the return value of the slider to an integer. 590 Text(this.outSetValueOne.toFixed(0)).fontSize(12) 591 } 592 .width('80%') 593 Row() { 594 Slider({ 595 value: this.outSetValueTwo, 596 step: 10, 597 style: SliderStyle.OutSet 598 }) 599 .showSteps(true) 600 .onChange((value: number, mode: SliderChangeMode) => { 601 this.outSetValueTwo = value 602 console.info('value:' + value + 'mode:' + mode.toString()) 603 }) 604 Text(this.outSetValueTwo.toFixed(0)).fontSize(12) 605 } 606 .width('80%') 607 608 Text('inset slider').fontSize(9).fontColor(0xCCCCCC).width('90%').margin(15) 609 Row() { 610 Slider({ 611 value: this.inSetValueOne, 612 min: 0, 613 max: 100, 614 style: SliderStyle.InSet 615 }) 616 .blockColor('#191970') 617 .trackColor('#ADD8E6') 618 .selectedColor('#4169E1') 619 .showTips(true) 620 .onChange((value: number, mode: SliderChangeMode) => { 621 this.inSetValueOne = value 622 console.info('value:' + value + 'mode:' + mode.toString()) 623 }) 624 Text(this.inSetValueOne.toFixed(0)).fontSize(12) 625 } 626 .width('80%') 627 Row() { 628 Slider({ 629 value: this.inSetValueTwo, 630 step: 10, 631 style: SliderStyle.InSet 632 }) 633 .blockColor('#191970') 634 .trackColor('#ADD8E6') 635 .selectedColor('#4169E1') 636 .showSteps(true) 637 .onChange((value: number, mode: SliderChangeMode) => { 638 this.inSetValueTwo = value 639 console.info('value:' + value + 'mode:' + mode.toString()) 640 }) 641 Text(this.inSetValueTwo.toFixed(0)).fontSize(12) 642 } 643 .width('80%') 644 645 Text('none slider').fontSize(9).fontColor(0xCCCCCC).width('90%').margin(15) 646 Row() { 647 Slider({ 648 value: this.noneValueOne, 649 min: 0, 650 max: 100, 651 style: SliderStyle.NONE 652 }) 653 .blockColor('#191970') 654 .trackColor('#ADD8E6') 655 .selectedColor('#4169E1') 656 .showTips(true) 657 .onChange((value: number, mode: SliderChangeMode) => { 658 this.noneValueOne = value 659 console.info('value:' + value + 'mode:' + mode.toString()) 660 }) 661 Text(this.noneValueOne.toFixed(0)).fontSize(12) 662 } 663 .width('80%') 664 665 Row() { 666 Column() { 667 Text('vertical outset slider').fontSize(9).fontColor(0xCCCCCC).width('50%').margin(15) 668 Row() { 669 Text().width('10%') 670 Slider({ 671 value: this.vOutSetValueOne, 672 style: SliderStyle.OutSet, 673 direction: Axis.Vertical 674 }) 675 .blockColor('#191970') 676 .trackColor('#ADD8E6') 677 .selectedColor('#4169E1') 678 .showTips(true) 679 .onChange((value: number, mode: SliderChangeMode) => { 680 this.vOutSetValueOne = value 681 console.info('value:' + value + 'mode:' + mode.toString()) 682 }) 683 Slider({ 684 value: this.vOutSetValueTwo, 685 step: 10, 686 style: SliderStyle.OutSet, 687 direction: Axis.Vertical 688 }) 689 .blockColor('#191970') 690 .trackColor('#ADD8E6') 691 .selectedColor('#4169E1') 692 .showSteps(true) 693 .onChange((value: number, mode: SliderChangeMode) => { 694 this.vOutSetValueTwo = value 695 console.info('value:' + value + 'mode:' + mode.toString()) 696 }) 697 } 698 }.width('50%').height(300) 699 700 Column() { 701 Text('vertical inset slider').fontSize(9).fontColor(0xCCCCCC).width('50%').margin(15) 702 Row() { 703 Slider({ 704 value: this.vInSetValueOne, 705 style: SliderStyle.InSet, 706 direction: Axis.Vertical, 707 reverse: true // By default, at the top of the vertical slider is the min value and at the bottom is the max value. Therefore, if you want to slide from bottom to top, set reverse to true. 708 }) 709 .showTips(true) 710 .onChange((value: number, mode: SliderChangeMode) => { 711 this.vInSetValueOne = value 712 console.info('value:' + value + 'mode:' + mode.toString()) 713 }) 714 Slider({ 715 value: this.vInSetValueTwo, 716 step: 10, 717 style: SliderStyle.InSet, 718 direction: Axis.Vertical, 719 reverse: true 720 }) 721 .showSteps(true) 722 .onChange((value: number, mode: SliderChangeMode) => { 723 this.vInSetValueTwo = value 724 console.info('value:' + value + 'mode:' + mode.toString()) 725 }) 726 } 727 }.width('50%').height(300) 728 } 729 }.width('100%') 730 } 731} 732``` 733 734 735 736### Example 2 737 738```ts 739@Entry 740@Component 741struct SliderExample { 742 @State tipsValue: number = 40 743 744 build() { 745 Column({ space: 8 }) { 746 Text('block').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 747 Slider({ style: SliderStyle.OutSet, value: 40 }) 748 .blockSize({ width: 40, height: 40 }) 749 .blockBorderColor(Color.Red) 750 .blockBorderWidth(5) 751 Divider() 752 Text('step').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 753 Slider({ style: SliderStyle.InSet, value: 40, step: 10 }) 754 .showSteps(true) 755 .stepSize(8) 756 .stepColor(Color.Yellow) 757 Divider() 758 Text('track').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 759 Slider({ style: SliderStyle.InSet, value: 40 }) 760 .trackBorderRadius(2) 761 Divider() 762 Text('selected').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 763 Slider({ style: SliderStyle.InSet, value: 40 }) 764 .selectedBorderRadius(2) 765 Divider() 766 Text('blockStyle').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 767 Slider({ style: SliderStyle.OutSet, value: 40 }) 768 .blockStyle({ type: SliderBlockType.DEFAULT }) 769 Slider({ style: SliderStyle.OutSet, value: 40 }) 770 .blockStyle({ type: SliderBlockType.IMAGE, image: $r('sys.media.ohos_app_icon') }) 771 Slider({ style: SliderStyle.OutSet, value: 40 }) 772 .blockSize({ width: '60px', height: '60px' }) 773 .blockColor(Color.Red) 774 .blockStyle({ type: SliderBlockType.SHAPE, shape: new Path({ commands: 'M60 60 M30 30 L15 56 L45 56 Z' }) }) 775 Divider() 776 Text('tips').fontSize(9).fontColor(0xCCCCCC).margin(15).width('90%') 777 Slider({ style: SliderStyle.InSet, value: this.tipsValue }) 778 .showTips(true, this.tipsValue.toFixed()) 779 .onChange(value => { 780 this.tipsValue = value 781 }) 782 } 783 } 784} 785``` 786 787 788 789 790 791### Example 3 792 793```ts 794// xxx.ets 795// This example implements a slider with a customcontent area through a style builder. When the Increase button is clicked, the progress bar increases based on the step size set by the original slider. When the Decrease button is clicked, the progress bar decreases, and the original slider's onChange event is triggered. 796@Builder function buildSlider(config: SliderConfiguration) { 797 Row() { 798 Column({space: 30}) { 799 Progress({value: config.value, total: config.max, type:ProgressType.Ring}) 800 .margin({ top:20 }) 801 802 Button ('Increase').onClick () => { 803 config.value = config.value + config.step 804 config.triggerChange(config.value, SliderChangeMode.Click) 805 }) 806 .width(100) 807 .height(25) 808 .fontSize(10) 809 .enabled(config.value<config.max) 810 811 Button ('Decrease').onClick () => { 812 config.value=config.value-config.step 813 config.triggerChange(config.value, SliderChangeMode.Click) 814 }) 815 .width(100) 816 .height(25) 817 .fontSize(10) 818 .enabled(config.value>config.min) 819 820 Slider({ 821 value: config.value, 822 min: config.min, 823 max: config.max, 824 step:config.step, 825 }) 826 .width(config.max) 827 .visibility((config.contentModifier as MySliderStyle).showSlider?Visibility.Visible:Visibility.Hidden) 828 .showSteps(true) 829 .onChange((value: number, mode: SliderChangeMode) => { 830 config.triggerChange(value, mode) 831 }) 832 Text('Current state: '+ ((config.contentModifier as MySliderStyle).sliderChangeMode==0?"Begin" 833 :((config.contentModifier as MySliderStyle).sliderChangeMode==1?"Moving" 834 :((config.contentModifier as MySliderStyle).sliderChangeMode==2?"End" 835 :((config.contentModifier as MySliderStyle).sliderChangeMode==3?"Click":"None"))))) 836 .fontSize(10) 837 Text('Progress: '+ config.value) 838 .fontSize(10) 839 Text('Min: ' + config.min) 840 .fontSize(10) 841 Text('Max: ' + config.max) 842 .fontSize(10) 843 Text ('Step: ' + config.step) 844 .fontSize(10) 845 } 846 .width('80%') 847 848 } 849 .width('100%') 850} 851 852class MySliderStyle implements ContentModifier<SliderConfiguration> { 853 showSlider:boolean=true 854 sliderChangeMode:number=0 855 constructor(showSlider: boolean,sliderChangeMode:number) { 856 this.showSlider = showSlider 857 this.sliderChangeMode = sliderChangeMode 858 } 859 applyContent() : WrappedBuilder<[SliderConfiguration]> { 860 return wrapBuilder(buildSlider) 861 } 862} 863 864 865@Entry 866@Component 867struct SliderExample { 868 @State showSlider:boolean=true 869 @State sliderValue: number = 0 870 @State sliderMin: number = 10 871 @State sliderMax: number = 100 872 @State sliderStep: number = 20 873 @State sliderChangeMode: number = 0 874 875 build() { 876 Column({ space: 8 }) { 877 878 Row() { 879 Slider({ 880 value: this.sliderValue, 881 min: this.sliderMin, 882 max: this.sliderMax, 883 step:this.sliderStep, 884 }) 885 .showSteps(true) 886 .onChange((value: number, mode: SliderChangeMode) => { 887 this.sliderValue = value 888 this.sliderChangeMode=mode 889 console.info(' [SliderLog] value:' + value + 'mode:' + mode.toString()) 890 }) 891 .contentModifier(new MySliderStyle(this.showSlider,this.sliderChangeMode)) 892 893 } 894 .width('100%') 895 896 }.width('100%') 897 } 898} 899``` 900 901 902