Skip to content

Commit fb223af

Browse files
Quick fix to use Sharp Icons
--Assuming you have set up Pro-- Copy the contents of icon-families.json into the lib/fonts/icons.json This code is backwards compatible so that if you use icons.json it will pull out the relevant fields. icon-families.json doesn't have the styles tag so instead we can iterate over the svgs keys of "classic" and check if it has "sharp" This also means that the key for sharp is now "sharp <thin, light, regular, solid> name" so we have to do some manipulation to remove the empty space to map the Class names correctly and to un-comment out the relevant pubspec.yaml files. I hope this helps someone out to start using Sharp Icons in this.
1 parent b03974f commit fb223af

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

lib/src/icon_data.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,55 @@ class IconDataThin extends IconData {
8383
fontPackage: 'font_awesome_flutter',
8484
);
8585
}
86+
87+
/// [IconData] for a font awesome sharp thin icon from a code point. Only works if
88+
/// thin icons (font awesome pro, v6+) have been installed.
89+
///
90+
/// Code points can be obtained from fontawesome.com
91+
class IconDataSharpThin extends IconData {
92+
const IconDataSharpThin(int codePoint)
93+
: super(
94+
codePoint,
95+
fontFamily: 'FontAwesomeSharpThin',
96+
fontPackage: 'font_awesome_flutter',
97+
);
98+
}
99+
100+
/// [IconData] for a font awesome sharp light icon from a code point. Only works if
101+
/// thin icons (font awesome pro, v6+) have been installed.
102+
///
103+
/// Code points can be obtained from fontawesome.com
104+
class IconDataSharpLight extends IconData {
105+
const IconDataSharpLight(int codePoint)
106+
: super(
107+
codePoint,
108+
fontFamily: 'FontAwesomeSharpLight',
109+
fontPackage: 'font_awesome_flutter',
110+
);
111+
}
112+
113+
/// [IconData] for a font awesome sharp regular icon from a code point. Only works if
114+
/// thin icons (font awesome pro, v6+) have been installed.
115+
///
116+
/// Code points can be obtained from fontawesome.com
117+
class IconDataSharpRegular extends IconData {
118+
const IconDataSharpRegular(int codePoint)
119+
: super(
120+
codePoint,
121+
fontFamily: 'FontAwesomeSharpRegular',
122+
fontPackage: 'font_awesome_flutter',
123+
);
124+
}
125+
126+
/// [IconData] for a font awesome sharp solid icon from a code point. Only works if
127+
/// thin icons (font awesome pro, v6+) have been installed.
128+
///
129+
/// Code points can be obtained from fontawesome.com
130+
class IconDataSharpSolid extends IconData {
131+
const IconDataSharpSolid(int codePoint)
132+
: super(
133+
codePoint,
134+
fontFamily: 'FontAwesomeSharpSolid',
135+
fontPackage: 'font_awesome_flutter',
136+
);
137+
}

pubspec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ flutter:
4444
# fonts:
4545
# - asset: lib/fonts/fa-thin-100.ttf
4646
# weight: 100
47+
# - family: FontAwesomeSharpThin
48+
# fonts:
49+
# - asset: lib/fonts/fa-sharp-thin-100.ttf
50+
# weight: 100
51+
# - family: FontAwesomeSharpLight
52+
# fonts:
53+
# - asset: lib/fonts/fa-sharp-light-300.ttf
54+
# weight: 300
55+
# - family: FontAwesomeSharpRegular
56+
# fonts:
57+
# - asset: lib/fonts/fa-sharp-regular-400.ttf
58+
# weight: 400
59+
# - family: FontAwesomeSharpSolid
60+
# fonts:
61+
# - asset: lib/fonts/fa-sharp-solid-900.ttf
62+
# weight: 900

util/lib/main.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void adjustPubspecFontIncludes(Set<String> styles) {
194194
if (!line.trimLeft().startsWith('- family:')) continue;
195195

196196
styleName = line.substring(25).toLowerCase(); // - family: FontAwesomeXXXXXX
197-
if (styles.contains(styleName)) {
197+
if (styles.any((element) => element.replaceAll(' ', '') == styleName)) { //Because of 'sharp thin' we need to remove spaces here
198198
pubspec[i] = uncommentYamlLine(pubspec[i]);
199199
pubspec[i + 1] = uncommentYamlLine(pubspec[i + 1]);
200200
pubspec[i + 2] = uncommentYamlLine(pubspec[i + 2]);
@@ -445,7 +445,7 @@ String normalizeIconName(String iconName, String style, int styleCompetitors) {
445445

446446
/// Utility function to generate the correct 'IconData' subclass for a [style]
447447
String styleToDataSource(String style) {
448-
return 'IconData${style[0].toUpperCase()}${style.substring(1)}';
448+
return 'IconData${style.split(' ').map((word) => word.isNotEmpty ? word[0].toUpperCase() + word.substring(1) : '').toList().join('')}';
449449
}
450450

451451
/// Gets the default branch from github's metadata
@@ -556,13 +556,25 @@ bool readAndPickMetadata(File iconsJson, List<IconMetadata> metadata,
556556
versions.add(v);
557557
}
558558

559-
List<String> iconStyles = (icon['styles'] as List).cast<String>();
560-
559+
List<String> iconStyles = [];
560+
if (icon.containsKey("styles")) {
561+
iconStyles = (icon['styles'] as List).cast<String>();
562+
} else if (icon.containsKey("svgs")) {
563+
iconStyles.addAll((icon['svgs']['classic'] as Map<String, dynamic>).keys);
564+
if (icon['svgs']?['sharp'] != null) {
565+
iconStyles.addAll((icon['svgs']['sharp'] as Map<String, dynamic>).keys.map((key) => 'sharp $key')); //"sharp thin ..."
566+
}
567+
}
561568
//TODO: Remove line once duotone support discontinuation notice is removed
562569
if (iconStyles.contains('duotone')) hasDuotoneIcons = true;
563570

564571
for (var excluded in excludedStyles) {
565-
iconStyles.remove(excluded);
572+
if (excluded == 'sharp') {
573+
//Since it's 'sharp thin' then remove any containing sharp
574+
iconStyles.removeWhere((element) => element.contains('sharp'));
575+
} else {
576+
iconStyles.remove(excluded);
577+
}
566578
}
567579

568580
if (iconStyles.isEmpty) continue;
@@ -628,7 +640,7 @@ ArgParser setUpArgParser() {
628640
argParser.addMultiOption('exclude',
629641
abbr: 'e',
630642
defaultsTo: [],
631-
allowed: ['brands', 'regular', 'solid', 'duotone', 'light', 'thin'],
643+
allowed: ['brands', 'regular', 'solid', 'duotone', 'light', 'thin', 'sharp'],
632644
help: 'icon styles which are excluded by the generator');
633645

634646
argParser.addFlag('dynamic',

0 commit comments

Comments
 (0)