dev
dart 83 lines 2.97 KB
Raw
1 import 'package:cw_core/utils/proxy_wrapper.dart';
2 import 'package:flutter/material.dart';
3 import 'package:flutter_svg/svg.dart';
4
5 class ImageUtil {
6 static Widget getImageFromPath({
7 required String imagePath,
8 double? height,
9 double? width,
10 Color? svgImageColor,
11 BoxFit? fit,
12 double? borderRadius,
13 }) {
14 bool isNetworkImage = imagePath.startsWith('http') || imagePath.startsWith('https');
15
16 if (CakeTor.instance!.enabled && isNetworkImage) {
17 imagePath = "assets/images/tor_logo.svg";
18 isNetworkImage = false;
19 }
20 final isSvg = imagePath.endsWith('.svg');
21 final bool ignoreSize = fit != null;
22 final double? _height = ignoreSize ? null : (height ?? 35);
23 final double? _width = ignoreSize ? null : (width ?? 35);
24
25 Widget img;
26 if (isNetworkImage) {
27 img = isSvg
28 ? SvgPicture.network(imagePath,
29 key: ValueKey(imagePath),
30 height: _height,
31 width: _width,
32 fit: fit ?? BoxFit.contain,
33 placeholderBuilder: (_) => _placeholder(_height, _width),
34 errorBuilder: (_, __, ___) => _errorPlaceholder(_height, _width))
35 : Image.network(imagePath,
36 key: ValueKey(imagePath),
37 height: _height,
38 width: _width,
39 fit: fit,
40 loadingBuilder: (_, child, progress) =>
41 progress == null ? child : _placeholder(_height, _width),
42 errorBuilder: (_, __, ___) => _errorPlaceholder(_height, _width));
43 } else {
44 img = isSvg
45 ? SvgPicture.asset(imagePath,
46 key: ValueKey(imagePath),
47 height: _height,
48 width: _width,
49 fit: fit ?? BoxFit.contain,
50 colorFilter:
51 svgImageColor != null ? ColorFilter.mode(svgImageColor, BlendMode.srcIn) : null,
52 placeholderBuilder: (_) => _placeholder(_height, _width),
53 errorBuilder: (_, __, ___) => _errorPlaceholder(_height, _width))
54 : Image.asset(
55 imagePath,
56 key: ValueKey(imagePath),
57 height: _height,
58 width: _width,
59 fit: fit,
60 errorBuilder: (_, __, ___) => _errorPlaceholder(_height, _width),
61 );
62 }
63
64 if (borderRadius != null && borderRadius > 0) {
65 img = ClipRRect(
66 borderRadius: BorderRadius.circular(borderRadius),
67 child: img,
68 );
69 }
70 return img;
71 }
72
73 static Widget _placeholder(double? h, double? w) => (h != null || w != null)
74 ? SizedBox(height: h, width: w, child: const Center(child: CircularProgressIndicator()))
75 : const Center(child: CircularProgressIndicator());
76
77 static Widget _errorPlaceholder(double? h, double? w) => (h != null || w != null)
78 ? SizedBox(
79 height: h,
80 width: w,
81 child: const Center(child: Icon(Icons.error_outline, color: Colors.grey)))
82 : const Center(child: Icon(Icons.error_outline, color: Colors.grey));
83 }