Flutter 图片工具类封装

作者: 呆头呆脑雷 | 来源:发表于2023-05-05 10:46 被阅读0次

目的是为了整合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,
      ),
    );
  }
}

相关文章

网友评论

    本文标题:Flutter 图片工具类封装

    本文链接:https://www.haomeiwen.com/subject/tanfsdtx.html