Reputation: 513
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?
Upvotes: 2
Views: 3487
Reputation: 191
You can save the unicode data in the Json. For example :
static const IconData clock = const IconDataRegular(0xf017);
save f017 in the Json.
Later use the following function for conversion into an 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;
}
And finally use the following :
Icon(IconDataSolid(getHexFromStr(' f017')))
Upvotes: 1
Reputation: 27177
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.
Upvotes: 1