Fix slider issues. Siplify Entity view

This commit is contained in:
estevez 2018-09-30 01:07:02 +03:00
parent 571778fbd4
commit 76d2750ad6
4 changed files with 57 additions and 43 deletions

View File

@ -8,16 +8,19 @@ class Entity {
"unknown": Colors.black12, "unknown": Colors.black12,
"playing": Colors.amber "playing": Colors.amber
}; };
static const RIGTH_WIDGET_PADDING = 14.0; static const RIGHT_WIDGET_PADDING = 14.0;
static const LEFT_WIDGET_PADDING = 8.0; static const LEFT_WIDGET_PADDING = 8.0;
static const EXTENDED_WIDGET_HEIGHT = 50.0; static const EXTENDED_WIDGET_HEIGHT = 50.0;
static const WIDGET_HEIGHT = 34.0; static const WIDGET_HEIGHT = 34.0;
static const ICON_SIZE = 28.0;
static const STATE_FONT_SIZE = 16.0;
static const NAME_FONT_SIZE = 16.0;
static const SMALL_FONT_SIZE = 12.0;
Map _attributes; Map _attributes;
String _domain; String _domain;
String _entityId; String _entityId;
String _state; String _state;
String _entityPicture;
DateTime _lastUpdated; DateTime _lastUpdated;
String get displayName => _attributes["friendly_name"] ?? (_attributes["name"] ?? "_"); String get displayName => _attributes["friendly_name"] ?? (_attributes["name"] ?? "_");
@ -72,28 +75,28 @@ class Entity {
eventBus.fire(new ShowEntityPageEvent(this)); eventBus.fire(new ShowEntityPageEvent(this));
} }
Widget buildWidget(BuildContext context) { Widget buildWidget(GlobalKey<FormState> formKey, bool tapAction) {
return SizedBox( return SizedBox(
height: Entity.WIDGET_HEIGHT, height: Entity.WIDGET_HEIGHT,
child: Row( child: Row(
children: <Widget>[ children: <Widget>[
GestureDetector( GestureDetector(
child: _buildIconWidget(), child: _buildIconWidget(),
onTap: openEntityPage, onTap: tapAction ? openEntityPage : null,
), ),
Expanded( Expanded(
child: GestureDetector( child: GestureDetector(
child: _buildNameWidget(), child: _buildNameWidget(),
onTap: openEntityPage, onTap: tapAction ? openEntityPage : null,
), ),
), ),
_buildActionWidget(context) _buildActionWidget()
], ],
), ),
); );
} }
Widget buildExtendedWidget(BuildContext context, String staticState) { /*Widget buildExtendedWidget(BuildContext context, GlobalKey<FormState> formKey, String staticState) {
return Row( return Row(
children: <Widget>[ children: <Widget>[
_buildIconWidget(), _buildIconWidget(),
@ -115,12 +118,12 @@ class Entity {
) )
], ],
); );
} }*/
Widget _buildIconWidget() { Widget _buildIconWidget() {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0), padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
child: MaterialDesignIcons.createIconWidgetFromEntityData(this, 28.0, Entity.STATE_ICONS_COLORS[_state] ?? Colors.blueGrey), child: MaterialDesignIcons.createIconWidgetFromEntityData(this, Entity.ICON_SIZE, Entity.STATE_ICONS_COLORS[_state] ?? Colors.blueGrey),
); );
} }
@ -129,7 +132,7 @@ class Entity {
'${this.lastUpdated}', '${this.lastUpdated}',
textAlign: TextAlign.left, textAlign: TextAlign.left,
style: TextStyle( style: TextStyle(
fontSize: 12.0, fontSize: Entity.SMALL_FONT_SIZE,
color: Colors.black26 color: Colors.black26
), ),
); );
@ -143,22 +146,22 @@ class Entity {
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
style: TextStyle( style: TextStyle(
fontSize: 16.0 fontSize: Entity.NAME_FONT_SIZE
), ),
), ),
); );
} }
Widget _buildActionWidget(BuildContext context) { Widget _buildActionWidget() {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGTH_WIDGET_PADDING, 0.0), padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
child: GestureDetector( child: GestureDetector(
child: Text( child: Text(
this.isPasswordField ? "******" : this.isPasswordField ? "******" :
"$_state${this.unitOfMeasurement}", "$_state${this.unitOfMeasurement}",
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: new TextStyle( style: new TextStyle(
fontSize: 16.0, fontSize: Entity.STATE_FONT_SIZE,
) )
), ),
onTap: openEntityPage, onTap: openEntityPage,
@ -166,9 +169,9 @@ class Entity {
); );
} }
Widget _buildExtendedActionWidget(BuildContext context, String staticState) { /*Widget _buildExtendedActionWidget(BuildContext context, String staticState) {
return _buildActionWidget(context); return _buildActionWidget(context);
} }*/
} }
class SwitchEntity extends Entity { class SwitchEntity extends Entity {
@ -176,7 +179,7 @@ class SwitchEntity extends Entity {
SwitchEntity(Map rawData) : super(rawData); SwitchEntity(Map rawData) : super(rawData);
@override @override
Widget _buildActionWidget(BuildContext context) { Widget _buildActionWidget() {
return Switch( return Switch(
value: this.isOn, value: this.isOn,
onChanged: ((switchState) { onChanged: ((switchState) {
@ -192,7 +195,7 @@ class ButtonEntity extends Entity {
ButtonEntity(Map rawData) : super(rawData); ButtonEntity(Map rawData) : super(rawData);
@override @override
Widget _buildActionWidget(BuildContext context) { Widget _buildActionWidget() {
return FlatButton( return FlatButton(
onPressed: (() { onPressed: (() {
eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null)); eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null));
@ -200,7 +203,7 @@ class ButtonEntity extends Entity {
child: Text( child: Text(
"EXECUTE", "EXECUTE",
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: new TextStyle(fontSize: 16.0, color: Colors.blue), style: new TextStyle(fontSize: Entity.STATE_FONT_SIZE, color: Colors.blue),
), ),
); );
} }
@ -211,8 +214,8 @@ class InputEntity extends Entity {
InputEntity(Map rawData) : super(rawData); InputEntity(Map rawData) : super(rawData);
@override /*@override
Widget buildExtendedWidget(BuildContext context, String staticState) { Widget buildExtendedWidget(BuildContext context, GlobalKey<FormState> formKey, String staticState) {
return Column( return Column(
children: <Widget>[ children: <Widget>[
SizedBox( SizedBox(
@ -229,14 +232,14 @@ class InputEntity extends Entity {
), ),
SizedBox( SizedBox(
height: Entity.EXTENDED_WIDGET_HEIGHT, height: Entity.EXTENDED_WIDGET_HEIGHT,
child: _buildExtendedActionWidget(context, staticState), child: _buildInputWidget(context, formKey, staticState),
) )
], ],
); );
} }*/
@override @override
Widget _buildActionWidget(BuildContext context) { Widget _buildActionWidget() {
if (this.isSliderField) { if (this.isSliderField) {
return Container( return Container(
width: 200.0, width: 200.0,
@ -247,9 +250,9 @@ class InputEntity extends Entity {
min: this.minValue*10, min: this.minValue*10,
max: this.maxValue*10, max: this.maxValue*10,
value: (this.doubleState <= this.maxValue) && (this.doubleState >= this.minValue) ? this.doubleState*10 : this.minValue*10, value: (this.doubleState <= this.maxValue) && (this.doubleState >= this.minValue) ? this.doubleState*10 : this.minValue*10,
divisions: this.getValueDivisions(), //divisions: this.getValueDivisions(),
onChanged: (value) { onChanged: (value) {
eventBus.fire(new StateChangedEvent(_entityId, (value.roundToDouble() / 10).toString(), true)); eventBus.fire(new StateChangedEvent(_entityId, ((value / 10).roundToDouble()).toString(), true));
}, },
onChangeEnd: (value) { onChangeEnd: (value) {
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "$_state"})); eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,{"value": "$_state"}));
@ -257,27 +260,40 @@ class InputEntity extends Entity {
), ),
), ),
Padding( Padding(
padding: EdgeInsets.only(right: 16.0), padding: EdgeInsets.only(right: Entity.RIGHT_WIDGET_PADDING),
child: Text( child: Text(
"$_state${this.unitOfMeasurement}", "$_state${this.unitOfMeasurement}",
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: new TextStyle( style: new TextStyle(
fontSize: 16.0, fontSize: Entity.STATE_FONT_SIZE,
) )
), ),
) )
], ],
), ),
); );
} else if (this.isTextField || this.isPasswordField) {
return Container(
width: 160.0,
child: TextField(
obscureText: this.isPasswordField,
controller: TextEditingController(
text: _state,
),
onChanged: (value) {
//TODO
//staticState = value;
},
),
);
} else { } else {
return super._buildActionWidget(context); return super._buildActionWidget();
} }
} }
@override /*Widget _buildInputWidget(BuildContext context, GlobalKey<FormState> formKey, String staticState) {
Widget _buildExtendedActionWidget(BuildContext context, String staticState) {
return Padding( return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, Entity.RIGTH_WIDGET_PADDING, 0.0), padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
@ -302,13 +318,13 @@ class InputEntity extends Entity {
child: Text( child: Text(
"SET", "SET",
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: new TextStyle(fontSize: 16.0, color: Colors.blue), style: new TextStyle(fontSize: Entity.STATE_FONT_SIZE, color: Colors.blue),
), ),
), ),
) )
], ],
) )
); );
} }*/
} }

View File

@ -12,21 +12,18 @@ class EntityViewPage extends StatefulWidget {
class _EntityViewPageState extends State<EntityViewPage> { class _EntityViewPageState extends State<EntityViewPage> {
String _title; String _title;
Entity _entity; Entity _entity;
String _lastState;
StreamSubscription _stateSubscription; StreamSubscription _stateSubscription;
final _formKey = GlobalKey<FormState>();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_entity = widget.entity; _entity = widget.entity;
_lastState = _entity.state;
if (_stateSubscription != null) _stateSubscription.cancel(); if (_stateSubscription != null) _stateSubscription.cancel();
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) { _stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
setState(() { if (event.entityId == _entity.entityId) {
if (event.entityId == _entity.entityId) { setState(() {});
_lastState = event.newState ?? _entity.state; }
}
});
}); });
_prepareData(); _prepareData();
} }
@ -50,7 +47,7 @@ class _EntityViewPageState extends State<EntityViewPage> {
padding: EdgeInsets.all(10.0), padding: EdgeInsets.all(10.0),
child: ListView( child: ListView(
children: <Widget>[ children: <Widget>[
_entity.buildExtendedWidget(context, _lastState) _entity.buildWidget(_formKey, false)
], ],
), ),
), ),

View File

@ -409,7 +409,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
entities.add( entities.add(
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: entity.buildWidget(context), child: entity.buildWidget(null, true),
)); ));
} }
}); });

View File

@ -2875,6 +2875,7 @@ class MaterialDesignIcons {
if (data.entityPicture != null) { if (data.entityPicture != null) {
if (homeAssistantWebHost != null) { if (homeAssistantWebHost != null) {
return CircleAvatar( return CircleAvatar(
radius: size/2,
backgroundColor: Colors.white, backgroundColor: Colors.white,
backgroundImage: CachedNetworkImageProvider( backgroundImage: CachedNetworkImageProvider(
"$homeAssistantWebHost${data.entityPicture}", "$homeAssistantWebHost${data.entityPicture}",