OpenContacts/lib/widgets/generic_avatar.dart

38 lines
1.1 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-01 16:00:56 -04:00
const GenericAvatar({this.imageUri="", super.key, this.placeholderIcon=Icons.person});
2023-04-30 17:14:29 -04:00
final String imageUri;
2023-05-01 16:00:56 -04:00
final IconData placeholderIcon;
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-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,
);
},
imageUrl: imageUri,
placeholder: (context, url) {
2023-05-01 16:00:56 -04:00
return const CircleAvatar(
backgroundColor: Colors.white54,
child: 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-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
),
);
}
}