2023-08-17 17:24:53 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:flutter_svg/svg.dart';
|
|
|
|
|
|
|
|
class IconUtils {
|
|
|
|
IconUtils._privateConstructor();
|
|
|
|
|
|
|
|
static final IconUtils instance = IconUtils._privateConstructor();
|
|
|
|
|
2023-08-17 17:46:10 +00:00
|
|
|
// Map of icon-title to the color code in HEX
|
2023-08-17 17:24:53 +00:00
|
|
|
final Map<String, String> _simpleIcons = {};
|
2023-08-17 17:46:10 +00:00
|
|
|
final Map<String, String> _customIcons = {};
|
2023-08-17 17:24:53 +00:00
|
|
|
|
|
|
|
Future<void> init() async {
|
|
|
|
await _loadJson();
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:18:26 +00:00
|
|
|
Widget getIcon(
|
|
|
|
String provider, {
|
|
|
|
double width = 24,
|
|
|
|
}) {
|
2023-08-17 17:24:53 +00:00
|
|
|
final title = _getProviderTitle(provider);
|
2023-08-18 03:20:48 +00:00
|
|
|
if (_customIcons.containsKey(title)) {
|
2023-08-17 17:46:10 +00:00
|
|
|
return _getSVGIcon(
|
2023-08-18 03:20:48 +00:00
|
|
|
"assets/custom-icons/icons/$title.svg",
|
2023-08-17 17:46:10 +00:00
|
|
|
title,
|
2023-08-18 03:20:48 +00:00
|
|
|
_customIcons[title]!,
|
2023-09-08 12:18:26 +00:00
|
|
|
width,
|
2023-08-17 17:46:10 +00:00
|
|
|
);
|
2023-08-18 03:20:48 +00:00
|
|
|
} else if (_simpleIcons.containsKey(title)) {
|
2023-08-17 17:46:10 +00:00
|
|
|
return _getSVGIcon(
|
2023-08-18 03:20:48 +00:00
|
|
|
"assets/simple-icons/icons/$title.svg",
|
2023-08-17 17:46:10 +00:00
|
|
|
title,
|
2023-08-18 03:20:48 +00:00
|
|
|
_simpleIcons[title]!,
|
2023-09-08 12:18:26 +00:00
|
|
|
width,
|
2023-08-17 17:24:53 +00:00
|
|
|
);
|
|
|
|
} else {
|
2023-08-18 03:20:24 +00:00
|
|
|
return const SizedBox.shrink();
|
2023-08-17 17:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:18:26 +00:00
|
|
|
Widget _getSVGIcon(
|
|
|
|
String path,
|
|
|
|
String title,
|
|
|
|
String color,
|
|
|
|
double width,
|
|
|
|
) {
|
2023-08-17 17:46:10 +00:00
|
|
|
return SvgPicture.asset(
|
|
|
|
path,
|
2023-09-08 12:18:26 +00:00
|
|
|
width: width,
|
2023-08-17 17:46:10 +00:00
|
|
|
semanticsLabel: title,
|
|
|
|
colorFilter: ColorFilter.mode(
|
|
|
|
Color(int.parse("0xFF" + color)),
|
|
|
|
BlendMode.srcIn,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-17 17:24:53 +00:00
|
|
|
Future<void> _loadJson() async {
|
2023-08-17 17:46:10 +00:00
|
|
|
final simpleIconData = await rootBundle
|
2023-08-17 17:24:53 +00:00
|
|
|
.loadString('assets/simple-icons/_data/simple-icons.json');
|
2023-08-17 17:46:10 +00:00
|
|
|
final simpleIcons = json.decode(simpleIconData);
|
|
|
|
for (final icon in simpleIcons["icons"]) {
|
2023-08-17 17:24:53 +00:00
|
|
|
_simpleIcons[icon["title"].toString().toLowerCase()] = icon["hex"];
|
|
|
|
}
|
2023-08-17 17:46:10 +00:00
|
|
|
final customIconData = await rootBundle
|
|
|
|
.loadString('assets/custom-icons/_data/custom-icons.json');
|
|
|
|
final customIcons = json.decode(customIconData);
|
|
|
|
for (final icon in customIcons["icons"]) {
|
|
|
|
_customIcons[icon["title"].toString().toLowerCase()] = icon["hex"];
|
|
|
|
}
|
2023-08-17 17:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String _getProviderTitle(String provider) {
|
2023-08-24 20:05:11 +00:00
|
|
|
return provider.split(RegExp(r'[.(]'))[0].trim().toLowerCase();
|
2023-08-17 17:24:53 +00:00
|
|
|
}
|
|
|
|
}
|