gpt4 book ai didi

dart - 将字符串作为Font Awesome Icons传递

转载 作者:行者123 更新时间:2023-12-03 03:01:51 25 4
gpt4 key购买 nike

正在我的应用程序中使用font_awesome_flutter来显示一些图标。

我正在从json获取图标的名称作为字符串..我如何将其传递给图标?

有没有办法实现这一目标?

例如:

从json中获取:

String icon = 'ad';

然后我想这样使用它:
new Icon(FontAwesomeIcons.icon),

我知道它不能像这样工作..但是我该怎么做?这可行吗?

最佳答案

我发现一种可以帮助您的方法。如下编辑 font_awesome_flutter.dart 文件,并按以下方式访问。

我只演示了两个Icon,您可以根据需要继续进行所有操作。

font_awesome_flutter.dart

库font_awesome_flutter;

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()]),
),
),
);
}
}

我知道“以这种方式”有点困难,但我只是以这种方式发现。

关于dart - 将字符串作为Font Awesome Icons传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52929254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com