Skip to content

Commit 089eecc

Browse files
fix: loading fonts performance issue (#170)
* adds local cache to know when a font family was already loaded to avoid processing * adds multiple icons tests
1 parent d7ad4bb commit 089eecc

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import 'package:alchemist/alchemist.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
group('Multiple icons test >', () {
7+
final iconsMap = <String, IconData>{
8+
'add': Icons.add,
9+
'alarm': Icons.alarm,
10+
'android': Icons.android,
11+
'arrow_back': Icons.arrow_back,
12+
'arrow_forward': Icons.arrow_forward,
13+
'build': Icons.build,
14+
'call': Icons.call,
15+
'camera_alt': Icons.camera_alt,
16+
'check': Icons.check,
17+
'close': Icons.close,
18+
'delete': Icons.delete,
19+
'done': Icons.done,
20+
'edit': Icons.edit,
21+
'favorite': Icons.favorite,
22+
'home': Icons.home,
23+
'info': Icons.info,
24+
'menu': Icons.menu,
25+
'more_vert': Icons.more_vert,
26+
'notifications': Icons.notifications,
27+
'phone': Icons.phone,
28+
'search': Icons.search,
29+
'settings': Icons.settings,
30+
'share': Icons.share,
31+
'shopping_cart': Icons.shopping_cart,
32+
'star': Icons.star,
33+
'accessibility': Icons.accessibility,
34+
'accessible': Icons.accessible,
35+
'account_circle': Icons.account_circle,
36+
'account_box': Icons.account_box,
37+
'add_box': Icons.add_box,
38+
'add_circle': Icons.add_circle,
39+
'add_shopping_cart': Icons.add_shopping_cart,
40+
'airplanemode_active': Icons.airplanemode_active,
41+
'album': Icons.album,
42+
'all_inbox': Icons.all_inbox,
43+
'analytics': Icons.analytics,
44+
'archive': Icons.archive,
45+
'arrow_drop_down': Icons.arrow_drop_down,
46+
'arrow_drop_up': Icons.arrow_drop_up,
47+
'assessment': Icons.assessment,
48+
'attach_file': Icons.attach_file,
49+
'attachment': Icons.attachment,
50+
'auto_fix_high': Icons.auto_fix_high,
51+
'backup': Icons.backup,
52+
'battery_full': Icons.battery_full,
53+
'bed': Icons.bed,
54+
'book': Icons.book,
55+
'bookmark': Icons.bookmark,
56+
'brightness_4': Icons.brightness_4,
57+
'business': Icons.business,
58+
'calendar_today': Icons.calendar_today,
59+
'camera': Icons.camera,
60+
'card_giftcard': Icons.card_giftcard,
61+
'card_membership': Icons.card_membership,
62+
'chat': Icons.chat,
63+
'check_box': Icons.check_box,
64+
'chevron_left': Icons.chevron_left,
65+
'chevron_right': Icons.chevron_right,
66+
'cloud': Icons.cloud,
67+
'cloud_queue': Icons.cloud_queue,
68+
'code': Icons.code,
69+
'commute': Icons.commute,
70+
'compare_arrows': Icons.compare_arrows,
71+
'credit_card': Icons.credit_card,
72+
'crop_square': Icons.crop_square,
73+
'dashboard': Icons.dashboard,
74+
'directions': Icons.directions,
75+
'directions_bike': Icons.directions_bike,
76+
'directions_car': Icons.directions_car,
77+
'directions_walk': Icons.directions_walk,
78+
'download': Icons.download,
79+
'event': Icons.event,
80+
'exit_to_app': Icons.exit_to_app,
81+
'face': Icons.face,
82+
'fingerprint': Icons.fingerprint,
83+
};
84+
85+
for (final iconEntry in iconsMap.entries) {
86+
goldenTest(
87+
'should render icon ${iconEntry.key} correctly',
88+
fileName: 'icons/${iconEntry.key}',
89+
skip: true, // removes if you want to check loading fonts performance
90+
builder: () => GoldenTestScenario.builder(
91+
name: iconEntry.key,
92+
builder: (_) => Icon(
93+
iconEntry.value,
94+
size: 64,
95+
),
96+
),
97+
);
98+
}
99+
});
100+
}

lib/src/golden_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import 'package:flutter/services.dart';
1111
import 'package:flutter_test/flutter_test.dart';
1212
import 'package:meta/meta.dart';
1313

14+
final Set<String> _loadedFontFamilies = {};
15+
1416
/// Default golden test runner which uses the flutter test framework.
1517
const defaultGoldenTestRunner = FlutterGoldenTestRunner();
1618
GoldenTestRunner _goldenTestRunner = defaultGoldenTestRunner;
@@ -44,6 +46,11 @@ Future<void> loadFonts() async {
4446
final family = (entry['family'] as String)
4547
.stripFontFamilyAlchemistPackageName();
4648

49+
// Skip if font family is already loaded
50+
if (_loadedFontFamilies.contains(family)) {
51+
continue;
52+
}
53+
4754
final fontAssets = [
4855
for (final fontAssetEntry in entry['fonts'] as List<dynamic>)
4956
(fontAssetEntry as Map<String, dynamic>)['asset'] as String,
@@ -55,6 +62,7 @@ Future<void> loadFonts() async {
5562
}
5663

5764
await loader.load();
65+
_loadedFontFamilies.add(family);
5866
}
5967
}
6068

0 commit comments

Comments
 (0)