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

362 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);
}
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 = [];
card.childCards.forEach((card) {
2018-12-14 19:37:49 +02:00
if (card.getEntitiesToShow().isNotEmpty || card.showEmpty) {
children.add(
Flexible(
fit: FlexFit.tight,
child: card.build(context),
)
);
}
});
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
);
}
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.forEach((card) {
children.add(
card.build(context)
);
});
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 = [];
2019-09-07 18:23:04 +03:00
body.add(CardHeader(name: card.name));
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}",
style: TextStyle(
2019-01-29 15:00:15 +02:00
color: Colors.grey
),
2019-03-12 23:35:33 +02:00
),
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,);
}
List<Widget> rows = [];
2019-09-07 18:23:04 +03:00
rows.add(CardHeader(name: card.name));
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,
runSpacing: Sizes.doubleRowPadding,
2019-09-07 16:46:41 +03:00
children: buttons,
);
}
),
),
)
);
return Card(
2019-09-07 16:46:41 +03:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: rows
)
);
}
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) {
2019-03-12 23:35:33 +02:00
card.linkedEntityWrapper.displayName = card.name?.toUpperCase() ??
card.linkedEntityWrapper.displayName.toUpperCase();
return Card(
child: EntityModel(
entityWrapper: card.linkedEntityWrapper,
2019-09-07 18:23:04 +03:00
child: EntityButtonCardBody(),
2019-03-12 23:35:33 +02:00
handleTap: true
)
);
}
2019-09-07 15:47:09 +03:00
Widget _buildGaugeCard(BuildContext context) {
card.linkedEntityWrapper.displayName = card.name ??
card.linkedEntityWrapper.displayName;
2019-09-07 17:04:40 +03:00
card.linkedEntityWrapper.unitOfMeasurement = card.unit ??
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.displayName = card.name ??
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 = [];
2019-09-07 18:23:04 +03:00
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
}