Solian/lib/widgets/account/account_avatar.dart

108 lines
2.8 KiB
Dart
Raw Normal View History

2024-06-01 13:39:28 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2024-05-18 10:17:16 +00:00
import 'package:flutter/material.dart';
2024-06-01 13:39:28 +00:00
import 'package:solian/platform.dart';
2024-05-18 10:17:16 +00:00
import 'package:solian/services.dart';
class AccountAvatar extends StatelessWidget {
final dynamic content;
2024-05-28 16:14:41 +00:00
final Color? bgColor;
final Color? feColor;
2024-05-18 10:17:16 +00:00
final double? radius;
2024-05-28 16:14:41 +00:00
const AccountAvatar({
super.key,
required this.content,
this.bgColor,
this.feColor,
this.radius,
});
2024-05-18 10:17:16 +00:00
@override
Widget build(BuildContext context) {
bool direct = false;
bool isEmpty = content == null;
if (content is String) {
direct = content.startsWith('http');
2024-05-25 16:11:00 +00:00
if (!isEmpty) isEmpty = content.isEmpty;
if (!isEmpty) isEmpty = content.endsWith('/attachments/0');
}
2024-05-18 10:17:16 +00:00
2024-06-01 13:39:28 +00:00
final url = direct
? content
: ServiceFinder.buildUrl('files', '/attachments/$content');
2024-06-01 13:39:28 +00:00
2024-05-18 10:17:16 +00:00
return CircleAvatar(
2024-05-19 10:01:00 +00:00
key: Key('a$content'),
2024-05-18 10:17:16 +00:00
radius: radius,
2024-05-28 16:14:41 +00:00
backgroundColor: bgColor,
backgroundImage: !isEmpty
2024-06-01 13:39:28 +00:00
? (PlatformInfo.canCacheImage
? CachedNetworkImageProvider(url)
: NetworkImage(url)) as ImageProvider<Object>?
: null,
child: isEmpty
? Icon(
Icons.account_circle,
size: radius != null ? radius! * 1.2 : 24,
2024-05-28 16:14:41 +00:00
color: feColor,
)
: null,
2024-05-18 10:17:16 +00:00
);
}
}
2024-06-03 15:36:46 +00:00
class AccountProfileImage extends StatelessWidget {
final dynamic content;
final BoxFit fit;
const AccountProfileImage({
super.key,
required this.content,
this.fit = BoxFit.cover,
});
@override
Widget build(BuildContext context) {
bool direct = false;
bool isEmpty = content == null;
if (content is String) {
direct = content.startsWith('http');
if (!isEmpty) isEmpty = content.isEmpty;
if (!isEmpty) isEmpty = content.endsWith('/attachments/0');
2024-06-03 15:36:46 +00:00
}
final url = direct
? content
: ServiceFinder.buildUrl('files', '/attachments/$content');
2024-06-03 15:36:46 +00:00
if (PlatformInfo.canCacheImage) {
return CachedNetworkImage(
imageUrl: url,
fit: fit,
progressIndicatorBuilder: (context, url, downloadProgress) => Center(
child: CircularProgressIndicator(
value: downloadProgress.progress,
),
),
);
} else {
return Image.network(
url,
fit: fit,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
);
},
);
}
}
}