flutter Image

Flutter中,Image是一个用于显示图片的控件,可以显示网络图片、本地图片以及Asset中的图片。Image控件支持多种常见的图片格式,例如PNG、JPEG、GIF等。

Dart 复制代码
  const Image({
    super.key,
    required this.image,
    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.isAntiAlias = false,
    this.filterQuality = FilterQuality.low,
  })

Image控件重要属性

Image.network:指定显示的网络图片的URL,用于加载网络图片。

Image.asset:指定显示的本地图片的Asset路径,用于加载Asset目录下的图片。

width和height:设置图片的宽度和高度。

Image.asset

通过Image.asset加载本地图片

Dart 复制代码
import 'package:flutter/material.dart';

class ImageDemoPage extends StatelessWidget {
  const ImageDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("ImageDemoPage"),
      ),
      body: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text("TEST IMAGE"),
            Container(
                width: 100,
                height: 100,
                child: Image.asset("static/test_image.png"))
          ],
        ),
      ),
    );
  }
}

Image.network

用于加载网络图片。

Dart 复制代码
import 'package:flutter/material.dart';

class ImageDemoPage extends StatelessWidget {
  const ImageDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("ImageDemoPage"),
      ),
      body: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text("TEST IMAGE"),
            Container(
                width: 100,
                height: 100,
                child: Image.network("https://cdn.carlcare.com/carlcare/default/1273327404667740207"))
          ],
        ),
      ),
    );
  }
}

实现圆角的几种方式

使用BoxDecoration+DecorationImage+AssetImage实现

Dart 复制代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ImageDemoPage extends StatelessWidget {
  const ImageDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("ImageDemoPage"),
      ),
      body: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text("USE BoxDecoration"),
            Container(
              width: 100,
              height: 100,
              decoration: const BoxDecoration(
                  color: Colors.blue,
                  image: DecorationImage(
                    fit: BoxFit.cover,
                    image: AssetImage("static/test_image.png"),
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(10.0))),
            )
          ],
        ),
      ),
    );
  }
}

ClipRRect+Image实现

Dart 复制代码
class ImageDemoPage extends StatelessWidget {
  const ImageDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("ImageDemoPage"),
      ),
      body: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text("USE ClipRRect"),
            ClipRRect(
              borderRadius: const BorderRadius.all(Radius.circular(10.0)),
              child: Image.asset(
                "static/test_image.png",
                fit: BoxFit.cover,
                width: 100,
                height: 100,
              ),
            )
          ],
        ),
      ),
    );
  }
}
相关推荐
重生之我在四川卖苕皮24 天前
QT仿QQ聊天项目,第二节,美化登录界面
c++·qt·image·qss·qt控件
JUNIOR_MU1 个月前
【VMware VCF】更新 VCF 5.1 至 VCF 5.2 版本。
esxi·nsx·image·vsphere·lcm·vsan·vcenter server·vlcm·vcf·vmware cloud foundation·nsx manager·sddc manager·映像
不cong明的亚子2 个月前
关于优化活动页面的大尺寸图片的展示问题
html5·image·预加载图片
伊织code3 个月前
pdfplumber - pdf 数据提取
pdf·ocr·image·table·pdfplumber
柳木泉3 个月前
04--Docker
linux·运维·笔记·docker·容器·centos·image
一丝晨光4 个月前
ios CCUINavigationBar.m
开发语言·ios·objective-c·cocoa·软件开发·image
亚林瓜子4 个月前
在Mac M1上面使用Dockerfile打x86_64镜像
macos·docker·image·dockerfile·m1·构建·x86_64
一丝晨光4 个月前
ios CCUIImage.m
ios·objective-c·cocoa·api·image
JUNIOR_MU6 个月前
【VMware vSphere】使用vSphere Lifecycle Manager(vLCM)管理独立主机和集群的生命周期。
esxi·image·vcenter·vlcm