2

Want to call Icons.album with the string "album" like Icons["album"] but it's not work's. do I need to do enum for each icon or it was a other way ?

List tasks = [{
    "title": "task 1",
    "description": "description 1",
    "icon": "ring_volume",
  },{
    "title": "task 2",
    "description": "description 2",
    "icon": "flag",
  },{
    "title": "task 3",
    "description": "description 3",
    "icon": "album",
  },
  ];

[...]

  Widget taskCard(Map task) {
    Text title = Text(task['title']);
    return Center(
      child: Card(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
             ListTile(
              leading: Icons(Icons.album), // HERE
              title: new Text(task['title']),
              subtitle: new Text(task['description']),
            ),

          ],
        ),
      ),
    );

  }

2 Answers2

1

You cannot call the icons like these Icons["album"] but you can store IconData in the maps like this

List tasks = [{
    "title": "task 1",
    "description": "description 1",
    "icon": Icons.ring_volume,
  },{
    "title": "task 2",
    "description": "description 2",
    "icon": Icons.flag,
  },{
    "title": "task 3",
    "description": "description 3",
    "icon": Icons.album,
  },
  ];

then call it in your UI the like task['icon']

  Widget taskCard(Map task) {
    Text title = Text(task['title']);
    return Center(
      child: Card(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
             ListTile(
              leading: Icons(task['icon']), // HERE
              title: new Text(task['title']),
              subtitle: new Text(task['description']),
            ),

          ],
        ),
      ),
    );

  }
JideGuru
  • 7,102
  • 6
  • 26
  • 48
0

you should use this:

in dictionary:

"icon": Icons.album

and in widget:

leading: Icon(task["icon"])
Wehid Behrami
  • 226
  • 2
  • 6
  • thank's, it work's but it's hard to use widget with IconData type. I prefere storage a string – Cyril Ferté Aug 21 '20 at 16:29
  • You need to make a workaround to use strings. Take a look at this post: https://stackoverflow.com/a/44060511/4335775 – Rod Aug 21 '20 at 17:51
  • well, you can put all the widget in "icon" property to use easier. "icon": Icon(Icons.album) leading: task["icon"] – Wehid Behrami Aug 21 '20 at 18:57