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

/// 网络图片加载组件
class KRNetworkImage extends StatelessWidget {
  /// 图片URL
  final String kr_imageUrl;
  
  /// 图片宽度
  final double? kr_width;
  
  /// 图片高度 
  final double? kr_height;
  
  /// 图片填充方式
  final BoxFit kr_fit;
  
  /// 加载中占位组件
  final Widget? kr_placeholder;
  
  /// 加载失败占位组件
  final Widget? kr_errorWidget;

  /// 构造函数
  const KRNetworkImage({
    Key? key,
    required this.kr_imageUrl,
    this.kr_width,
    this.kr_height,
    this.kr_fit = BoxFit.cover,
    this.kr_placeholder,
    this.kr_errorWidget,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ExtendedImage.network(
      kr_imageUrl,
      width: kr_width,
      height: kr_height,
      fit: kr_fit,
      cache: true,
      loadStateChanged: (ExtendedImageState state) {
        switch (state.extendedImageLoadState) {
          case LoadState.loading:
            return kr_placeholder ?? 
                const Center(child: CircularProgressIndicator());
          case LoadState.completed:
            return null; // 返回实际图片
          case LoadState.failed:
            return kr_errorWidget ?? 
                const Icon(Icons.error);
        }
      },
    );
  }
} 