Flutter 初识:图像与媒体控件

在Flutter中,处理图像和媒体控件(例如视频和音频)变得非常简单和高效。

图像控件

本地图片Image.asset

要显示应用程序中的本地图片,可以使用Image.asset:

属性解析
 Image.asset(
    String name, {
    super.key,
    AssetBundle? bundle,
    this.frameBuilder,
    this.errorBuilder,
    this.semanticLabel,
    this.excludeFromSemantics = false,
    double? scale,
    this.width,
    this.height,
    this.color,
    this.opacity,
    this.colorBlendMode,
    this.fit,
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
    this.centerSlice,
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
    this.isAntiAlias = false,
    String? package,
    this.filterQuality = FilterQuality.low,
    int? cacheWidth,
    int? cacheHeight,
  })
  • name: 图片资源的名称或 URL。
  • key: 唯一标识这个组件的键值。
  • bundle: 用于加载图片的资源包。
  • frameBuilder: 自定义图片构建方式的构建器函数。
  • errorBuilder: 处理加载图片时错误的构建器函数。
  • semanticLabel: 用于无障碍访问的标签。
  • excludeFromSemantics: 是否将图片排除在语义树之外。
  • scale: 图片的缩放因子。
  • width: 图片的宽度。
  • height: 图片的高度。
  • color: 用于给图片着色的颜色。
  • opacity: 图片的不透明度。
  • colorBlendMode: 应用颜色滤镜时使用的混合模式。
  • fit: 图片如何适配其外框。
  • alignment: 图片在其边界内如何对齐。
  • repeat: 如果图片没有填满其框,如何重复图片。
  • centerSlice: 描述九切片图像中心部分的矩形。
  • matchTextDirection: 当文本方向为 RTL(从右到左)时是否水平翻转图片。
  • gaplessPlayback: 当图像提供者改变时,是否继续显示旧图片。
  • isAntiAlias: 是否对图片应用抗锯齿处理。
  • package: 从哪个包加载图片的包名。
  • filterQuality: 图片过滤的质量。
  • cacheWidth: 缓存图片的宽度。
  • cacheHeight: 缓存图片的高度。
示例
class ImageWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Image.asset 示例')),
        body: Center(
          child: Image.asset(
            'assets/1.jpeg',
            key: Key('image_key'),
            scale: 2.0,
            width: 200,
            height: 200,
            // color: Colors.red,
            colorBlendMode: BlendMode.colorBurn,
            fit: BoxFit.cover,
            alignment: Alignment.topLeft,
            repeat: ImageRepeat.noRepeat,
            matchTextDirection: false,
            gaplessPlayback: true,
            isAntiAlias: true,
            filterQuality: FilterQuality.high,
            cacheWidth: 100,
            cacheHeight: 100,
          ),
        ),
      ),
    );
  }
}

备注:
如图:
在这里插入图片描述

 
 

网络图片Image.network

要显示来自网络的图片,可以使用Image.network:

属性解析
Image.network(
    String src, {
    super.key,
    double scale = 1.0,
    this.frameBuilder,
    this.loadingBuilder,
    this.errorBuilder,
    this.semanticLabel,
    this.excludeFromSemantics = false,
    this.width,
    this.height,
    this.color,
    this.opacity,
    this.colorBlendMode,
    this.fit,
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
    this.centerSlice,
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
    this.filterQuality = FilterQuality.low,
    this.isAntiAlias = false,
    Map<String, String>? headers,
    int? cacheWidth,
    int? cacheHeight,
  }) 
  • src (String): 图像的 URL,是必填项。
  • key (Key): 控件的唯一标识符,用于树形结构和状态管理。
  • scale (double): 图像的缩放比例,默认值为 1.0。
  • frameBuilder (ImageFrameBuilder?): 绘制每一帧时的回调函数。
  • loadingBuilder (ImageLoadingBuilder?): 图像正在加载时的回调函数。
  • errorBuilder (ImageErrorWidgetBuilder?): 当图像加载失败时构建错误组件的回调函数。
  • semanticLabel (String?): 语义标签,用于无障碍支持。
  • excludeFromSemantics (bool): 是否将该图像从语义树中排除,默认值为 false。
  • width (double?): 图像的宽度。
  • height (double?): 图像的高度。
  • color (Color?): 图像的颜色,会与 colorBlendMode 一起使用。
  • opacity (Animation?): 图像的不透明度,可以使用动画。
  • colorBlendMode (BlendMode?): 图像颜色的混合模式。
  • fit (BoxFit?): 图像的适应方式,例如填充、包含等。
  • alignment (Alignment): 图像的对齐方式,默认值为 Alignment.center。
  • repeat (ImageRepeat): 图像的重复方式,默认值为 ImageRepeat.noRepeat。
  • centerSlice (Rect?): 中心切片矩形,用于图像拉伸。
  • matchTextDirection (bool): 是否根据文本方向来镜像翻转图像,默认值为 false。
  • gaplessPlayback (bool): 是否在更改图像时持续显示上一张图像,直到新图像可用,默认值为 false。
  • filterQuality (FilterQuality): 图
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值