- import 'dart:io';
-
- import 'package:flutter/material.dart';
- import 'package:path/path.dart';
-
- enum AvatarTypes {
- initials,
- icon,
- image,
- }
-
- class CustomCircleAvatar extends StatelessWidget {
- final String? initials;
- final Icon? icon;
- final File? image;
- final Function ()? editImageCallback;
- final double radius;
-
- const CustomCircleAvatar({
- Key? key,
- this.initials,
- this.icon,
- this.image,
- this.editImageCallback,
- this.radius = 20,
- }) : super(key: key);
-
- Widget avatar(BuildContext context) {
- AvatarTypes? type;
-
- if (icon != null) {
- type = AvatarTypes.icon;
- }
-
- if (initials != null) {
- type = AvatarTypes.initials;
- }
-
- if (image != null) {
- type = AvatarTypes.image;
- }
-
- if (type == null) {
- throw ArgumentError('Invalid arguments passed to CustomCircleAvatar');
- }
-
- if (type == AvatarTypes.initials) {
- return CircleAvatar(
- backgroundColor: Theme.of(context).colorScheme.tertiary,
- radius: radius,
- child: Text(initials!),
- );
- }
-
- if (type == AvatarTypes.icon) {
- return CircleAvatar(
- backgroundColor: Theme.of(context).colorScheme.tertiary,
- radius: radius,
- child: icon,
- );
- }
-
- return Container(
- width: radius * 2,
- height: radius * 2,
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- image: DecorationImage(
- image: Image.file(image!).image,
- fit: BoxFit.fill
- ),
- ),
- );
- }
-
- Widget editIcon(BuildContext context) {
- if (editImageCallback == null) {
- return const SizedBox.shrink();
- }
-
- return SizedBox(
- height: (radius * 2),
- width: (radius * 2),
- child: Align(
- alignment: Alignment.bottomRight,
- child: GestureDetector(
- onTap: editImageCallback,
- child: Container(
- height: (radius / 2) + (radius / 7),
- width: (radius / 2) + (radius / 7),
- decoration: BoxDecoration(
- color: Theme.of(context).scaffoldBackgroundColor,
- borderRadius: BorderRadius.circular(30),
- ),
- child: Icon(
- Icons.add,
- color: Theme.of(context).primaryColor,
- size: radius / 2
- ),
- ),
- ),
- ),
- );
- }
-
- @override
- Widget build(BuildContext context) {
- return Stack(
- children: [
- avatar(context),
- editIcon(context),
- ]
- );
- }
- }
|