2018-10-27 14:27:41 +03:00
|
|
|
part of '../../main.dart';
|
|
|
|
|
|
|
|
class LightControlsWidget extends StatefulWidget {
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LightControlsWidgetState createState() => _LightControlsWidgetState();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LightControlsWidgetState extends State<LightControlsWidget> {
|
|
|
|
|
|
|
|
int _tmpBrightness;
|
2018-12-29 17:44:11 +02:00
|
|
|
int _tmpColorTemp = 0;
|
2019-02-10 17:15:52 +02:00
|
|
|
HSVColor _tmpColor = HSVColor.fromAHSV(1.0, 30.0, 0.0, 1.0);
|
2018-10-27 14:27:41 +03:00
|
|
|
bool _changedHere = false;
|
|
|
|
String _tmpEffect;
|
|
|
|
|
|
|
|
void _resetState(LightEntity entity) {
|
|
|
|
_tmpBrightness = entity.brightness ?? 0;
|
2018-12-29 17:44:11 +02:00
|
|
|
_tmpColorTemp = entity.colorTemp ?? entity.minMireds?.toInt();
|
|
|
|
_tmpColor = entity.color ?? _tmpColor;
|
2019-01-24 12:26:38 +02:00
|
|
|
_tmpEffect = entity.effect;
|
2018-10-27 14:27:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void _setBrightness(LightEntity entity, double value) {
|
|
|
|
setState(() {
|
|
|
|
_tmpBrightness = value.round();
|
|
|
|
_changedHere = true;
|
|
|
|
if (_tmpBrightness > 0) {
|
|
|
|
eventBus.fire(new ServiceCallEvent(
|
|
|
|
entity.domain, "turn_on", entity.entityId,
|
|
|
|
{"brightness": _tmpBrightness}));
|
|
|
|
} else {
|
|
|
|
eventBus.fire(new ServiceCallEvent(
|
|
|
|
entity.domain, "turn_off", entity.entityId,
|
|
|
|
null));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setColorTemp(LightEntity entity, double value) {
|
|
|
|
setState(() {
|
|
|
|
_tmpColorTemp = value.round();
|
|
|
|
_changedHere = true;
|
|
|
|
eventBus.fire(new ServiceCallEvent(
|
|
|
|
entity.domain, "turn_on", entity.entityId,
|
|
|
|
{"color_temp": _tmpColorTemp}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:15:52 +02:00
|
|
|
void _setColor(LightEntity entity, HSVColor color) {
|
2018-10-27 14:27:41 +03:00
|
|
|
setState(() {
|
|
|
|
_tmpColor = color;
|
|
|
|
_changedHere = true;
|
2019-02-10 17:15:52 +02:00
|
|
|
Logger.d( "HS Color: [${color.hue}, ${color.saturation}]");
|
|
|
|
eventBus.fire(new ServiceCallEvent(
|
|
|
|
entity.domain, "turn_on", entity.entityId,
|
|
|
|
{"hs_color": [color.hue, color.saturation*100]}));
|
2018-10-27 14:27:41 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setEffect(LightEntity entity, String value) {
|
|
|
|
setState(() {
|
|
|
|
_tmpEffect = value;
|
|
|
|
_changedHere = true;
|
|
|
|
if (_tmpEffect != null) {
|
|
|
|
eventBus.fire(new ServiceCallEvent(
|
|
|
|
entity.domain, "turn_on", entity.entityId,
|
|
|
|
{"effect": "$value"}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final entityModel = EntityModel.of(context);
|
2018-11-16 22:32:43 +02:00
|
|
|
final LightEntity entity = entityModel.entityWrapper.entity;
|
2018-10-27 14:27:41 +03:00
|
|
|
if (!_changedHere) {
|
|
|
|
_resetState(entity);
|
|
|
|
} else {
|
|
|
|
_changedHere = false;
|
|
|
|
}
|
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
_buildBrightnessControl(entity),
|
|
|
|
_buildColorTempControl(entity),
|
|
|
|
_buildColorControl(entity),
|
|
|
|
_buildEffectControl(entity)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBrightnessControl(LightEntity entity) {
|
2019-02-10 19:06:07 +02:00
|
|
|
if ((entity.supportBrightness) && (_tmpBrightness != null)) {
|
2018-11-17 22:40:33 +02:00
|
|
|
return UniversalSlider(
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_changedHere = true;
|
|
|
|
_tmpBrightness = value.round();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
min: 0.0,
|
|
|
|
max: 255.0,
|
|
|
|
onChangeEnd: (value) => _setBrightness(entity, value),
|
2019-02-10 19:06:07 +02:00
|
|
|
value: _tmpBrightness == null ? 0.0 : _tmpBrightness.toDouble(),
|
2018-11-17 22:40:33 +02:00
|
|
|
leading: Icon(Icons.brightness_5),
|
|
|
|
title: "Brightness",
|
2018-10-27 14:27:41 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container(width: 0.0, height: 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildColorTempControl(LightEntity entity) {
|
2018-12-29 17:44:11 +02:00
|
|
|
if (entity.supportColorTemp) {
|
2018-11-17 22:40:33 +02:00
|
|
|
return UniversalSlider(
|
|
|
|
title: "Color temperature",
|
|
|
|
leading: Text("Cold", style: TextStyle(color: Colors.lightBlue),),
|
2019-02-10 19:06:07 +02:00
|
|
|
value: _tmpColorTemp == null ? entity.maxMireds : _tmpColorTemp.toDouble(),
|
2018-11-17 22:40:33 +02:00
|
|
|
onChangeEnd: (value) => _setColorTemp(entity, value),
|
|
|
|
max: entity.maxMireds,
|
|
|
|
min: entity.minMireds,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_changedHere = true;
|
|
|
|
_tmpColorTemp = value.round();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
closing: Text("Warm", style: TextStyle(color: Colors.amberAccent),),
|
2018-10-27 14:27:41 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container(width: 0.0, height: 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildColorControl(LightEntity entity) {
|
2018-12-29 17:44:11 +02:00
|
|
|
if (entity.supportColor) {
|
2019-02-10 17:15:52 +02:00
|
|
|
return LightColorPicker(
|
|
|
|
color: _tmpColor,
|
|
|
|
onColorSelected: (color) => _setColor(entity, color),
|
2018-10-27 14:27:41 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container(width: 0.0, height: 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _showColorPicker(LightEntity entity) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
titlePadding: EdgeInsets.all(0.0),
|
|
|
|
contentPadding: EdgeInsets.all(0.0),
|
2019-02-10 17:15:52 +02:00
|
|
|
content: LightColorPicker(
|
|
|
|
color: _tmpColor,
|
2018-10-27 14:27:41 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildEffectControl(LightEntity entity) {
|
|
|
|
if ((entity.supportEffect) && (entity.effectList != null)) {
|
|
|
|
return ModeSelectorWidget(
|
|
|
|
onChange: (effect) => _setEffect(entity, effect),
|
|
|
|
caption: "Effect",
|
|
|
|
options: entity.effectList,
|
|
|
|
value: _tmpEffect
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container(width: 0.0, height: 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|