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

67 lines
2.2 KiB
Dart
Raw Normal View History

2018-10-27 14:27:41 +03:00
part of '../main.dart';
2019-03-06 18:50:30 +02:00
class EntityIcon extends StatelessWidget {
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-06 18:50:30 +02:00
const EntityIcon({Key key, this.color, this.size: Sizes.iconSize, this.padding: const EdgeInsets.fromLTRB(
Sizes.leftWidgetPadding, 0.0, 12.0, 0.0)}) : super(key: key);
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-06 18:50:30 +02:00
Widget buildIcon(EntityWrapper data, Color color) {
if (data == null) {
2019-03-06 18:50:30 +02:00
return null;
}
2019-03-06 18:50:30 +02:00
if (data.entity.entityPicture != null) {
return CircleAvatar(
2019-03-06 18:50:30 +02:00
radius: 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-06 18:50:30 +02:00
size: 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;
return Padding(
2019-03-06 18:50:30 +02:00
padding: padding,
child: buildIcon(
2018-11-24 00:37:55 +02:00
entityWrapper,
2019-03-06 18:50:30 +02:00
color ?? EntityColor.stateColor(entityWrapper.entity.state)
2018-10-27 14:27:41 +03:00
),
);
}
}