The WithEnv component is used to set a local environment variable scope for a child component tree. Developers can use this component to provide custom environment variables for descendant components, or set system environment variables.
Since: 26.0.0
WithEnv()
Sets a local environment variable scope container.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Model restriction: This API can be used only in the stage model.
env<T>(key: WritableSystemEnvKey<T>, value: T)
Sets the system environment variable within the scope. The currently officially supported system environment variable keys are WritableEnvKey.FONT_SCALE and WritableEnvKey.DIRECTION.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Model Constraints: This API can be used only in the stage model.
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| key | WritableSystemEnvKey<T> | Yes | System environment variable key. Currently, WritableEnvKey.FONT_SCALE and WritableEnvKey.DIRECTION are officially supported. |
| value | T | Yes | System environment variable value. The type T of value corresponds to the type T in WritableSystemEnvKey<T>. When key is WritableEnvKey.FONT_SCALE, the type of value is number. When key is WritableEnvKey.DIRECTION, the type of value is Direction. |
customEnv<T>(key: CustomEnvKey<T>, value: T)
Sets a custom environment variable that can be read by descendant custom components within the scope.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Model restriction: This API can be used only in the stage model.
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| key | CustomEnvKey<T> | Yes | Key of the custom environment variable. |
| value | T | Yes | Value of the custom environment variable. The type T of value corresponds to the type T of CustomEnvKey<T>. |
This example uses env(WritableEnvKey.FONT_SCALE, value) to set a local font scale for components within the scope.
Since API version 26.0.0, the env attribute and the key WritableEnvKey.FONT_SCALE are added.
// xxx.ets
import { WithEnv } from '@kit.ArkUI';
@Entry
@Component
struct WithEnvExample1 {
@State fontScale: number = 1.0;
build() {
Column({ space: 12 }) {
Row({ space: 8 }) {
Button('Zoom out 0.5x')
.onClick(() => {
this.fontScale = 0.5;
})
Button('Normal 1.0x')
.onClick(() => {
this.fontScale = 1.0;
})
Button('Zoom in 1.5x')
.onClick(() => {
this.fontScale = 1.5;
})
}
WithEnv() {
Column({ space: 8 }) {
Text('Text within the current font scale scope')
.fontSize(16)
Text('This text is also affected by the WithEnv font scaling')
.fontSize(14)
.fontColor('#99182431')
}
.width('100%')
.alignItems(HorizontalAlign.Start)
}
.env(WritableEnvKey.FONT_SCALE, this.fontScale) // Set the local font scale ratio.
}
.padding(12)
.width('100%')
}
} This example uses env(WritableEnvKey.DIRECTION, value) to set the local layout direction for components within the scope.
Since API version 26.0.0, the env attribute and the key WritableEnvKey.DIRECTION are added.
// xxx.ets
import { WithEnv } from '@kit.ArkUI';
@Entry
@Component
struct WithEnvExample2 {
@State directionValue: Direction = Direction.Ltr;
build() {
Column({ space: 12 }) {
Row({ space: 10 }) {
Column().backgroundColor('#F0FAFF').width(60).height('100%')
Column().backgroundColor('#2787D9').width(60).height('100%')
Column().backgroundColor('#004AAF').width(60).height('100%')
}.backgroundColor('#D5D5D5').width(200).height(50)
WithEnv() {
Row({ space: 10 }) {
Column().backgroundColor('#F0FAFF').width(60).height('100%')
Column().backgroundColor('#2787D9').width(60).height('100%')
Column().backgroundColor('#004AAF').width(60).height('100%')
}.backgroundColor('#D5D5D5').width(200).height(50)
}
.env(WritableEnvKey.DIRECTION, this.directionValue) // Set local layout direction.
Button('change direction').onClick(() => {
if (this.directionValue === Direction.Ltr) {
this.directionValue = Direction.Rtl;
} else {
this.directionValue = Direction.Ltr;
}
})
}
.width('80%')
.height('30%')
}
} 