Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter String to Icon value

Tags:

flutter

how to convert String value to Icons value in Flutter, i'm getting Icon value from json as a String.

I got following error when i'm tried to Use that value

error: The argument type 'String' can't be assigned to the parameter type 'IconData'. (argument_type_not_assignable at [hippo] lib\screens\dynamic_list.dart:71)
{
  "page": 1,
  "MenuItems": [
    {
      "id": 419701,
      "icon": "MdiIcons.account",
      "name": "account"
    },
    {
      "id": 419702,
      "icon": "MdiIcons.currencyUsd",
      "name": "Funds"
    },
    {
      "id": 419703,
      "icon": "MdiIcons.home",
      "name": "home"
    }
  ]
}
like image 811
Gokul Sundar Avatar asked Jan 22 '20 06:01

Gokul Sundar


3 Answers

You can do it by using Icon class constants according to official Flutter docs.

Icon(IconData(61668, fontFamily: 'MaterialIcons'));

Check more Icon class constants here: https://api.flutter.dev/flutter/material/Icons-class.html#constants

https://api.flutter.dev/flutter/material/Icons-class.html#constants

like image 62
Muhammad Nabeel Avatar answered Nov 04 '22 03:11

Muhammad Nabeel


you can use Icon class constants according to official Flutter docs. (https://api.flutter.dev/flutter/material/Icons-class.html#constants)

example:IconData(0xf518, fontFamily: 'MaterialIcons')

also you can generate custom images to font icon (Generate to font). save ttf file in assets. pass unicode data (like "e90a").

example:

Icon(IconData(int.parse('0x${e90a}',
    fontFamily: 'family name given in the link above'));
like image 35
Mary Avatar answered Nov 04 '22 02:11

Mary


use flutter_remote_icon

var remoteIconData = new RemoteIconData("material://Icons.add"); // -> native material icons remotely (dynamically)  

return RemoteIcon(icon: remoteIconData);

https://github.com/softmarshmallow/remote-ui/tree/master/flutter/packages/flutter_remote_icon

https://pub.dev/packages/flutter_remote_icon

like image 44
softmarshmallow Avatar answered Nov 04 '22 02:11

softmarshmallow