2018-10-01 21:57:54 +03:00
|
|
|
part of '../main.dart';
|
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
class _SliderEntityWidgetState extends _EntityWidgetState {
|
2018-10-01 21:57:54 +03:00
|
|
|
int _multiplier = 1;
|
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
double get minValue => widget.entity._attributes["min"] ?? 0.0;
|
|
|
|
double get maxValue => widget.entity._attributes["max"] ?? 100.0;
|
|
|
|
double get valueStep => widget.entity._attributes["step"] ?? 1.0;
|
|
|
|
double get doubleState => double.tryParse(widget.entity.state) ?? 0.0;
|
2018-10-01 21:57:54 +03:00
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void sendNewState(newValue) {
|
2018-10-02 00:41:40 +03:00
|
|
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
|
2018-10-01 21:57:54 +03:00
|
|
|
{"value": "${newValue.toString()}"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
2018-10-02 00:41:40 +03:00
|
|
|
if (valueStep < 1) {
|
|
|
|
_multiplier = 10;
|
|
|
|
} else if (valueStep < 0.1) {
|
|
|
|
_multiplier = 100;
|
|
|
|
}
|
2018-10-01 21:57:54 +03:00
|
|
|
return Container(
|
|
|
|
width: 200.0,
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Expanded(
|
|
|
|
child: Slider(
|
|
|
|
min: this.minValue * _multiplier,
|
|
|
|
max: this.maxValue * _multiplier,
|
2018-10-02 00:41:40 +03:00
|
|
|
value: (doubleState <= this.maxValue) &&
|
|
|
|
(doubleState >= this.minValue)
|
|
|
|
? doubleState * _multiplier
|
2018-10-01 21:57:54 +03:00
|
|
|
: this.minValue * _multiplier,
|
|
|
|
onChanged: (value) {
|
2018-10-02 00:41:40 +03:00
|
|
|
setState(() {
|
|
|
|
widget.entity.state = (value.roundToDouble() / _multiplier).toString();
|
|
|
|
});
|
|
|
|
/*eventBus.fire(new StateChangedEvent(widget.entity.entityId,
|
|
|
|
(value.roundToDouble() / _multiplier).toString(), true));*/
|
2018-10-01 21:57:54 +03:00
|
|
|
},
|
|
|
|
onChangeEnd: (value) {
|
|
|
|
sendNewState(value.roundToDouble() / _multiplier);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(right: Entity.RIGHT_WIDGET_PADDING),
|
2018-10-02 00:41:40 +03:00
|
|
|
child: Text("${widget.entity.state}${widget.entity.unitOfMeasurement}",
|
2018-10-01 21:57:54 +03:00
|
|
|
textAlign: TextAlign.right,
|
|
|
|
style: new TextStyle(
|
|
|
|
fontSize: Entity.STATE_FONT_SIZE,
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|