5

I'm wondering is it possible to use unicode to display icon with FontAwesome package. If it is, how to achieve it?

What is may problem exactly:

Here's my FontAwesome use and I need to replace IconData with string with unicode


CategoriesWidget(
              icon: FontAwesomeIcons.fighterJet, // need to replace fighterJet to it's unicode (f0fb)
              color: Colors.purple[400],
              category: "Military", // it's going to be model.title (from JSON title field)
            ),

Why I want to do this? I'm passing category data through JSON and I have to make icon possible to change according to data passed.

Part of JSON

title: "Military"
icon: "f0fb" // I can change it directly to name like fighterJet but I cannot use it while it's String not IconData

1 Answer 1

8

If you take a look into FontAwesomeIcons class you can see that all the icons are IconData constants. Your unicode is found under:

  static const IconData fighterJet = const IconDataSolid(0xf0fb);

So basically if you want to use the unicode to get the icon you can use:

String iconCode = 'f0fb';
...
Icon(IconDataSolid(int.parse('0x$iconCode')))

EDIT: There are multiple IconData classes in icon_data.dart file from font_awesome_flutter library, if you are using multiple icons from different families it would be best to also store in json the family of the icon and use it directly with the IconData constructor. It would look like this:

Icon(IconData(
    0xf0fb,
    fontFamily: 'FontAwesomeSolid',
    fontPackage: 'font_awesome_flutter',
  ))
Sign up to request clarification or add additional context in comments.

3 Comments

So I understand that solution will be something like this: 1. pass unicode in icon field in JSON (0xf0fb) 2. Than parse string to int and then pass to IconDataSolid Edit: I see than you wrote this in sec ago :)
Yes. Please also take in consideration the edit that I wrote
Luckily for me, I am using only one family

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.