It would be cool to have an easier Interface as an extension to your package which allows the extraction of the Icons like this:
///searches all libs and returns the first finding
IconData data = IconCache('altimeter')
///searches only Material Icons
IconData data = IconCache('altimeter', IconLib.mdi)
///searches only Material Icons
IconData data = IconCache('mdi.altimeter')
all methods return a default icon if no Icon is found (could be changed to null). This addresses #12
Here is the implimentation:
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:logging/logging.dart';
final _log = new Logger('ui.icon_cache.dart');
enum IconLib {
///All Icons
all,
///Ant Design Icons
ant,
///Entypo Icons
ent,
///Evil Icons
evi,
///Feather Icons
fea,
///Font Awesome Icons
faw,
///Foundation Icons
fou,
///Ionicons Icons
ion,
///Material Community Icons
mco,
///Material Icons
mdi,
///Octicons Icons
oct,
///Simple Line Icons
sli,
///Zocial Icons
zoc,
///Weather Icons
wea
}
class IconCache {
static final IconCache _singleton = new IconCache._internal();
factory IconCache() {
return _singleton;
}
IconCache._internal();
IconData operator [](String name) {
return _singleton.icon(name);
}
IconLib _fromString(String iconLibEnumPrefix) => IconLib.values.firstWhere(
(e) => e.toString().split('.').last == iconLibEnumPrefix,
orElse: null);
// The format is like 'mdi.car' which loads the car
// icon from the Material Design Icons
//if the name contains the prefix separated with a dot and the prefix
// is the default than the prefix is taken from the name. e.g. mdi.car
IconData icon(String name, {IconLib prefix = IconLib.all}) {
String iconName;
IconLib namePrefix;
//if the name contains the prefix separated with a dot and the prefix
// is the default than the prefix is taken from the name. e.g. mdi.car
if (name.contains('.') && prefix == IconLib.all) {
namePrefix = _fromString(name.split('.').first);
iconName = name.split('.').last;
//defaults to all lib
if (namePrefix == null || iconName == null) throw ArgumentError();
return _iconInternal(namePrefix, iconName);
} else {
return _iconInternal(prefix, name);
}
}
IconData _iconInternal(IconLib prefix, String name) {
IconData data;
if (prefix != IconLib.all) {
data = _extractIconFromSpecificLib(prefix, name);
if (data == null || data.codePoint == null) {
_log.finest('Could not load Icon $name ');
return MaterialIcons.getIconData('error');
} else {
return data;
}
}
return _searchIconsInAllLibs(data, name, prefix);
}
_searchIconsInAllLibs(IconData data, String name, IconLib prefix) {
try {
//return MdiIcons()[name];
data = _searchMaterialIcons(name);
if (data == null) {
data = _searchMaterialCommunityIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchMaterialCommunityIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchAntDesignIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchEntypoIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchEvilIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchFeatherIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchFontAwesomeIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchFoundationIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchIonIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchOcticonsIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchSimpleLineIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchZocialIcons(name);
} else {
return data;
}
if (data == null) {
data = _searchWeatherIcons(name);
} else {
return data;
}
if (data == null) {
_log.finest('Could not load Icon $name from lib: $prefix');
return MaterialIcons.getIconData('error');
} else {
_log.finest('Could not load Icon $name, from lib: $prefix}');
return MaterialIcons.getIconData('error');
}
} catch (e) {
_log.finest(
'Could not load Icon $name, from lib: $prefix, Error: ${e.toString()}');
return MaterialIcons.getIconData('error');
}
}
IconData _extractIconFromSpecificLib(IconLib prefix, String name) {
switch (prefix) {
case IconLib.ant:
return _searchAntDesignIcons(name);
break;
case IconLib.all:
_log.finest('Could not load Icon $name');
return MaterialIcons.getIconData('error');
break;
case IconLib.ent:
return _searchEntypoIcons(name);
break;
case IconLib.evi:
return _searchEvilIcons(name);
break;
case IconLib.fea:
return _searchFeatherIcons(name);
break;
case IconLib.faw:
return _searchFontAwesomeIcons(name);
break;
case IconLib.fou:
return _searchFoundationIcons(name);
break;
case IconLib.ion:
return _searchIonIcons(name);
break;
case IconLib.mco:
return _searchMaterialCommunityIcons(name);
break;
case IconLib.mdi:
return _searchMaterialIcons(name);
break;
case IconLib.oct:
return _searchOcticonsIcons(name);
break;
case IconLib.sli:
return _searchSimpleLineIcons(name);
break;
case IconLib.zoc:
return _searchZocialIcons(name);
break;
case IconLib.wea:
return _searchWeatherIcons(name);
break;
default:
{
_log.finest('Could not load Icon $name');
return MaterialIcons.getIconData('error');
}
}
}
IconData _searchWeatherIcons(String name) {
IconData data = WeatherIcons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchZocialIcons(String name) {
IconData data = Zocial.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchSimpleLineIcons(String name) {
IconData data = SimpleLineIcons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchOcticonsIcons(String name) {
IconData data = Octicons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchIonIcons(String name) {
IconData data = Ionicons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchFoundationIcons(String name) {
IconData data = Foundation.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchFontAwesomeIcons(String name) {
IconData data = FontAwesome.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchFeatherIcons(String name) {
IconData data = Feather.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchEvilIcons(String name) {
IconData data = EvilIcons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchEntypoIcons(String name) {
IconData data = Entypo.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchAntDesignIcons(String name) {
IconData data = AntDesign.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchMaterialIcons(String name) {
IconData data = MaterialIcons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
IconData _searchMaterialCommunityIcons(String name) {
IconData data = MaterialCommunityIcons.getIconData(name);
if (data == null || data.codePoint == null) {
return null;
} else {
return data;
}
}
}
It would be cool to have an easier Interface as an extension to your package which allows the extraction of the Icons like this:
all methods return a default icon if no Icon is found (could be changed to null). This addresses #12
Here is the implimentation: