HWConditional

Render one of two branches based on widget data.

HWConditional

HWConditional is the abstract base for widgets that pick between two children based on a condition over the widget's data. Use one of the concrete subclasses below; both branches contribute their data dependencies to the generated entry type.

Variants

HWDataExists

Renders whenPresent if the data field has a value in the widget storage, otherwise whenAbsent. Works with any HWDataType.

HWDataExists({required HWDataType data, required HWWidget whenPresent, required HWWidget whenAbsent})

HWBoolConditional

Renders whenTrue if the boolean data field is true, otherwise whenFalse. The data must be an HWBool (or a JSON leaf of HWBool) with a non-null defaultValue so the condition is always defined.

HWBoolConditional({required HWDataType data, required HWWidget whenTrue, required HWWidget whenFalse})

Example

HWDataExists(
  data: HWBool('hasData'),
  whenPresent: HWBoolConditional(
    data: HWBool('data', defaultValue: true),
    whenTrue: HWText.fixed(
      'Enabled',
      style: HWRoleTextStyle.headline(color: HWColor.fixed(0xFF00FF00)),
    ),
    whenFalse: HWText.fixed(
      'Disabled',
      style: HWRoleTextStyle.headline(color: HWColor.fixed(0xFFFF0000)),
    ),
  ),
  whenAbsent: HWText.fixed('No Data'),
)

Parameters

HWDataExists

NameTypeDefaultDescription
dataHWDataTyperequiredField whose presence is tested.
whenPresentHWWidgetrequiredRendered when the field has a value.
whenAbsentHWWidgetrequiredRendered when the field is null/missing.

HWBoolConditional

NameTypeDefaultDescription
dataHWDataTyperequiredAn HWBool (or HWJson with HWBool leaf) with a defaultValue.
whenTrueHWWidgetrequiredRendered when the value is true.
whenFalseHWWidgetrequiredRendered when the value is false.

Platform mapping

  • iOS: generates a SwiftUI if / else expression in the view body — see SwiftUI ViewBuilder.
  • Android: generates a Kotlin if / else expression in the Glance composable — see Glance composables.