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

74 lines
2.3 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-10 22:50:39 +02:00
const EntityIcon({Key key, this.color, this.size: Sizes.iconSize, this.padding: const EdgeInsets.all(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-13 22:12:01 +02:00
if (data.entityPicture != null) {
2019-03-10 22:50:39 +02:00
return Container(
height: size+12,
width: size+12,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit:BoxFit.cover,
image: CachedNetworkImageProvider(
2019-03-13 22:12:01 +02:00
"${data.entityPicture}"
2019-03-10 22:50:39 +02:00
),
)
),
);
2019-03-13 20:07:44 +02:00
}
String iconName = data.icon;
int iconCode = 0;
if (iconName.length > 0) {
iconCode = MaterialDesignIcons.getIconCodeByIconName(iconName);
} else {
2019-03-13 20:07:44 +02:00
iconCode = getDefaultIconByEntityId(data.entity.entityId,
data.entity.deviceClass, data.entity.state); //
}
return Padding(
padding: EdgeInsets.fromLTRB(6.0, 6.0, 6.0, 6.0),
child: Icon(
2019-03-10 22:50:39 +02:00
IconData(iconCode, fontFamily: 'Material Design Icons'),
size: size,
color: color,
)
2019-03-13 20:07:44 +02:00
);
}
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,
2020-04-05 00:39:12 +03:00
color ?? HAClientTheme().getColorByEntityState(entityWrapper.entity.state, context)
2018-10-27 14:27:41 +03:00
),
);
}
}