Flutter 图片工具类封装

目的是为了整合asset图片显示、以及远程url图片显示。
增加依赖cached_network_image

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

class NFTimage extends StatelessWidget {
  final double? width;
  final double? height;
  final double? radius;
  final BoxFit? fit;
  final Color? color;

  final ImageProvider image;

  NFTimage.asset(
    String name, {
    Key? key,
    this.width,
    this.height,
    this.color,
    this.radius,
    this.fit,
  })  : image = Image.asset('images/common/$name').image,
        super(key: key);
/*
NFTimage.asset -> image
也可以用这种内部实现方案
 image = ResizeImage.resizeIfNeeded(
         cacheWidth,
         cacheHeight,
         scale != null
           ? ExactAssetImage(name, bundle: bundle, scale: scale, package: package)
           : AssetImage(name, bundle: bundle, package: package),
       )
*/

  NFTimage.network(String url,
      {Key? key,
      this.width,
      this.height,
      this.color,
      this.radius,
      this.fit = BoxFit.cover})
      : image = CachedNetworkImageProvider(url),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius:
          radius != null ? BorderRadius.circular(radius!) : BorderRadius.zero,
      child: Image(
        image: image,
        width: width,
        height: height,
        color: color,
        fit: fit,
      ),
    );
  }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容