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

class ShimmerImage extends StatelessWidget {
  String url;
  BoxFit? fit;
  double? width;
  double? height;
  double? aspectRatio;
  double? iconHolderSize = 40;

  ShimmerImage(
    this.url, {
    this.fit,
    this.width,
    this.height,
    this.aspectRatio,
    this.iconHolderSize,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Shimmer.fromColors(
          baseColor: Colors.grey,
          highlightColor: Colors.grey,
          child: aspectRatio != null
              ? AspectRatio(
                  aspectRatio: aspectRatio ?? 1,
                  child: Container(
                    child: _buildIcon(),
                  ),
                )
              : Container(
                  width: width,
                  height: height,
                  child: _buildIcon(),
                ),
        ),
        aspectRatio != null
            ? AspectRatio(
                aspectRatio: aspectRatio ?? 1,
                child: Image.network(
                  url,
                  fit: fit ?? BoxFit.contain,
                ),
              )
            : Image.network(
                url,
                width: width,
                height: height,
                fit: fit ?? BoxFit.contain,
              ),
      ],
    );
  }

  _buildIcon() {
    return Center(
      child: Icon(
        Icons.crop_original,
        color: Colors.red,
        size: iconHolderSize,
      ),
    );
  }
}
