Trying to dynamically set the Icon based on a JSON string value

前端 未结 3 901
时光取名叫无心
时光取名叫无心 2021-01-15 05:47

I have a client config that is on a server in JSON format.

example JSON would be like { \"icon\" : \"facebook\" }

I have the widget below.

          


        
3条回答
  •  醉梦人生
    2021-01-15 06:23

    Just follow the below steps to achieve.

    1. Download the package [icons_helper]: https://pub.dev/packages/icons_helper from flutter packages

    2. Just add below code wherever you want a dynamic icon using JSON.

      a) FontAwesome icon: getIconGuessFavorFA(name:"ICON_NAME")

      b) Material icon: getIconGuessFavorMaterial(name:"ICON_NAME")

    Example:

    ExpansionTile(
      leading:  Icon(getIconGuessFavorMaterial(name:root.menuIcon)),  //Icon(FontAwesomeIcons.dashcube),            
      key: PageStorageKey(root),
      title: Text(root.menuName),
      children: root.children.map(_buildTiles).toList(),
    )
    

    Note: If you face any error while complying like ('Error: Getter not found: 'haykal'.'). Just comment on the icon in 'icons_helper.dart'

提交回复
热议问题