Update image_utill.dart

This commit is contained in:
Serhii 2025-06-23 17:24:18 +03:00
parent 88ba8b42ab
commit a8eb98b612

View file

@ -1,10 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:cw_core/utils/proxy_wrapper.dart';
class ImageUtil {
static Widget getImageFromPath({
@ -15,40 +14,52 @@ class ImageUtil {
BoxFit? fit,
double? borderRadius,
}) {
final isNetwork = imagePath.startsWith('http');
final isSvg = imagePath.endsWith('.svg');
bool isNetworkImage = imagePath.startsWith('http') || imagePath.startsWith('https');
if (CakeTor.instance.enabled && isNetworkImage) {
imagePath = "assets/images/tor_logo.svg";
isNetworkImage = false;
}
final bool isSvg = imagePath.endsWith('.svg');
final bool ignoreSize = fit != null;
final double? _height = ignoreSize ? null : (height ?? 35);
final double? _width = ignoreSize ? null : (width ?? 35);
Widget img;
if (isNetwork) {
if (isNetworkImage) {
img = isSvg
? SvgPicture.network(imagePath,
? SvgPicture.network(
imagePath,
key: ValueKey(imagePath),
height: _height,
width: _width,
fit: fit ?? BoxFit.contain,
placeholderBuilder: (_) => _placeholder(_height, _width))
: Image.network(imagePath,
placeholderBuilder: (_) => _placeholder(_height, _width),
)
: Image.network(
imagePath,
key: ValueKey(imagePath),
height: _height,
width: _width,
fit: fit,
loadingBuilder: (_, child, progress) =>
progress == null ? child : _placeholder(_height, _width),
errorBuilder: (_, __, ___) => const SizedBox.shrink());
errorBuilder: (_, __, ___) => const SizedBox.shrink(),
);
} else {
img = isSvg
? SvgPicture.asset(imagePath,
? SvgPicture.asset(
imagePath,
key: ValueKey(imagePath),
height: _height,
width: _width,
fit: fit ?? BoxFit.contain,
colorFilter:
svgImageColor != null ? ColorFilter.mode(svgImageColor, BlendMode.srcIn) : null,
placeholderBuilder: (_) => const Icon(Icons.error))
placeholderBuilder: (_) => const Icon(Icons.error),
)
: Image.asset(
imagePath,
key: ValueKey(imagePath),
@ -65,13 +76,19 @@ class ImageUtil {
child: img,
);
}
return img;
}
static Widget _placeholder(double? h, double? w) => (h != null || w != null)
? SizedBox(height: h, width: w, child: const Center(child: CircularProgressIndicator()))
: const Center(child: CircularProgressIndicator());
static Widget _placeholder(double? height, double? width) {
return (height != null || width != null)
? SizedBox(
height: height,
width: width,
child: const Center(child: CircularProgressIndicator()),
)
: const Center(child: CircularProgressIndicator());
}
static Future<String?> saveAvatarLocally(String imageUriOrPath) async {
if (imageUriOrPath.isEmpty) return null;
@ -80,11 +97,18 @@ class ImageUtil {
final dir = await getApplicationDocumentsDirectory();
String ext = p.extension(imageUriOrPath);
if (ext.isEmpty) ext = '.png';
final file = File('${dir.path}/${DateTime.now().millisecondsSinceEpoch}$ext');
if (imageUriOrPath.startsWith('http')) {
final response = await http.get(Uri.parse(imageUriOrPath));
final response = await ProxyWrapper()
.get(
clearnetUri: Uri.parse(imageUriOrPath),
)
.catchError((error) {
throw Exception('HTTP request failed: $error');
});
if (response.statusCode == 200) {
await file.writeAsBytes(response.bodyBytes);
} else {