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/cards/card_widget.dart

384 lines
11 KiB
Dart
Raw Normal View History

2019-09-07 18:23:04 +03:00
part of '../main.dart';
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,);
}
if (card.linkedEntityWrapper.entity.statelessType == StatelessEntityType.MISSED) {
2019-03-12 23:35:33 +02:00
return EntityModel(
entityWrapper: card.linkedEntityWrapper,
child: MissedEntityWidget(),
handleTap: false,
);
}
}
if (card.conditions.isNotEmpty) {
2019-09-09 13:36:33 +03:00
bool showCardByConditions = true;
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-09-09 13:36:33 +03:00
showCardByConditions = false;
break;
}
}
if (!showCardByConditions) {
return Container(width: 0.0, height: 0.0,);
}
}
switch (card.type) {
2019-09-07 15:47:09 +03:00
case CardType.ENTITIES: {
return _buildEntitiesCard(context);
}
2019-09-07 15:47:09 +03:00
case CardType.GLANCE: {
return _buildGlanceCard(context);
}
2019-09-07 15:47:09 +03:00
case CardType.MEDIA_CONTROL: {
return _buildMediaControlsCard(context);
}
2019-09-07 15:47:09 +03:00
case CardType.ENTITY_BUTTON: {
return _buildEntityButtonCard(context);
2020-04-13 21:17:14 +03:00
}
case CardType.BUTTON: {
return _buildEntityButtonCard(context);
}
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: {
if (card.childCards.isNotEmpty) {
List<Widget> children = [];
children = card.childCards.map((childCard) => Flexible(
fit: FlexFit.tight,
child: childCard.build(context),
)
).toList();
2020-04-25 17:15:19 +03:00
return IntrinsicHeight(
child: Row(
2020-04-21 12:01:21 +03:00
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
2020-04-25 17:15:19 +03:00
crossAxisAlignment: CrossAxisAlignment.stretch,
2020-04-21 12:01:21 +03:00
children: children,
2020-04-25 17:15:19 +03:00
),
);
}
return Container(height: 0.0, width: 0.0,);
}
2019-09-07 15:47:09 +03:00
case CardType.VERTICAL_STACK: {
if (card.childCards.isNotEmpty) {
List<Widget> children = card.childCards.map((childCard) => childCard.build(context)).toList();
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 = [];
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
)
);
entitiesToShow.forEach((EntityWrapper entity) {
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)
),
));
});
return Card(
child: Padding(
padding: EdgeInsets.only(right: Sizes.rightWidgetPadding, left: Sizes.leftWidgetPadding),
child: Column(mainAxisSize: MainAxisSize.min, children: body),
)
);
}
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(
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
}
Widget _buildGlanceCard(BuildContext context) {
List<EntityWrapper> entitiesToShow = card.getEntitiesToShow();
if (entitiesToShow.isEmpty && !card.showEmpty) {
return Container(height: 0.0, width: 0.0,);
}
2020-04-25 17:15:19 +03:00
int length = entitiesToShow.length;
int columnsCount = length >= card.columnsCount ? card.columnsCount : entitiesToShow.length;
int rowsCount = (length / columnsCount).round();
List<TableRow> rows = [];
for (int i = 0; i < rowsCount; i++) {
int start = i*columnsCount;
int end = start + math.min(columnsCount, length - start);
List<Widget> rowChildren = [];
rowChildren.addAll(entitiesToShow.sublist(
start, end
).map(
(EntityWrapper entity){
return EntityModel(
entityWrapper: entity,
child: GlanceCardEntityContainer(
showName: card.showName,
showState: card.showState,
),
handleTap: true
);
}
).toList()
);
while (rowChildren.length < columnsCount) {
rowChildren.add(
Container()
);
}
rows.add(
TableRow(
children: rowChildren
)
2020-04-25 17:15:19 +03:00
);
}
return Card(
2020-04-25 17:15:19 +03:00
child: Padding(
padding: EdgeInsets.symmetric(vertical: Sizes.rowPadding),
child: Table(
children: rows
2019-09-07 16:46:41 +03:00
)
2020-04-25 17:15:19 +03:00
)
);
}
Widget _buildMediaControlsCard(BuildContext context) {
2019-03-12 23:35:33 +02:00
return Card(
child: EntityModel(
entityWrapper: card.linkedEntityWrapper,
handleTap: null,
child: MediaPlayerWidget()
)
);
}
Widget _buildEntityButtonCard(BuildContext context) {
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,
child: EntityButtonCardBody(
2020-04-25 17:15:19 +03:00
depth: card.depth,
showName: card.showName,
),
2019-03-12 23:35:33 +02:00
handleTap: true
)
);
}
2019-09-07 15:47:09 +03:00
Widget _buildGaugeCard(BuildContext context) {
card.linkedEntityWrapper.overrideName = card.name ??
2019-09-07 15:47:09 +03:00
card.linkedEntityWrapper.displayName;
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) {
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
)
);
}
Widget _buildUnsupportedCard(BuildContext context) {
List<Widget> body = [];
body.add(
CardHeader(
name: card.name ?? ""
)
);
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
}