将字符串作为FontAwesomeIcons传递 [英] passing string as FontAwesomeIcons

查看:81
本文介绍了将字符串作为FontAwesomeIcons传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

am using font_awesome_flutter in my app to display some icons..

am从json获取字符串形式的图标名称..我如何将其传递给图标?

am getting the name of the icons from json as strings .. how i can pass it to the icon?

有一个实现此目标的方法?

there is a way to achieve this?

例如:

am从json获取:

am getting from the json:

String icon = 'ad';

然后我要像这样使用它:

and then i want to use it like this:

new Icon(FontAwesomeIcons.icon),

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

i know it doesn't work like this .. but how can i do this? is it doable?

推荐答案

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

I found one way may that help you. edit font_awesome_flutter.dart file as following and also access as below.

我只演示了两个Icon,您可以继续进行尽可能多的操作

I just demonstrate with two Icon you can go on with as much as you need or for all.

font_awesome_flutter.dart

library font_awesome_flutter;

library 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
  }

现在您可以使用以下代码:

Now you can use as Following Code:

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

我知道这种方式有点难,但我只能通过这种方式找到。

I know That This Way is little bit tough but i found this way only.

这篇关于将字符串作为FontAwesomeIcons传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆