passing string as FontAwesomeIcons

2019-08-21 01:17发布

问题:

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

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?

for example:

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?

回答1:

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

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;

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.



标签: dart flutter