【问题标题】:Is there a way to show default image if network/asset image fails to load for circle avatar?如果网络/资产图像无法加载圆形头像,有没有办法显示默认图像?
【发布时间】:2021-03-18 18:26:00
【问题描述】:

如果网络/资产图像无法加载,我想要一种加载占位符图像的方法。这是我尝试过的。

class CircularImageWidget extends StatelessWidget {
  CircularImageWidget({
    Key key,
    @required this.radius,
    @required this.assetPath,
    @required this.imageType,
  }) : super(key: key);

  final double radius;
  final String assetPath;
  final ImageType imageType;

  Image getErrorImage() {
    return Image.asset(AssetUtils.profilepic);
  }

  ImageProvider returnImageAccordingToImageType() {
    if (imageType == ImageType.Network) {
      return NetworkImage(
        assetPath,
      );
    } else {
      return AssetImage(
        assetPath,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return CircleAvatar(
      backgroundImage: returnImageAccordingToImageType() ??
          AssetImage(AssetUtils.profilepic),
      radius: radius,
      onBackgroundImageError: (exception, stackTrace) {
        return AssetImage(AssetUtils.profilepic);
      },
    );
  }
}

【问题讨论】:

    标签: image flutter flutter-image


    【解决方案1】:

    您可以通过检查图像的 URL 并根据它显示不同的小部件来实现这一点。

      @override
       Widget build(BuildContext context) {
       return CircleAvatar(
     imageURL== null? backgroundImage: 
          AssetImage(AssetUtils.profilepic),
      radius: radius,
     
    ):backgroundImage: 
          NetworkImage(imageURL),
      radius: radius,
     
    ),
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-15
      • 2015-01-20
      • 2022-08-10
      • 1970-01-01
      • 2019-04-30
      • 2021-01-17
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多