icon_utils.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter/widgets.dart';
  5. import 'package:flutter_svg/svg.dart';
  6. class IconUtils {
  7. IconUtils._privateConstructor();
  8. static final IconUtils instance = IconUtils._privateConstructor();
  9. // Map of icon-title to the color code in HEX
  10. final Map<String, String> _simpleIcons = {};
  11. final Map<String, String> _customIcons = {};
  12. Future<void> init() async {
  13. await _loadJson();
  14. }
  15. Widget getIcon(String provider) {
  16. final title = _getProviderTitle(provider);
  17. if (_customIcons.containsKey(title)) {
  18. return _getSVGIcon(
  19. "assets/custom-icons/icons/$title.svg",
  20. title,
  21. _customIcons[title]!,
  22. );
  23. } else if (_simpleIcons.containsKey(title)) {
  24. return _getSVGIcon(
  25. "assets/simple-icons/icons/$title.svg",
  26. title,
  27. _simpleIcons[title]!,
  28. );
  29. } else {
  30. return const SizedBox.shrink();
  31. }
  32. }
  33. Widget _getSVGIcon(String path, String title, String color) {
  34. return SvgPicture.asset(
  35. path,
  36. width: 24,
  37. semanticsLabel: title,
  38. colorFilter: ColorFilter.mode(
  39. Color(int.parse("0xFF" + color)),
  40. BlendMode.srcIn,
  41. ),
  42. );
  43. }
  44. Future<void> _loadJson() async {
  45. final simpleIconData = await rootBundle
  46. .loadString('assets/simple-icons/_data/simple-icons.json');
  47. final simpleIcons = json.decode(simpleIconData);
  48. for (final icon in simpleIcons["icons"]) {
  49. _simpleIcons[icon["title"].toString().toLowerCase()] = icon["hex"];
  50. }
  51. final customIconData = await rootBundle
  52. .loadString('assets/custom-icons/_data/custom-icons.json');
  53. final customIcons = json.decode(customIconData);
  54. for (final icon in customIcons["icons"]) {
  55. _customIcons[icon["title"].toString().toLowerCase()] = icon["hex"];
  56. }
  57. }
  58. String _getProviderTitle(String provider) {
  59. return provider.split(".")[0].toLowerCase();
  60. }
  61. }