relaySwitch method

Widget relaySwitch(
  1. BuildContext context, {
  2. required String name,
  3. required BoolState relayState(
    1. RelaysData relays
    ),
  4. BoolState desiredState(
    1. RelaysCommand desired
    )?,
  5. RelaysCommand toggleCommand({
    1. required BoolState value,
    })?,
})

Widget for a single relay switch

Implementation

Widget relaySwitch(
  BuildContext context, {
  required String name,
  required BoolState Function(RelaysData relays) relayState,
  BoolState Function(RelaysCommand desired)? desiredState,
  RelaysCommand Function({required BoolState value})? toggleCommand,
}) {
  final current = relayState(model.relays);
  final desired = desiredState?.call(model.desiredRelays);

  return SizedBox(
    width: 75,
    child: Column(
      children: [
        Text(
          name,
          style: context.textTheme.bodyMedium,
          textAlign: TextAlign.center,
        ),
        RotatedBox(
          quarterTurns: -1,
          child: Switch(
            value: (desired ?? current).toBool(),
            onChanged: (value) {
              if (toggleCommand == null) {
                return;
              }
              model.toggleRelay(
                toggleCommand(value: value ? BoolState.ON : BoolState.OFF),
              );
            },
          ),
        ),
        if (current == BoolState.BOOL_UNDEFINED &&
            (desired == null || desired == BoolState.BOOL_UNDEFINED))
          const Tooltip(
            message: "Unknown Relay State",
            child: Icon(Icons.question_mark),
          )
        else if (desired != null)
          desiredIcon(
            current,
            desired,
          ),
      ],
    ),
  );
}