Dart 将字符串作为FontAwesomeIcons传递
我在我的应用程序中使用来显示一些图标 我从json获取图标的名称作为字符串。。如何将其传递给图标 有办法做到这一点吗 例如: 我从json得到:Dart 将字符串作为FontAwesomeIcons传递,dart,flutter,Dart,Flutter,我在我的应用程序中使用来显示一些图标 我从json获取图标的名称作为字符串。。如何将其传递给图标 有办法做到这一点吗 例如: 我从json得到: String icon = 'ad'; 然后我想这样使用它: new Icon(FontAwesomeIcons.icon), 我知道这样不行。。但是我该怎么做呢?这可行吗 我找到了一种可能对你有帮助的方法。编辑font\u awesome\u flatter.dart文件,如下所示,也可访问 我只是用两个图标来演示,你可以根据需要继续使用,也可以
String icon = 'ad';
然后我想这样使用它:
new Icon(FontAwesomeIcons.icon),
我知道这样不行。。但是我该怎么做呢?这可行吗 我找到了一种可能对你有帮助的方法。编辑font\u awesome\u flatter.dart文件,如下所示,也可访问 我只是用两个图标来演示,你可以根据需要继续使用,也可以全部使用 font\u awesome\u flatter.dart 图书馆字体(真棒)(颤振),
import 'package:flutter/widgets.dart';
import 'package:font_awesome_flutter/icon_data.dart';
// THIS FILE IS AUTOMATICALLY GENERATED!
class FontAwesomeIcons {
static const createDoc = {
'fiveHundredPx': IconDataBrands(0xf26e),
'accessibleIcon': IconDataBrands(0xf368),
//.......add all Icons HERE
};
static const IconData fiveHundredPx = const IconDataBrands(0xf26e);
static const IconData accessibleIcon = const IconDataBrands(0xf368);
static const IconData accusoft = const IconDataBrands(0xf369);
static const IconData acquisitionsIncorporated = const IconDataBrands(0xf6af);
static const IconData ad = const IconDataSolid(0xf641);
static const IconData addressBook = const IconDataRegular(0xf2b9);
static const IconData solidAddressBook = const IconDataSolid(0xf2b9);
static const IconData addressCard = const IconDataRegular(0xf2bb);
static const IconData solidAddressCard = const IconDataSolid(0xf2bb);
//.......
//.......add all Icons HERE To as already in your file
}
现在,您可以使用以下代码:
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() {
runApp(new FontAwesomeGalleryApp());
}
class FontAwesomeGalleryApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Font Awesome Flutter Gallery',
theme: new ThemeData.light().copyWith(
iconTheme: new IconThemeData(size: 36.0, color: Colors.black87),
textTheme: new TextTheme(
body1: new TextStyle(fontSize: 16.0, color: Colors.black87),
),
),
home: new Home(),
);
}
}
class Home extends StatelessWidget {
String data = 'fiveHundredPx';
@override
Widget build(BuildContext context) {
return Scaffold(
body: new Container(
child: Center(
child: Icon(FontAwesomeIcons.createDoc[data.toString()]),
),
),
);
}
}
我知道这种方法有点难,但我只找到了这种方法。您可以将unicode数据保存在Json中。例如:
static const IconData clock = const IconDataRegular(0xf017);
将f017保存在Json中
稍后使用以下函数转换为int
int getHexFromStr(String fontCode) {
int val = 0;
int len = fontCode.length;
for (int i = 0; i < len; i++) {
int hexDigit = fontCode.codeUnitAt(i);
if (hexDigit >= 48 && hexDigit <= 57) {
val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 65 && hexDigit <= 70) {
// A..F
val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 97 && hexDigit <= 102) {
// a..f
val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
} else {
throw new FormatException("An error occurred when converting");
}
}
return val;
}
Icon(IconDataSolid(getHexFromStr(' f017')))