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

294 lines
8.4 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: {
2020-04-25 18:59:07 +03:00
return GlanceCard(card: card);
}
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: {
2020-04-25 18:59:07 +03:00
return EntityButtonCard(card: card);
2020-04-13 21:17:14 +03:00
}
case CardType.BUTTON: {
2020-04-25 18:59:07 +03:00
return EntityButtonCard(card: card);
}
2019-09-07 15:47:09 +03:00
case CardType.GAUGE: {
2020-04-25 18:59:07 +03:00
return GaugeCard(card: card);
2019-09-07 15:47:09 +03:00
}
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
)
);
2020-04-25 17:39:15 +03:00
body.addAll(
entitiesToShow.map((EntityWrapper entity) {
return Padding(
padding: EdgeInsets.fromLTRB(0.0, 4.0, 0.0, 4.0),
child: EntityModel(
entityWrapper: entity,
handleTap: true,
child: entity.entity.buildDefaultWidget(context)
),
2020-04-25 17:39:15 +03:00
);
})
);
2020-04-25 18:59:07 +03:00
return CardWrapper(
child: Padding(
2020-04-25 18:59:07 +03:00
padding: EdgeInsets.only(
right: Sizes.rightWidgetPadding,
left: Sizes.leftWidgetPadding,
bottom: Sizes.rowPadding,
),
2020-04-25 17:39:15 +03:00
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
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));
2020-04-25 18:59:07 +03:00
return CardWrapper(
2019-01-23 23:34:45 +02:00
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
);
2020-04-25 18:59:07 +03:00
return CardWrapper(
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 _buildMediaControlsCard(BuildContext context) {
2020-04-25 18:59:07 +03:00
return CardWrapper(
2019-03-12 23:35:33 +02:00
child: EntityModel(
entityWrapper: card.linkedEntityWrapper,
handleTap: null,
child: MediaPlayerWidget()
)
);
}
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);
2020-04-25 18:59:07 +03:00
return CardWrapper(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: body
)
);
}
2019-08-27 12:05:33 +03:00
}