This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entity_class/slider_entity.class.dart

65 lines
2.1 KiB
Dart
Raw Normal View History

part of '../main.dart';
2018-10-02 00:41:40 +03:00
class _SliderEntityWidgetState extends _EntityWidgetState {
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-02 00:41:40 +03:00
@override
void initState() {
super.initState();
}
@override
void sendNewState(newValue) {
2018-10-02 00:41:40 +03:00
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
{"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;
}
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
: 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));*/
},
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}",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: Entity.STATE_FONT_SIZE,
)),
)
],
),
);
}
}