import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class CustomAvatar extends StatelessWidget {
  String? avtarUrl;
  double size;
  double radius;

  CustomAvatar(
    this.avtarUrl, {
    Key? key,
    this.size = 44,
    this.radius = 4,
  }) : super(key: key);

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

    avtarUrl = avtarUrl!.trim();

    return SizedBox(
      width: size,
      height: size,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: _image(),
      ),
    );
  }

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