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/entity_widgets/entity_icon.dart

86 lines
3.0 KiB
Dart
Raw Normal View History

2018-10-27 14:27:41 +03:00
part of '../main.dart';
2019-03-02 20:13:24 +02:00
class EntityIcon extends StatefulWidget {
2018-11-14 19:52:17 +02:00
final EdgeInsetsGeometry padding;
final double size;
final Color color;
2018-11-14 19:52:17 +02:00
2019-03-02 20:13:24 +02:00
EntityIcon({Key key, this.padding: const EdgeInsets.fromLTRB(
Sizes.leftWidgetPadding, 0.0, 12.0, 0.0), this.size: Sizes.iconSize, this.color}) : super(key: key);
@override
_EntityIconState createState() => _EntityIconState();
}
class _EntityIconState extends State<EntityIcon> {
2018-11-14 19:52:17 +02:00
int getDefaultIconByEntityId(String entityId, String deviceClass, String state) {
String domain = entityId.split(".")[0];
String iconNameByDomain = MaterialDesignIcons.defaultIconsByDomains["$domain.$state"] ?? MaterialDesignIcons.defaultIconsByDomains["$domain"];
String iconNameByDeviceClass;
if (deviceClass != null) {
iconNameByDeviceClass = MaterialDesignIcons.defaultIconsByDeviceClass["$domain.$deviceClass.$state"] ?? MaterialDesignIcons.defaultIconsByDeviceClass["$domain.$deviceClass"];
}
String iconName = iconNameByDeviceClass ?? iconNameByDomain;
if (iconName != null) {
return MaterialDesignIcons.iconsDataMap[iconName] ?? 0;
} else {
return 0;
}
}
2019-03-02 20:13:24 +02:00
Widget buildIcon(HomeAssistantModel homeAssistantModel, EntityWrapper data, Color color) {
if (data == null) {
2019-03-02 20:13:24 +02:00
return Container(width: widget.size, height: widget.size,);
}
if ((data.entity.domain == "camera" || data.entity.domain == "media_player") && data.entity.thumbnailBase64 == null) {
homeAssistantModel.homeAssistant.updateEntityThumbnail(data.entity);
}
2019-03-02 20:13:24 +02:00
if (data.entity.thumbnailBase64 != null) {
return CircleAvatar(
radius: widget.size/2,
backgroundColor: Colors.white,
backgroundImage: MemoryImage(
Base64Codec().decode(data.entity.thumbnailBase64),
)
);
} else if (data.entity.entityPicture != null && data.entity.domain != "camera" && data.entity.domain != "media_player") {
return CircleAvatar(
2019-03-02 20:13:24 +02:00
radius: widget.size/2,
backgroundColor: Colors.white,
backgroundImage: CachedNetworkImageProvider(
"$homeAssistantWebHost${data.entity.entityPicture}",
),
);
} else {
String iconName = data.icon;
int iconCode = 0;
if (iconName.length > 0) {
iconCode = MaterialDesignIcons.getIconCodeByIconName(iconName);
} else {
iconCode = getDefaultIconByEntityId(data.entity.entityId,
data.entity.deviceClass, data.entity.state); //
}
return Icon(
IconData(iconCode, fontFamily: 'Material Design Icons'),
2019-03-02 20:13:24 +02:00
size: widget.size,
color: color,
);
}
}
2018-10-27 14:27:41 +03:00
@override
Widget build(BuildContext context) {
2018-11-24 00:37:55 +02:00
final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper;
2019-03-02 20:13:24 +02:00
final HomeAssistantModel homeAssistantModel = HomeAssistantModel.of(context);
2018-11-24 00:37:55 +02:00
return Padding(
2019-03-02 20:13:24 +02:00
padding: widget.padding,
child: buildIcon(
2019-03-02 20:13:24 +02:00
homeAssistantModel,
2018-11-24 00:37:55 +02:00
entityWrapper,
2019-03-02 20:13:24 +02:00
widget.color ?? EntityColor.stateColor(entityWrapper.entity.state)
2018-10-27 14:27:41 +03:00
),
);
}
}