2019-09-07 18:23:04 +03:00
|
|
|
part of '../main.dart';
|
2018-12-07 22:04:14 +02:00
|
|
|
|
|
|
|
class CardWidget extends StatelessWidget {
|
|
|
|
|
|
|
|
final HACard card;
|
|
|
|
|
|
|
|
const CardWidget({
|
|
|
|
Key key,
|
|
|
|
this.card
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2019-03-12 23:35:33 +02:00
|
|
|
if (card.linkedEntityWrapper!= null) {
|
|
|
|
if (card.linkedEntityWrapper.entity.isHidden) {
|
|
|
|
return Container(width: 0.0, height: 0.0,);
|
|
|
|
}
|
2019-03-13 00:56:57 +02:00
|
|
|
if (card.linkedEntityWrapper.entity.statelessType == StatelessEntityType.MISSED) {
|
2019-03-12 23:35:33 +02:00
|
|
|
return EntityModel(
|
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
|
|
|
child: MissedEntityWidget(),
|
|
|
|
handleTap: false,
|
|
|
|
);
|
|
|
|
}
|
2018-12-07 22:04:14 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 20:05:33 +03:00
|
|
|
if (card.conditions.isNotEmpty) {
|
2019-09-09 13:36:33 +03:00
|
|
|
bool showCardByConditions = true;
|
2019-08-26 17:03:28 +03:00
|
|
|
for (var condition in card.conditions) {
|
|
|
|
Entity conditionEntity = HomeAssistant().entities.get(condition['entity']);
|
|
|
|
if (conditionEntity != null &&
|
2019-09-09 13:36:33 +03:00
|
|
|
((condition['state'] != null && conditionEntity.state != condition['state']) ||
|
|
|
|
(condition['state_not'] != null && conditionEntity.state == condition['state_not']))
|
2019-08-26 17:03:28 +03:00
|
|
|
) {
|
2019-09-09 13:36:33 +03:00
|
|
|
showCardByConditions = false;
|
|
|
|
break;
|
2019-08-26 17:03:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!showCardByConditions) {
|
|
|
|
return Container(width: 0.0, height: 0.0,);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 22:04:14 +02:00
|
|
|
switch (card.type) {
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.ENTITIES: {
|
2018-12-07 22:04:14 +02:00
|
|
|
return _buildEntitiesCard(context);
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.GLANCE: {
|
2018-12-07 22:04:14 +02:00
|
|
|
return _buildGlanceCard(context);
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.MEDIA_CONTROL: {
|
2018-12-07 22:04:14 +02:00
|
|
|
return _buildMediaControlsCard(context);
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.ENTITY_BUTTON: {
|
2018-12-07 22:04:14 +02:00
|
|
|
return _buildEntityButtonCard(context);
|
2020-04-13 21:17:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case CardType.BUTTON: {
|
|
|
|
return _buildEntityButtonCard(context);
|
2018-12-07 22:04:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.GAUGE: {
|
|
|
|
return _buildGaugeCard(context);
|
|
|
|
}
|
|
|
|
|
2019-09-08 19:06:41 +03:00
|
|
|
/* case CardType.LIGHT: {
|
2019-09-08 19:04:12 +03:00
|
|
|
return _buildLightCard(context);
|
2019-09-08 19:06:41 +03:00
|
|
|
}*/
|
2019-09-08 19:04:12 +03:00
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.MARKDOWN: {
|
2019-01-23 23:34:45 +02:00
|
|
|
return _buildMarkdownCard(context);
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.ALARM_PANEL: {
|
2019-01-29 15:00:15 +02:00
|
|
|
return _buildAlarmPanelCard(context);
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.HORIZONTAL_STACK: {
|
2018-12-07 22:04:14 +02:00
|
|
|
if (card.childCards.isNotEmpty) {
|
|
|
|
List<Widget> children = [];
|
2020-04-13 22:24:37 +03:00
|
|
|
children = card.childCards.map((childCard) => Flexible(
|
|
|
|
fit: FlexFit.tight,
|
|
|
|
child: childCard.build(context),
|
|
|
|
)
|
|
|
|
).toList();
|
2018-12-07 22:04:14 +02:00
|
|
|
return Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2020-04-13 22:24:37 +03:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
2018-12-07 22:04:14 +02:00
|
|
|
children: children,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Container(height: 0.0, width: 0.0,);
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
case CardType.VERTICAL_STACK: {
|
2018-12-07 22:04:14 +02:00
|
|
|
if (card.childCards.isNotEmpty) {
|
2020-04-13 22:24:37 +03:00
|
|
|
List<Widget> children = card.childCards.map((childCard) => childCard.build(context)).toList();
|
2018-12-07 22:04:14 +02:00
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
children: children,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Container(height: 0.0, width: 0.0,);
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
if ((card.linkedEntityWrapper == null) && (card.entities.isNotEmpty)) {
|
|
|
|
return _buildEntitiesCard(context);
|
|
|
|
} else {
|
|
|
|
return _buildUnsupportedCard(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildEntitiesCard(BuildContext context) {
|
|
|
|
List<EntityWrapper> entitiesToShow = card.getEntitiesToShow();
|
|
|
|
if (entitiesToShow.isEmpty && !card.showEmpty) {
|
|
|
|
return Container(height: 0.0, width: 0.0,);
|
|
|
|
}
|
|
|
|
List<Widget> body = [];
|
2020-02-19 12:17:08 +02:00
|
|
|
Widget headerSwitch;
|
|
|
|
if (card.showHeaderToggle) {
|
|
|
|
bool headerToggleVal = entitiesToShow.any((EntityWrapper en){ return en.entity.state == EntityState.on; });
|
|
|
|
List<String> entitiesToToggle = entitiesToShow.where((EntityWrapper enw) {
|
|
|
|
return <String>["switch", "light", "automation", "input_boolean"].contains(enw.entity.domain);
|
|
|
|
}).map((EntityWrapper en) {
|
|
|
|
return en.entity.entityId;
|
|
|
|
}).toList();
|
|
|
|
headerSwitch = Switch(
|
|
|
|
value: headerToggleVal,
|
|
|
|
onChanged: (val) {
|
|
|
|
if (entitiesToToggle.isNotEmpty) {
|
|
|
|
ConnectionManager().callService(
|
|
|
|
domain: "homeassistant",
|
|
|
|
service: val ? "turn_on" : "turn_off",
|
|
|
|
entityId: entitiesToToggle
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
body.add(
|
|
|
|
CardHeader(
|
|
|
|
name: card.name,
|
|
|
|
trailing: headerSwitch
|
|
|
|
)
|
|
|
|
);
|
2018-12-07 22:04:14 +02:00
|
|
|
entitiesToShow.forEach((EntityWrapper entity) {
|
2019-10-14 13:39:00 +03:00
|
|
|
body.add(
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 4.0, 0.0, 4.0),
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: entity,
|
|
|
|
handleTap: true,
|
|
|
|
child: entity.entity.buildDefaultWidget(context)
|
|
|
|
),
|
|
|
|
));
|
2018-12-07 22:04:14 +02:00
|
|
|
});
|
|
|
|
return Card(
|
2019-06-23 16:08:12 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.only(right: Sizes.rightWidgetPadding, left: Sizes.leftWidgetPadding),
|
|
|
|
child: Column(mainAxisSize: MainAxisSize.min, children: body),
|
|
|
|
)
|
2018-12-07 22:04:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-23 23:34:45 +02:00
|
|
|
Widget _buildMarkdownCard(BuildContext context) {
|
|
|
|
if (card.content == null) {
|
|
|
|
return Container(height: 0.0, width: 0.0,);
|
|
|
|
}
|
|
|
|
List<Widget> body = [];
|
2019-09-07 18:23:04 +03:00
|
|
|
body.add(CardHeader(name: card.name));
|
2019-01-23 23:34:45 +02:00
|
|
|
body.add(MarkdownBody(data: card.content));
|
|
|
|
return Card(
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, Sizes.rowPadding),
|
|
|
|
child: new Column(mainAxisSize: MainAxisSize.min, children: body),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-29 15:00:15 +02:00
|
|
|
Widget _buildAlarmPanelCard(BuildContext context) {
|
2019-03-12 23:35:33 +02:00
|
|
|
List<Widget> body = [];
|
2019-09-07 18:23:04 +03:00
|
|
|
body.add(CardHeader(
|
2019-03-12 23:35:33 +02:00
|
|
|
name: card.name ?? "",
|
|
|
|
subtitle: Text("${card.linkedEntityWrapper.entity.displayState}",
|
|
|
|
),
|
|
|
|
trailing: Row(
|
2019-01-29 18:51:28 +02:00
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
EntityIcon(
|
2019-02-21 16:32:55 +02:00
|
|
|
size: 50.0,
|
2019-01-29 18:51:28 +02:00
|
|
|
),
|
|
|
|
Container(
|
2019-03-12 23:35:33 +02:00
|
|
|
width: 26.0,
|
|
|
|
child: IconButton(
|
|
|
|
padding: EdgeInsets.all(0.0),
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
icon: Icon(MaterialDesignIcons.getIconDataFromIconName(
|
|
|
|
"mdi:dots-vertical")),
|
2019-09-14 18:32:44 +03:00
|
|
|
onPressed: () => eventBus.fire(new ShowEntityPageEvent(entity: card.linkedEntityWrapper.entity))
|
2019-03-12 23:35:33 +02:00
|
|
|
)
|
2019-01-29 18:51:28 +02:00
|
|
|
)
|
|
|
|
]
|
2019-03-12 23:35:33 +02:00
|
|
|
),
|
|
|
|
));
|
|
|
|
body.add(
|
2019-01-29 15:00:15 +02:00
|
|
|
AlarmControlPanelControlsWidget(
|
|
|
|
extended: true,
|
|
|
|
states: card.states,
|
|
|
|
)
|
2019-03-12 23:35:33 +02:00
|
|
|
);
|
|
|
|
return Card(
|
2019-01-29 15:00:15 +02:00
|
|
|
child: EntityModel(
|
2019-03-12 23:35:33 +02:00
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
|
|
|
handleTap: null,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: body
|
|
|
|
)
|
2019-01-29 15:00:15 +02:00
|
|
|
)
|
2019-03-12 23:35:33 +02:00
|
|
|
);
|
2019-01-29 15:00:15 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 22:04:14 +02:00
|
|
|
Widget _buildGlanceCard(BuildContext context) {
|
|
|
|
List<EntityWrapper> entitiesToShow = card.getEntitiesToShow();
|
|
|
|
if (entitiesToShow.isEmpty && !card.showEmpty) {
|
|
|
|
return Container(height: 0.0, width: 0.0,);
|
|
|
|
}
|
|
|
|
List<Widget> rows = [];
|
2019-09-07 18:23:04 +03:00
|
|
|
rows.add(CardHeader(name: card.name));
|
2018-12-07 22:04:14 +02:00
|
|
|
|
|
|
|
int columnsCount = entitiesToShow.length >= card.columnsCount ? card.columnsCount : entitiesToShow.length;
|
|
|
|
|
|
|
|
rows.add(
|
|
|
|
Padding(
|
2019-09-07 16:46:41 +03:00
|
|
|
padding: EdgeInsets.only(bottom: Sizes.rowPadding, top: Sizes.rowPadding),
|
|
|
|
child: FractionallySizedBox(
|
|
|
|
widthFactor: 1,
|
|
|
|
child: LayoutBuilder(
|
|
|
|
builder: (BuildContext context, BoxConstraints constraints) {
|
|
|
|
List<Widget> buttons = [];
|
|
|
|
double buttonWidth = constraints.maxWidth / columnsCount;
|
|
|
|
entitiesToShow.forEach((EntityWrapper entity) {
|
|
|
|
buttons.add(
|
|
|
|
SizedBox(
|
|
|
|
width: buttonWidth,
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: entity,
|
2019-09-07 18:23:04 +03:00
|
|
|
child: GlanceCardEntityContainer(
|
2019-09-07 16:46:41 +03:00
|
|
|
showName: card.showName,
|
|
|
|
showState: card.showState,
|
|
|
|
),
|
|
|
|
handleTap: true
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return Wrap(
|
|
|
|
//spacing: 5.0,
|
|
|
|
//alignment: WrapAlignment.spaceEvenly,
|
2019-09-09 12:25:13 +03:00
|
|
|
runSpacing: Sizes.doubleRowPadding,
|
2019-09-07 16:46:41 +03:00
|
|
|
children: buttons,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
),
|
2018-12-07 22:04:14 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return Card(
|
2019-09-07 16:46:41 +03:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: rows
|
|
|
|
)
|
2018-12-07 22:04:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildMediaControlsCard(BuildContext context) {
|
2019-03-12 23:35:33 +02:00
|
|
|
return Card(
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
|
|
|
handleTap: null,
|
|
|
|
child: MediaPlayerWidget()
|
|
|
|
)
|
|
|
|
);
|
2018-12-07 22:04:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildEntityButtonCard(BuildContext context) {
|
2020-03-15 18:28:45 +02:00
|
|
|
card.linkedEntityWrapper.overrideName = card.name?.toUpperCase() ??
|
2019-03-12 23:35:33 +02:00
|
|
|
card.linkedEntityWrapper.displayName.toUpperCase();
|
|
|
|
return Card(
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
2019-11-29 12:12:41 +02:00
|
|
|
child: EntityButtonCardBody(
|
|
|
|
showName: card.showName,
|
|
|
|
),
|
2019-03-12 23:35:33 +02:00
|
|
|
handleTap: true
|
|
|
|
)
|
|
|
|
);
|
2018-12-07 22:04:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:47:09 +03:00
|
|
|
Widget _buildGaugeCard(BuildContext context) {
|
2020-03-15 18:28:45 +02:00
|
|
|
card.linkedEntityWrapper.overrideName = card.name ??
|
2019-09-07 15:47:09 +03:00
|
|
|
card.linkedEntityWrapper.displayName;
|
2020-03-15 18:28:45 +02:00
|
|
|
card.linkedEntityWrapper.unitOfMeasurementOverride = card.unit ??
|
2019-09-07 17:04:40 +03:00
|
|
|
card.linkedEntityWrapper.unitOfMeasurement;
|
2019-09-07 15:47:09 +03:00
|
|
|
return Card(
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
|
|
|
child: GaugeCardBody(
|
|
|
|
min: card.min,
|
|
|
|
max: card.max,
|
|
|
|
severity: card.severity,
|
|
|
|
),
|
|
|
|
handleTap: true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-08 19:04:12 +03:00
|
|
|
Widget _buildLightCard(BuildContext context) {
|
2020-03-15 18:28:45 +02:00
|
|
|
card.linkedEntityWrapper.overrideName = card.name ??
|
2019-09-08 19:04:12 +03:00
|
|
|
card.linkedEntityWrapper.displayName;
|
|
|
|
return Card(
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
|
|
|
child: LightCardBody(
|
|
|
|
min: card.min,
|
|
|
|
max: card.max,
|
|
|
|
severity: card.severity,
|
|
|
|
),
|
|
|
|
handleTap: true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-07 22:04:14 +02:00
|
|
|
Widget _buildUnsupportedCard(BuildContext context) {
|
|
|
|
List<Widget> body = [];
|
2020-02-19 12:17:08 +02:00
|
|
|
body.add(
|
|
|
|
CardHeader(
|
|
|
|
name: card.name ?? ""
|
|
|
|
)
|
|
|
|
);
|
2018-12-07 22:04:14 +02:00
|
|
|
List<Widget> result = [];
|
|
|
|
if (card.linkedEntityWrapper != null) {
|
|
|
|
result.addAll(<Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0, Sizes.rowPadding, 0.0, Sizes.rowPadding),
|
|
|
|
child: EntityModel(
|
|
|
|
entityWrapper: card.linkedEntityWrapper,
|
|
|
|
handleTap: true,
|
|
|
|
child: card.linkedEntityWrapper.entity.buildDefaultWidget(context)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
result.addAll(<Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, Sizes.rowPadding),
|
|
|
|
child: Text("'${card.type}' card is not supported yet"),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
body.addAll(result);
|
|
|
|
return Card(
|
|
|
|
child: new Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: body
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-27 12:05:33 +03:00
|
|
|
}
|