Refactoring: Entity classes by action type. Wntity widget building in
entity
This commit is contained in:
@ -1,6 +1,13 @@
|
||||
part of 'main.dart';
|
||||
|
||||
class Entity {
|
||||
static Map<String, Color> stateIconColors = {
|
||||
"on": Colors.amber,
|
||||
"off": Color.fromRGBO(68, 115, 158, 1.0),
|
||||
"unavailable": Colors.black12,
|
||||
"unknown": Colors.black12,
|
||||
"playing": Colors.amber
|
||||
};
|
||||
Map _attributes;
|
||||
String _domain;
|
||||
String _entityId;
|
||||
@ -43,4 +50,123 @@ class Entity {
|
||||
_state = rawData["state"];
|
||||
}
|
||||
|
||||
Widget buildWidget() {
|
||||
return SizedBox(
|
||||
height: 34.0,
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(8.0, 0.0, 12.0, 0.0),
|
||||
child: MaterialDesignIcons.createIconWidgetFromEntityData(this, 28.0, Entity.stateIconColors[_state] ?? Colors.blueGrey),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
"${this.displayName}",
|
||||
overflow: TextOverflow.fade,
|
||||
softWrap: false,
|
||||
style: TextStyle(
|
||||
fontSize: 16.0
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildActionWidget()
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionWidget() {
|
||||
return Padding(
|
||||
padding: EdgeInsets.fromLTRB(0.0, 0.0, 14.0, 0.0),
|
||||
child: Text(
|
||||
"${_state}${this.unitOfMeasurement}",
|
||||
textAlign: TextAlign.right,
|
||||
style: new TextStyle(
|
||||
fontSize: 16.0,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SwitchEntity extends Entity {
|
||||
|
||||
SwitchEntity(Map rawData) : super(rawData);
|
||||
|
||||
@override
|
||||
Widget _buildActionWidget() {
|
||||
return Switch(
|
||||
value: this.isOn,
|
||||
onChanged: ((switchState) {
|
||||
eventBus.fire(new ServiceCallEvent(_domain, switchState ? "turn_on" : "turn_off", entityId, null));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ButtonEntity extends Entity {
|
||||
|
||||
ButtonEntity(Map rawData) : super(rawData);
|
||||
|
||||
@override
|
||||
Widget _buildActionWidget() {
|
||||
return FlatButton(
|
||||
onPressed: (() {
|
||||
eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null));
|
||||
}),
|
||||
child: Text(
|
||||
"EXECUTE",
|
||||
textAlign: TextAlign.right,
|
||||
style: new TextStyle(fontSize: 16.0, color: Colors.blue),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InputEntity extends Entity {
|
||||
|
||||
InputEntity(Map rawData) : super(rawData);
|
||||
|
||||
@override
|
||||
Widget _buildActionWidget() {
|
||||
if (this.isSlider) {
|
||||
return Container(
|
||||
width: 200.0,
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Slider(
|
||||
min: this.minValue*10,
|
||||
max: this.maxValue*10,
|
||||
value: this.doubleState*10,
|
||||
divisions: this.getValueDivisions(),
|
||||
onChanged: (value) {
|
||||
eventBus.fire(new StateChangedEvent(_entityId, (value.roundToDouble() / 10).toString(), true));
|
||||
},
|
||||
onChangeEnd: (value) {
|
||||
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "${_state}"}));
|
||||
},
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(right: 16.0),
|
||||
child: Text(
|
||||
"${_state}${this.unitOfMeasurement}",
|
||||
textAlign: TextAlign.right,
|
||||
style: new TextStyle(
|
||||
fontSize: 16.0,
|
||||
)
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
//TODO draw box instead of slider
|
||||
return Text("Not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user