如何在 Flutter 中将 String 值转换为 Icons 值,我从 json 获取 Icon 值作为字符串。
当我尝试使用该值时出现以下错误
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)
Run Code Online (Sandbox Code Playgroud)
{
"page": 1,
"MenuItems": [
{
"id": 419701,
"icon": "MdiIcons.account",
"name": "account"
},
{
"id": 419702,
"icon": "MdiIcons.currencyUsd",
"name": "Funds"
},
{
"id": 419703,
"icon": "MdiIcons.home",
"name": "home"
}
]
}
Run Code Online (Sandbox Code Playgroud)
一种方法是您可以将图像生成为字体图标(生成为字体)。将 ttf 文件保存在资产中。将 unicode 数据传递给 json(如“e90a”)。
例如:
Icon(IconData(int.parse('0x${e90a}',
fontFamily: 'family name given in the link above'));
Run Code Online (Sandbox Code Playgroud)
您可以通过使用 Icon 类常量来实现。
Icon(IconData(61668, fontFamily: 'MaterialIcons'));
Run Code Online (Sandbox Code Playgroud)
查看更多 Icon 类常量:https : //api.flutter.dev/flutter/material/Icons-class.html#constants
归档时间: |
|
查看次数: |
7785 次 |
最近记录: |