// Author: fengshengxiong
// Date: 2024/5/9
// Description: 加载网络图像

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tone_snap/generated/assets.dart';

class NetworkImageWidget extends StatelessWidget {
  const NetworkImageWidget({
    super.key,
    this.width,
    this.height,
    this.radius = 0.0,
    this.url,
    this.fit = BoxFit.cover,
    this.placeholderWidth,
    this.placeholderHeight,
    this.placeholderImg,
    this.noPlaceholder = false,
  });

  final double? width;
  final double? height;
  final double radius;
  final String? url;
  final BoxFit fit;
  final double? placeholderWidth;
  final double? placeholderHeight;
  final String? placeholderImg;
  final bool noPlaceholder;

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(radius),
      child: CachedNetworkImage(
        width: width,
        height: height,
        imageUrl: '$url',
        fit: fit,
        placeholder: (context, url) {
          return noPlaceholder ? Container() : _placeholderWidget();
        },
        errorWidget: (context, url, error) {
          return noPlaceholder ? Container() : _placeholderWidget();
        },
      ),
    );
  }

  Widget _placeholderWidget() {
    return Container(
      color: const Color(0xFF242529),
      child: FittedBox(
        fit: BoxFit.none,
        child: Image.asset(
          placeholderImg ?? Assets.sideBMusicPlaceholder,
          width: placeholderWidth ?? 24.w,
          height: placeholderHeight ?? 24.w,
        ),
      ),
    );
  }
}
