import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class CustomCircleAvatar extends StatelessWidget {
  String? avtarUrl;
  double size;
  Color borderColor;
  double borderWidth;

  CustomCircleAvatar(
    this.avtarUrl, {
    Key? key,
    this.size = 44,
    this.borderColor = AppColors.transparent,
    this.borderWidth = 2,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (avtarUrl == null || avtarUrl == '') {
      avtarUrl = 'assets/chats/default_avatar.png';
    }

    return Container(
      width: size,
      height: size,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(size / 2),
        border: Border.all(
          color: borderColor,
          width: borderWidth,
        ),
      ),
      child: PhysicalModel(
        color: borderColor.withOpacity(0.1),
        shape: BoxShape.circle,
        clipBehavior: Clip.antiAlias,
        child: _image(),
      ),
    );
  }

  Widget _image() {
    if (avtarUrl!.contains('http')) {
      return CachedNetworkImage(
        imageUrl: avtarUrl!,
        width: size,
        height: size,
        fit: BoxFit.cover,
      );
    } else {
      return Image.asset(
        avtarUrl!,
        width: size,
        height: size,
        fit: BoxFit.cover,
      );
    }
  }
}
