WIP #308 Move entity icon generation into EntityIcon widget
This commit is contained in:
		| @@ -22,7 +22,7 @@ class ButtonEntityContainer extends StatelessWidget { | ||||
|               fit: BoxFit.fitHeight, | ||||
|               child: EntityIcon( | ||||
|                 padding: EdgeInsets.fromLTRB(2.0, 6.0, 2.0, 2.0), | ||||
|                 iconSize: Sizes.iconSize, | ||||
|                 size: Sizes.iconSize, | ||||
|               ) | ||||
|             ), | ||||
|           ), | ||||
|   | ||||
| @@ -27,14 +27,20 @@ class BadgeWidget extends StatelessWidget { | ||||
|       case "media_player": | ||||
|       case "binary_sensor": | ||||
|         { | ||||
|           badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData( | ||||
|               entityModel.entityWrapper, iconSize, Colors.black); | ||||
|           badgeIcon = EntityIcon( | ||||
|             padding: EdgeInsets.all(0.0), | ||||
|             size: iconSize, | ||||
|             color: Colors.black | ||||
|           ); | ||||
|           break; | ||||
|         } | ||||
|       case "device_tracker": | ||||
|         { | ||||
|           badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData( | ||||
|               entityModel.entityWrapper, iconSize, Colors.black); | ||||
|           badgeIcon = EntityIcon( | ||||
|               padding: EdgeInsets.all(0.0), | ||||
|               size: iconSize, | ||||
|               color: Colors.black | ||||
|           ); | ||||
|           onBadgeTextValue = entityModel.entityWrapper.entity.state; | ||||
|           break; | ||||
|         } | ||||
|   | ||||
| @@ -3,20 +3,64 @@ part of '../main.dart'; | ||||
| class EntityIcon extends StatelessWidget { | ||||
|  | ||||
|   final EdgeInsetsGeometry padding; | ||||
|   final double iconSize; | ||||
|   final double size; | ||||
|   final Color color; | ||||
|  | ||||
|   const EntityIcon({Key key, this.iconSize: Sizes.iconSize, this.padding: const EdgeInsets.fromLTRB( | ||||
|   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); | ||||
|  | ||||
|   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; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   Widget buildIcon(EntityWrapper data, Color color) { | ||||
|     if (data == null) { | ||||
|       return null; | ||||
|     } | ||||
|     if (data.entity.entityPicture != null) { | ||||
|       return CircleAvatar( | ||||
|         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'), | ||||
|         size: size, | ||||
|         color: color, | ||||
|       ); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
|     final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper; | ||||
|     return Padding( | ||||
|       padding: padding, | ||||
|       child: MaterialDesignIcons.createIconWidgetFromEntityData( | ||||
|       child: buildIcon( | ||||
|           entityWrapper, | ||||
|           iconSize, | ||||
|           EntityColor.stateColor(entityWrapper.entity.state) | ||||
|           color ?? EntityColor.stateColor(entityWrapper.entity.state) | ||||
|       ), | ||||
|     ); | ||||
|   } | ||||
|   | ||||
| @@ -35,7 +35,7 @@ class GlanceEntityContainer extends StatelessWidget { | ||||
|     result.add( | ||||
|       EntityIcon( | ||||
|         padding: EdgeInsets.all(0.0), | ||||
|         iconSize: iconSize, | ||||
|         size: iconSize, | ||||
|       ) | ||||
|     ); | ||||
|     if (!nameInTheBottom) { | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| part of 'main.dart'; | ||||
|  | ||||
| class MaterialDesignIcons { | ||||
|   static Map _defaultIconsByDomains = { | ||||
|   static final Map defaultIconsByDomains = { | ||||
|     "light": "mdi:lightbulb", | ||||
|     "switch": "mdi:flash", | ||||
|     "binary_sensor": "mdi:checkbox-blank-circle-outline", | ||||
| @@ -34,7 +34,7 @@ class MaterialDesignIcons { | ||||
|     "alarm_control_panel" : "mdi:bell" | ||||
|   }; | ||||
|  | ||||
|   static Map _defaultIconsByDeviceClass = { | ||||
|   static final Map defaultIconsByDeviceClass = { | ||||
|     //"binary_sensor.battery": "mdi:", //TODO | ||||
|     "binary_sensor.cold.on": "mdi:snowflake", | ||||
|     "binary_sensor.cold.off": "mdi:thermometer", | ||||
| @@ -92,7 +92,7 @@ class MaterialDesignIcons { | ||||
|     "cover.window.closing": "mdi:window-open", | ||||
|     "cover.window.opening": "mdi:window-open", | ||||
|   }; | ||||
|   static Map _iconsDataMap = { | ||||
|   static final Map iconsDataMap = { | ||||
|     "mdi:access-point": 0xf002, | ||||
|     "mdi:access-point-network": 0xf003, | ||||
|     "mdi:account": 0xf004, | ||||
| @@ -2890,7 +2890,7 @@ class MaterialDesignIcons { | ||||
|     "mdi:blank": 0xf68c | ||||
|   }; | ||||
|  | ||||
|   static Widget createIconWidgetFromEntityData(EntityWrapper data, double size, Color color) { | ||||
|   /*static Widget createIconWidgetFromEntityData(EntityWrapper data, double size, Color color) { | ||||
|     if (data == null) { | ||||
|       return null; | ||||
|     } | ||||
| @@ -2921,7 +2921,7 @@ class MaterialDesignIcons { | ||||
|         color: color, | ||||
|       ); | ||||
|     } | ||||
|   } | ||||
|   }*/ | ||||
|  | ||||
|   static IconData createIconDataFromIconCode(int code) { | ||||
|     return IconData(code, fontFamily: 'Material Design Icons'); | ||||
| @@ -2932,10 +2932,10 @@ class MaterialDesignIcons { | ||||
|   } | ||||
|  | ||||
|   static int getIconCodeByIconName(String name) { | ||||
|     return _iconsDataMap[name] ?? 0; | ||||
|     return iconsDataMap[name] ?? 0; | ||||
|   } | ||||
|  | ||||
|   static int getDefaultIconByEntityId(String entityId, String deviceClass, String state) { | ||||
|   /*static int getDefaultIconByEntityId(String entityId, String deviceClass, String state) { | ||||
|     String domain = entityId.split(".")[0]; | ||||
|     String iconNameByDomain = _defaultIconsByDomains["$domain.$state"] ?? _defaultIconsByDomains["$domain"]; | ||||
|     String iconNameByDeviceClass; | ||||
| @@ -2948,6 +2948,6 @@ class MaterialDesignIcons { | ||||
|     } else { | ||||
|       return 0; | ||||
|     } | ||||
|   } | ||||
|   }*/ | ||||
|  | ||||
| } | ||||
| @@ -149,7 +149,7 @@ class CardWidget extends StatelessWidget { | ||||
|           mainAxisAlignment: MainAxisAlignment.end, | ||||
|           children: [ | ||||
|             EntityIcon( | ||||
|               iconSize: 50.0, | ||||
|               size: 50.0, | ||||
|             ), | ||||
|             Container( | ||||
|               width: 26.0, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user