Flutter 完美封装Image

紧接着上一篇完美封装Text,本次依然使用android的写法,封装Image,加载本地图片比较简单,这边主要加载网络图片,同步设置宽度、高度、圆角、圆形图片、边框宽度、边框颜色等参数

根据用户需求又更新了一版,保持原有功能不变的情况下新增以下内容

1.支持使用网络图片,本地图片,手机选取的图片,自动识别显示,通通只需要把string类型的url传入即可自动识别加载

2.加载网络图片使用第三方库cached_network_image,加载图片的时候缓存到本地,提升没有网络的时候和下一次再进入时候的加载时间

3.宽度和高度可以自己设置相应的宽度和高度,也可以不设置,不设置就自动显示图片原有的大小

第三方图片加载库版本:

cached_network_image: ^3.2.0

先看效果图

 依然还是熟悉的配方,依然还是熟悉的味道,让你写Flutter有写android组件的感觉

ImageView(
            imageUrl,
            width: 100,
            height: 100,
            marginLeft: 10,
            cornerRadius: 5,
            borderWidth: 2,
            marginTop: 20,
            isCircle: true,
            borderColor: Colors.yellow,
          )

封装的类我们依然使用ImageView,以下是源代码

import 'dart:io';

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

class ImageView extends StatelessWidget {
  String url = "";
  double? width;
  double? height;

  double? marginLeft = 0;
  double? marginRight = 0;
  double? marginTop = 0;
  double? marginBottom = 0;
  double? cornerRadius = 0;
  double? margin = 0;
  double? borderWidth = 0;
  Color? borderColor = Colors.transparent;
  bool? isCircle = false;
  Color? backgroundColor = Colors.transparent;
  VoidCallback? onClick;

  ImageView(
    this.url, {
    this.width,
    this.height,
    this.marginBottom,
    this.marginRight,
    this.marginTop,
    this.marginLeft,
    this.margin,
    this.cornerRadius,
    this.isCircle,
    this.borderColor,
    this.borderWidth,
    this.backgroundColor,
    this.onClick,
  }) {
    if (margin != null) {
      if (margin != null && margin! > 0) {
        marginLeft = margin;
        marginTop = margin;
        marginRight = margin;
        marginBottom = margin;
      }
    }

    if (isCircle != null) {
      if (isCircle ?? true) {
        cornerRadius = width ?? 0 / 2;
      }
    }

    if (onClick == Null) {
      onClick = () {};
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.fromLTRB(this.marginLeft ?? 0, this.marginTop ?? 0,
            this.marginRight ?? 0, this.marginBottom ?? 0),
        decoration: new BoxDecoration(
          border: new Border.all(
              width: this.borderWidth ?? 0,
              color: this.borderColor ?? Colors.transparent),
          color: this.backgroundColor,
          borderRadius:
              new BorderRadius.all(new Radius.circular(this.cornerRadius ?? 0)),
        ),
        child: GestureDetector(
          onTap: this.onClick,
          child: ClipRRect(
              borderRadius: BorderRadius.circular(this.cornerRadius ?? 0),
              child: getImage()),
        ));
  }

  Widget getImage() {
    if (url.startsWith("http")) {
      //网络图片
      return CachedNetworkImage(
        imageUrl: url,
        width: this.width,
        height: this.height,
        fit: BoxFit.cover,
      );
    } else if (url.startsWith("images")) {
      //项目内图片
      print("加载项目图片:${url}");
      return Image.asset(url,
          width: this.width,
          height: this.height,
          fit: BoxFit.cover);
    } else {
      //加载手机里面的图片
      return Image.file(File(url),
          width: this.width,
          height: this.height,
          fit: BoxFit.cover);
    }
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CDimage 是一款镜像制作工具,出自微软。它是微软内部“专用的”ISO 封装工具,不向外提供;所以,我们平时所看到的、使用的都是从微软内部泄露出来的~!!CDimage是系统整合不可少的利器,大大节省了光盘存放空间;我们平时见的"N合1”光盘大都是由CDimage(或内置CDimage内核的光盘工具,如Easyboot等)创建的。 但是我想网友们最关心的或许并不是以上问题,而是“CDimage到底能不能封装出SHA1值和官方一致的ISO镜像?”。下面我们就来说说CDimage的用法--其实你英文好的话用help命令查看即可!以下是常用参数[2.47版]的中文说明: -l 卷标,无空格(例如:-lMYLABEL) -t 所有目录和文件的时间标记,无空格,任意分隔符(例如:-t12/31/2000,15:01:00) -g 把文件时间从本地时间转换为GMT时间 -h 包含隐藏的文件和目录 -n 允许长文件名(比DOS的8.3文件名长) -nt 允许长文件名,与NT3.51兼容(-nt与-d不能同时使用) -d 不强制转换文件名的小写字母为大写字母 -c 使用ANSI文件名,不用源OEM文件名 -b "El Torito"光盘启动文件,无空格(例如:-bc:\location\cdboot.bin) -p "El Torito"启动目录的平台ID -e 在El Torito启动目录中不要设置软盘模拟方式 -s 在镜像文件中签上数字签名(无空格,给出RPC服务器名和结束点名称,像这样-sServerName:EndPointName) -x 在镜像中计算并制作"AutoCRC"值 -o 优化容量:重复的文件只制作一次 -y 试验选项,跟一个数字(例如:-y1),为试验的目的而产生ISO-9660的非标准变化: 1 在文件名后续加版本号';1'(7.5.1) 2 圆整目录大小为2K的倍数(6.8.1.3) 5 先写\i386目录,逆序 6 允许目录记录正好排到扇区结束点(符合ISO-96606.8.1.1但是突破了MSCDEX) 7 产生NT4.0下的16位应用程序时报警 -oc 更慢的重复文件探测方式:二进制比较而不是MD5哈西值 -oi 比较文件时忽略diamond压缩时间标记 -os 建立镜像时显示重复的文件(-o选项可以组合,像这样:-ocis) -k (keep)即使某些源文件打开失败也生成镜像 -m 忽略镜像文件最大681,984,000bytes的限制 -a 《分配概要》显示文件和目录的大小 -q 只扫描源文件,不建立镜像文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值