relaySwitch method
- BuildContext context, {
- required String name,
- required BoolState relayState(
- RelaysData relays
- BoolState desiredState(
- RelaysCommand desired
- RelaysCommand toggleCommand({
- 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,
),
],
),
);
}