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

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

class ImageNetworkWidget extends StatelessWidget {
  const ImageNetworkWidget({
    super.key,
    this.width,
    this.height,
    this.radius = 0.0,
    required this.url,
    this.fit = BoxFit.cover,
    this.placeholder,
    this.errorWidget,
  });

  final double? width;
  final double? height;
  final double radius;
  final String? url;
  final BoxFit fit;
  final Widget? placeholder;
  final Widget? errorWidget;

  @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 placeholder ?? _placeholderWidget(Assets.imagesImgPlaceholder);
        },
        errorWidget: (context, url, error) {
          return errorWidget ?? _placeholderWidget(Assets.imagesImgError);
        },
      ),
    );
  }

  Widget _placeholderWidget(String imgName) {
    return Container(
      color: Colors.white10,
      child: Image.asset(
        imgName,
        color: Colors.white12,
      ),
    );
  }
}
