OpenContacts/lib/widgets/generic_avatar.dart

44 lines
1.3 KiB
Dart
Raw Normal View History

2023-04-30 17:14:29 -04:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class GenericAvatar extends StatelessWidget {
2023-05-05 09:05:06 -04:00
const GenericAvatar({this.imageUri="", super.key, this.placeholderIcon=Icons.person, this.radius});
2023-04-30 17:14:29 -04:00
final String imageUri;
2023-05-01 16:00:56 -04:00
final IconData placeholderIcon;
2023-05-05 09:05:06 -04:00
final double? radius;
2023-04-30 17:14:29 -04:00
@override
Widget build(BuildContext context) {
2023-05-01 16:00:56 -04:00
return imageUri.isEmpty ? CircleAvatar(
2023-05-05 09:05:06 -04:00
radius: radius,
2023-04-30 17:14:29 -04:00
backgroundColor: Colors.transparent,
2023-05-01 16:00:56 -04:00
child: Icon(placeholderIcon),
2023-04-30 17:14:29 -04:00
) : CachedNetworkImage(
imageBuilder: (context, imageProvider) {
return CircleAvatar(
foregroundImage: imageProvider,
backgroundColor: Colors.transparent,
2023-05-05 09:05:06 -04:00
radius: radius,
2023-04-30 17:14:29 -04:00
);
},
imageUrl: imageUri,
placeholder: (context, url) {
2023-05-05 09:05:06 -04:00
return CircleAvatar(
backgroundColor: Colors.white54,
radius: radius,
child: const Padding(
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator(color: Colors.black38, strokeWidth: 2),
),
);
2023-04-30 17:14:29 -04:00
},
2023-05-01 16:00:56 -04:00
errorWidget: (context, error, what) => CircleAvatar(
2023-05-05 09:05:06 -04:00
radius: radius,
2023-04-30 17:14:29 -04:00
backgroundColor: Colors.transparent,
2023-05-01 16:00:56 -04:00
child: Icon(placeholderIcon),
2023-04-30 17:14:29 -04:00
),
);
}
}