flutter-studio / flutter-icons

Customizable Icons for Flutter :boom:
Apache License 2.0
332 stars 123 forks source link

Is there a way to get an icon from a string value? #30

Open MostHated opened 4 years ago

MostHated commented 4 years ago

Hey there, I had been using another library before that had some nice helper functions, as seen below:

IconData getIconUsingPrefix({String name}) {
  final List<String> split = name.split('.');

  if (split.length > 1) {
    name = split[1];
    if (split[0].toLowerCase() == 'fa' || split[0].toLowerCase() == 'fontawesome') {
      return getFontAwesomeIcon(name: name);
    }
  } else {
    return getIconGuessFavorMaterial(name: name);
  }

  return getMaterialIcon(name: name);
}

// Returns an icon named in name favoring Font Awesome
IconData getIconGuessFavorFA({String name}) {
  if (FontAwesomeIconsMap[name] != null) {
    return FontAwesomeIconsMap[name];
  } else {
    return IconsMap[name];
  }
}

// Returns an icon named in name favoring Material
IconData getIconGuessFavorMaterial({String name}) {
  if (IconsMap[name] != null) {
    return IconsMap[name];
  } else {
    return FontAwesomeIconsMap[name];
  }
}

It would let me retrieve an icon based on a string, which was perfect as all of my data is built from retrieving json data from my site. I have been looking for a larger variety of icons, so this package looked great, but I am having issue trying to get them as I have been prior.

I tried to see if I could use something like this:

  CircleAvatar(
      child: (changeLog.icon != "")
          ? Icon(FlutterIcons['${changeLog.icon}'], color: Colors.orange.withOpacity(0.5))
          : Icon(backupIconList[index])),

Is there anything available that I just didn't see, or that can be added to this package similar to this? Thanks, -MH

2534290808 commented 4 years ago

I don't recommend getting the icon this way. I recommend using the static property of the class, such asAntDesign.stepforward,

This has the following advantages:

1. You can get friendly hints from editors, such as Android studio
2. If you can get the static property, the icon must exist
3. You can get icons of type const

If you use a string to get the icon, you won't get any of these advantages

pushpendraKh commented 4 years ago

@2534290808 Agreed on the advantages.

But, what about the case where one is getting an icon from the backend. Backend is ensuring to send the icons which actually exist. In that case, it will be beneficial to map the icons with string. Something like FlutterIcon.getIconData('<name-of-icon>')