Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MostHated opened this issue May 14, 2020 · 2 comments
Open

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

MostHated opened this issue May 14, 2020 · 2 comments

Comments

@MostHated
Copy link

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
Copy link
Contributor

2534290808 commented May 22, 2020

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
Copy link

@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>')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants