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

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.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.placeholder,
    this.noPlaceholder = false,
  });

  final double? width;
  final double? height;
  final double radius;
  final String? url;
  final BoxFit fit;
  final String? placeholder;
  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: noPlaceholder ? null : (context, url) {
          return _placeholderWidget(Assets.sideBImgPlaceholder);
        },
        errorWidget: noPlaceholder ? null : (context, url, error) {
          return _placeholderWidget(Assets.sideBImgError);
        },
      ),
    );
  }

  Widget _placeholderWidget(String img) {
    return Image.asset(
      placeholder ?? img,
      color: Colors.white12,
    );
  }
}
