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

Icons: add method fromName(String iconName) #147434

Closed
javaone199 opened this issue Apr 26, 2024 · 3 comments
Closed

Icons: add method fromName(String iconName) #147434

javaone199 opened this issue Apr 26, 2024 · 3 comments
Labels
r: duplicate Issue is closed as a duplicate of an existing issue

Comments

@javaone199
Copy link

Use case

Could not reopen the issue #145302.

Icon names are retrieved from server.
So a method for getting IconData from icon name is needed.

Icons.fromName(iconName)

iconName is the same as the constant name in Icons class.

pub.dev package material_icon_design_flutter does not contain many of the IconData constants defined in Icons, so it is not designed to provide name mappings for the Icons class.

Proposal

Add a new method for mapping names to IconData constants:

IconData Icons.fromName(iconName)

IconName are those already defined as constants such as more_vert.

IconData fromName(String iconName) {
   if (iconName=='more_vert') return Icons.more_vert;
   ....
}

or build a map from names to IconData constants.

@osama383
Copy link
Contributor

Hi. Interesting use case. Here is a possible solution.

Icon(Icons.more_vert);

If we look at the Icon constructor invoked above in the source code, we will see that the Icon class accepts an argument of type IconData and so Icons.more_vert is actually of type IconData.

In fact, Icons.more_vert is defined as follows:

static const IconData more_vert = IconData(0xe404, fontFamily: 'MaterialIcons');

If we next look at the constructor of the IconData class that is being invoked above, it reads as follows:

const IconData(
    this.codePoint, {
    this.fontFamily,
    this.fontPackage,
    this.matchTextDirection = false,
    this.fontFamilyFallback,
  });

Which means we really only need two pieces of information: the codePoint which is of type int and fontFamily which is of type string.

Instead of saving the string 'more_vert' in your database, you can save these two values and use them to build your icon.

int codePoint = Icons.more_vert.codePoint;
String? fontFamily = Icons.more_vert.fontFamily;

Icon icon = Icon(IconData(codePoint, fontFamily: fontFamily));

@javaone199
Copy link
Author

From the IconData doc, instantiating IconData instead of using the predefined IconData constants will cause tree-shaking issue.

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Apr 29, 2024
@darshankawar
Copy link
Member

@javaone199
I'll reopen the original issue. Just make sure you respond to the question asked by the team member in that issue.
#145302 (comment)
Closing this issue.

@darshankawar darshankawar closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
@darshankawar darshankawar added r: duplicate Issue is closed as a duplicate of an existing issue and removed in triage Presently being triaged by the triage team labels Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
r: duplicate Issue is closed as a duplicate of an existing issue
Projects
None yet
Development

No branches or pull requests

3 participants