Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Icons in Flutter

I retrieve some data from my DB, where I have a Column called "icon" in which I stored some Strings. For each of them, I want to pass in the Icon class of Flutter, charging the corresponding Icon. I have the single String inside

items[index]["icon"]

But I can't pass it inside Icon(items[index]["icon"]) How can I do?

like image 864
Edoardo Tavilla Avatar asked Dec 03 '22 18:12

Edoardo Tavilla


1 Answers

You can use the icons directly with their literal names by accessing the Material Icons font directly with Text() widget.

Example:

Text(
    'perm_contact_calendar',
    style: TextStyle(fontFamily: 'MaterialIcons'),
);

You may use it for custom Icon class to integrate with Material Framework smoothly, but in this case you will need a --no-tree-shake-icons flag for build command since non-constant IconData constructor breaks icon tree shaking.

like image 82
Sominemo Avatar answered Jan 12 '23 01:01

Sominemo