Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.6 KiB

import 'package:flutter/material.dart';
enum AvatarTypes {
initials,
icon,
image,
}
class CustomCircleAvatar extends StatefulWidget {
final String? initials;
final Icon? icon;
final String? imagePath;
final double radius;
const CustomCircleAvatar({
Key? key,
this.initials,
this.icon,
this.imagePath,
this.radius = 20,
}) : super(key: key);
@override
_CustomCircleAvatarState createState() => _CustomCircleAvatarState();
}
class _CustomCircleAvatarState extends State<CustomCircleAvatar>{
AvatarTypes type = AvatarTypes.image;
@override
void initState() {
super.initState();
if (widget.imagePath != null) {
type = AvatarTypes.image;
return;
}
if (widget.icon != null) {
type = AvatarTypes.icon;
return;
}
if (widget.initials != null) {
type = AvatarTypes.initials;
return;
}
throw ArgumentError('Invalid arguments passed to CustomCircleAvatar');
}
Widget avatar() {
if (type == AvatarTypes.initials) {
return CircleAvatar(
backgroundColor: Colors.grey[300],
child: Text(widget.initials!),
radius: widget.radius,
);
}
if (type == AvatarTypes.icon) {
return CircleAvatar(
backgroundColor: Colors.grey[300],
child: widget.icon,
radius: widget.radius,
);
}
return CircleAvatar(
backgroundImage: AssetImage(widget.imagePath!),
radius: widget.radius,
);
}
@override
Widget build(BuildContext context) {
return avatar();
}
}