font_loader.dart 676 B

12345678910111213141516171819202122232425
  1. import 'dart:collection';
  2. import 'package:fis_common/logger/logger.dart';
  3. import 'package:flutter/services.dart';
  4. import 'resource.dart';
  5. class FFontLoader {
  6. static final HashSet<String> _loadedFonts = HashSet();
  7. /// 加载字体
  8. static Future<bool> load(String fontFamily, FResourceKey resourceKey) async {
  9. if (_loadedFonts.contains(fontFamily)) return true;
  10. var fontLoader = FontLoader(fontFamily);
  11. fontLoader.addFont(fRootBundle.load(resourceKey));
  12. try {
  13. await fontLoader.load();
  14. } catch (e) {
  15. logger.e("FFontLoader load [$fontFamily] error.", e);
  16. return false;
  17. }
  18. _loadedFonts.add(fontFamily);
  19. return true;
  20. }
  21. }