OpenContacts/lib/widgets/generic_avatar.dart

49 lines
1.9 KiB
Dart
Raw Permalink 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 {
const GenericAvatar({this.imageUri="", super.key, this.placeholderIcon=Icons.person, this.radius, this.foregroundColor});
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;
final Color? foregroundColor;
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-05-25 13:30:15 -04:00
foregroundColor: foregroundColor ?? Theme.of(context).colorScheme.onPrimaryContainer,
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
child: Icon(placeholderIcon, color: foregroundColor,),
2023-04-30 17:14:29 -04:00
) : CachedNetworkImage(
imageBuilder: (context, imageProvider) {
return CircleAvatar(
foregroundImage: imageProvider,
2023-05-25 13:30:15 -04:00
foregroundColor: Colors.transparent,
2023-04-30 17:14:29 -04:00
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(
2023-05-25 13:30:15 -04:00
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
foregroundColor: foregroundColor ?? Theme.of(context).colorScheme.onPrimaryContainer,
2023-05-05 09:05:06 -04:00
radius: radius,
child: Padding(
padding: const EdgeInsets.all(8.0),
2023-05-25 13:30:15 -04:00
child: CircularProgressIndicator(color: foregroundColor ?? Theme.of(context).colorScheme.onPrimaryContainer, strokeWidth: 2),
2023-05-05 09:05:06 -04:00
),
);
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-05-25 13:30:15 -04:00
foregroundColor: foregroundColor ?? Theme.of(context).colorScheme.onPrimaryContainer,
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
child: Icon(placeholderIcon, color: foregroundColor ?? Theme.of(context).colorScheme.onPrimaryContainer,),
2023-04-30 17:14:29 -04:00
),
);
}
}