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;
|
2019-02-21 16:32:55 +02:00
|
|
|
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
|
|
|
|
2019-02-21 16:32:55 +02:00
|
|
|
int getDefaultIconByEntityId(String entityId, String deviceClass, String state) {
|
2020-05-02 11:27:20 +03:00
|
|
|
if (entityId == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-21 16:32:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:01:37 +03:00
|
|
|
Widget buildIcon(BuildContext context, EntityWrapper data, Color color) {
|
|
|
|
Widget result;
|
2019-02-21 16:32:55 +02:00
|
|
|
if (data == null) {
|
2019-03-06 18:50:30 +02:00
|
|
|
return null;
|
2019-02-21 16:32:55 +02:00
|
|
|
}
|
2019-03-13 22:12:01 +02:00
|
|
|
if (data.entityPicture != null) {
|
2020-04-29 23:01:37 +03:00
|
|
|
result = Container(
|
2019-03-10 22:50:39 +02:00
|
|
|
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-02-21 16:32:55 +02:00
|
|
|
} else {
|
2020-04-29 23:04:59 +03:00
|
|
|
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); //
|
|
|
|
}
|
|
|
|
result = Icon(
|
|
|
|
IconData(iconCode, fontFamily: 'Material Design Icons'),
|
|
|
|
size: size,
|
|
|
|
color: color,
|
2020-04-29 23:01:37 +03:00
|
|
|
);
|
2020-04-29 23:04:59 +03:00
|
|
|
if (data.entity is LightEntity &&
|
|
|
|
(data.entity as LightEntity).supportColor &&
|
|
|
|
(data.entity as LightEntity).color != null
|
|
|
|
) {
|
|
|
|
Color lightColor = (data.entity as LightEntity).color.toColor();
|
|
|
|
if (lightColor == Colors.white) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result = Stack(
|
|
|
|
children: <Widget>[
|
|
|
|
result,
|
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
child: Container(
|
|
|
|
width: size / 3,
|
|
|
|
height: size / 3,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: lightColor,
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
boxShadow: <BoxShadow>[
|
|
|
|
BoxShadow(
|
|
|
|
spreadRadius: 0,
|
|
|
|
blurRadius: 0,
|
|
|
|
offset: Offset(0.3, 0.3)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2020-04-29 23:01:37 +03:00
|
|
|
}
|
|
|
|
return result;
|
2019-02-21 16:32:55 +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;
|
2020-04-29 00:03:00 +03:00
|
|
|
Color iconColor;
|
|
|
|
if (color != null) {
|
|
|
|
iconColor = color;
|
|
|
|
} else if (entityWrapper.stateColor) {
|
|
|
|
iconColor = HAClientTheme().getColorByEntityState(entityWrapper.entity.state, context);
|
|
|
|
} else {
|
|
|
|
iconColor = HAClientTheme().getOffStateColor(context);
|
|
|
|
}
|
2018-11-24 00:37:55 +02:00
|
|
|
return Padding(
|
2019-03-06 18:50:30 +02:00
|
|
|
padding: padding,
|
2019-02-21 16:32:55 +02:00
|
|
|
child: buildIcon(
|
2020-04-29 23:01:37 +03:00
|
|
|
context,
|
2020-04-27 23:56:33 +03:00
|
|
|
entityWrapper,
|
2020-04-29 00:03:00 +03:00
|
|
|
iconColor
|
2018-10-27 14:27:41 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|