Flutter图片Image、本地图片、程程图片、圆片剪切、圆形图片

目录

图片组件的介绍

1.Image.network加载图片   

        1.1 Image scale图片缩小一倍

        1.2 Image alignment使用

        1.3 Image fit 属性的取值及说明

        1.3.1 Contain 默认效果 

        1.3.2 Fill 图片会缩放至完全填满目标区域(宽高)

        1.3.3 Fill 图片会缩放至完全填满目标区域(宽高)

        1.4 repeat 图片平铺

        1.4.1 repeatX轴\Y轴都平铺

1.5. 实现圆角图片

        1.5.1 Container 实现圆角图片

        1.5.2 Container circular 圆角参数设置

        1.6.1 使用ClipOval使用实现一个圆形图片

2.加载本地图片

        2.1 要在 Flutter 中加载本地图片,需要完成两个主要步骤:       

        2.1.2 在 项目下创建images资源文件

        2.1.2 在 pubspec.yaml 中配置图片资源路径

        2.1.3 使用 Image.asset 或 Image 组件加载图片


图片组件的介绍

1.Image.network加载图片   

import 'package:flutter/material.dart';void main() {runApp(MaterialApp(home: Scaffold(appBar: AppBar(title: Text("sss")), body: MyApp2()),),);
}class MyApp2 extends StatefulWidget {@overrideState<StatefulWidget> createState() {return MyAppState();}
}class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",),),);}
}

        1.1 Image scale图片缩小一倍

class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",scale: 2, //图片缩小),),);}
}

        1.2 Image alignment使用

class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",scale: 2,//缩放alignment: Alignment.centerLeft,//位置),),);}
}或者修改Container位置class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(alignment: Alignment.centerLeft, //位置height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",scale: 2,//缩放// alignment: Alignment.centerLeft, //位置),),);}
}

        1.3 Image fit 属性的取值及说明

属性名作用描述
BoxFit.fill- 强制图片填满整个容器(宽高均与容器一致)。
- 可能导致图片变形(宽高比被忽略)。
BoxFit.contain- 图片按原比例缩放,完全包含在容器内(宽或高中至少一边与容器边缘对齐)。
- 可能在另一边留有空白。
BoxFit.cover- 图片按原比例缩放,覆盖整个容器(宽和高均不小于容器)。
- 超出容器的部分会被裁剪。
BoxFit.fitWidth- 图片宽度与容器宽度一致,高度按比例缩放。
- 可能超出容器高度或留有空白。
BoxFit.fitHeight- 图片高度与容器高度一致,宽度按比例缩放。
- 可能超出容器宽度或留有空白。
BoxFit.none- 图片按原始尺寸显示,不进行任何缩放。
- 若图片尺寸大于容器,会被截断显示。
BoxFit.scaleDown- 类似于 contain,但只在图片尺寸大于容器时缩小,不会放大图片。
- 保持原图清晰度,避免低分辨率图片拉伸模糊。

         1.3.1 Contain 默认效果 

class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(// alignment: Alignment.centerRight, //位置height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",// scale: 2,//缩放// alignment: Alignment.centerLeft, //位置fit: BoxFit.contain, //图片fit属性 = 默认),),);}
}

        1.3.2 Fill 图片会缩放至完全填满目标区域(宽高)

        1.3.3 Fill 图片会缩放至完全填满目标区域(宽高)
class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(// alignment: Alignment.centerRight, //位置height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",// scale: 2,//缩放// alignment: Alignment.centerLeft, //位置fit: BoxFit.fitWidth, //图片fit属性= 宽度充满),),);}
}

        1.4 repeat 图片平铺


class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(// alignment: Alignment.centerRight, //位置height: 300,width: 500,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",// scale: 2,//缩放// alignment: Alignment.centerLeft, //位置// fit: BoxFit.fitWidth, //图片fit属性= 宽度充满repeat: ImageRepeat.repeatX, //X轴平铺),),);}
}

        1.4.1 repeatX轴\Y轴都平铺

class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(// alignment: Alignment.centerRight, //位置height: 300,width: 500,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",// scale: 2,//缩放// alignment: Alignment.centerLeft, //位置// fit: BoxFit.fitWidth, //图片fit属性= 宽度充满repeat: ImageRepeat.repeat, //X轴\Y轴都平铺),),);}
}

1.5. 实现圆角图片

        1.5.1 Container 实现圆角图片

import 'package:flutter/material.dart';void main() {runApp(MaterialApp(home: Scaffold(appBar: AppBar(title: Text("sss")),body: Column(children: [MyApp2(), SizedBox(height: 20), Circular()]),),),);
}class MyApp2 extends StatefulWidget {@overrideState<StatefulWidget> createState() {return MyAppState();}
}class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(// alignment: Alignment.centerRight, //位置height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",// scale: 2,//缩放// alignment: Alignment.centerLeft, //位置// fit: BoxFit.fitWidth, //图片fit属性= 宽度充满repeat: ImageRepeat.repeat, //X轴\Y轴都平铺),),);}
}//实现一个圆形图片
class Circular extends StatelessWidget {@overrideWidget build(BuildContext context) {return Container(margin: EdgeInsets.fromLTRB(0, 10, 0, 0),height: 300,width: 300,decoration: BoxDecoration(color: Colors.yellow,borderRadius: BorderRadius.circular(150),image: DecorationImage(image: NetworkImage("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",),fit: BoxFit.cover,),),);}
}

        1.5.2 Container circular 圆角参数设置

borderRadius: BorderRadius.circular(10)//圆角

        1.6.1 使用ClipOval使用实现一个圆形图片

import 'package:flutter/material.dart';void main() {runApp(MaterialApp(home: Scaffold(appBar: AppBar(title: Text("sss")),body: Column(children: [MyApp2(),SizedBox(height: 20),Circular(),SizedBox(height: 30),ClipImage(),],),),),);
}class MyApp2 extends StatefulWidget {@overrideState<StatefulWidget> createState() {return MyAppState();}
}class MyAppState extends State<MyApp2> {@overrideWidget build(BuildContext context) {return Center(child: Container(// alignment: Alignment.centerRight, //位置height: 150,width: 150,decoration: BoxDecoration(color: Colors.yellow),child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",// scale: 2,//缩放// alignment: Alignment.centerLeft, //位置// fit: BoxFit.fitWidth, //图片fit属性= 宽度充满repeat: ImageRepeat.repeat, //X轴\Y轴都平铺),),);}
}//实现一个圆形图片
class Circular extends StatelessWidget {@overrideWidget build(BuildContext context) {return Container(margin: EdgeInsets.fromLTRB(0, 10, 0, 0),height: 150,width: 150,decoration: BoxDecoration(color: Colors.yellow,borderRadius: BorderRadius.circular(10),image: DecorationImage(image: NetworkImage("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",),fit: BoxFit.cover,),),);}
}//使用ClipOval使用实现一个圆形图片
class ClipImage extends StatelessWidget {@overrideWidget build(BuildContext context) {return ClipOval(child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",width: 150,height: 150,fit: BoxFit.cover,),);}
}


//使用ClipOval使用实现一个圆形图片
class ClipImage extends StatelessWidget {@overrideWidget build(BuildContext context) {return ClipOval(child: Image.network("https://img2.baidu.com/it/u=1069891123,3492077884&fm=253&fmt=auto&app=138&f=JPEG?w=304&h=456",width: 150,height: 150,fit: BoxFit.cover,),);}
}

2.加载本地图片

        2.1 要在 Flutter 中加载本地图片,需要完成两个主要步骤:       

        2.1.2 在 项目下创建images资源文件

            2.1.2 在 pubspec.yaml 中配置图片资源路径

            2.1.3 使用 Image.asset 或 Image 组件加载图片
    //加载一个本地图片
    class LocalImage extends StatelessWidget {@overrideWidget build(BuildContext context) {return Container(width: 150,height: 150,child: Image.asset("images/a.png"),);}
    }
    

    class LocalImage extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(appBar: AppBar(title: const Text('本地圆形图片加载示例')),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [// 使用CircleAvatar加载圆形图片CircleAvatar(radius: 100,backgroundImage: AssetImage('images/profile.png'),),const SizedBox(height: 30),// 使用ClipOval自定义圆形图片ClipOval(child: Image.asset('images/background.jpg',width: 200,height: 200,fit: BoxFit.cover,),),const SizedBox(height: 30),// 使用Container的decoration属性Container(width: 150,height: 150,decoration: BoxDecoration(shape: BoxShape.circle,image: DecorationImage(image: AssetImage('images/icon.png'),fit: BoxFit.cover,),),),],),),),);}
    }

    本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/907525.shtml

    如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

    相关文章

    Prometheus学习之pushgateway和altermanager组件

    [rootnode-exporter41 /usr/local/alertmanager-0.28.1.linux-amd64]# pwd /usr/local/alertmanager-0.28.1.linux-amd64[rootnode-exporter41 /usr/local/alertmanager-0.28.1.linux-amd64]# cat alertmanager.yml # 通用配置 global:resolve_timeout: 5msmtp_from: 914XXXXX…

    NHANES指标推荐:CQI

    文章题目&#xff1a;The impact of carbohydrate quality index on menopausal symptoms and quality of life in postmenopausal women 中文标题&#xff1a;碳水化合物质量指数对绝经后妇女更年期症状和生活质量的影响 发表杂志&#xff1a;BMC Womens Health 影响因子&…

    【cpp-httplib】 安装与使用

    cpp-httplib 1. 介绍2. 安装3. 类与接口3.1 httplib请求3.2 httplib响应3.3 httplib服务端3.4 httplib客户端 4. 使用4.1 服务端4.2 客户端 1. 介绍 C HTTP 库&#xff08;cpp-httplib&#xff09;是一个轻量级的 C HTTP 客户端/服务器库&#xff0c;它提供了简单的 API 来创建…

    Electron-vite【实战】MD 编辑器 -- 系统菜单(含菜单封装,新建文件,打开文件,打开文件夹,保存文件,退出系统)

    最终效果 整体架构 src/main/index.ts import { createMenu } from ./menu在 const mainWindow 后 // 加载菜单createMenu(mainWindow)src/main/menu.ts import { BrowserWindow, Menu, MenuItem, MenuItemConstructorOptions, dialog, shell } from electron import fs from…

    【第4章 图像与视频】4.5 操作图像的像素

    文章目录 前言示例-获取和修改图像数据图像数据的遍历方式图像滤镜负片滤镜黑白滤镜浮雕滤镜filter滤镜属性 前言 getImageData() 与 putImageData() 这两个方法分别用来获取图像的像素信息&#xff0c;以及向图像中插入像素。与此同时&#xff0c;如果有需要&#xff0c;也可…

    【Docker 从入门到实战全攻略(一):核心概念 + 命令详解 + 部署案例】

    1. 是什么 Docker 是一个用于开发、部署和运行应用程序的开源平台&#xff0c;它使用 容器化技术 将应用及其依赖打包成独立的容器&#xff0c;确保应用在不同环境中一致运行。 2. Docker与虚拟机 2.1 Docker&#xff08;容器化&#xff09; 容器化是一种轻量级的虚拟化技术…

    Vue:axios(POST请求)

    发送 POST 请求 基本用法 axios.post(/api/login, {username: lcyyyy,password: 123456 }) .then(response > {console.log(请求成功:, response.data); }) .catch(error > {console.error(请求失败:, error); });在 Vue 组件中使用 export default {methods: {async …

    一周学会Pandas2之Python数据处理与分析-数据重塑与透视-unstack() - 解堆 (行 -> 列)

    锋哥原创的Pandas2 Python数据处理与分析 视频教程&#xff1a; 2025版 Pandas2 Python数据处理与分析 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili unstack() 是 pandas 中用于数据重塑的重要方法&#xff0c;它与 stack() 互为逆操作。unstack() 的主要功能是将行索…

    基于大模型预测的FicatIII-IV期股骨头坏死综合治疗研究报告

    目录 一、引言 1.1 研究背景与目的 1.2 国内外研究现状 1.3 研究意义和创新点 二、FicatIII-IV 期股骨头坏死概述 2.1 疾病定义与分期 2.2 病因与病理机制 2.3 临床症状与诊断方法 三、大模型预测原理与方法 3.1 大模型简介 3.2 数据收集与预处理 3.3 模型训练与优…

    C++?多态!!!

    一、引言 众所周知&#xff0c;C有三大特性&#xff0c;它们分别是封装、继承和多态&#xff0c;在之前的文章中已经详细介绍过封装和继承了&#xff0c;今天我们将一起学习多态相关的知识&#xff0c;如果还想了解封装、继承相关的知识&#xff0c;可以跳转到以下链接&#xf…

    electron安装报错处理

    electron安装报错 解决方法&#xff1a; 修改 C:\Users\用户名.npmrc下配置文件 添加代码 electron_mirrorhttps://cdn.npmmirror.com/binaries/electron/ electron_builder_binaries_mirrorhttps://npmmirror.com/mirrors/electron-builder-binaries/最后代码 registryhtt…

    Windows10下使用QEMU安装Ubuntu20.04虚拟机,并启用硬件加速

    Windows10下使用QEMU安装Ubuntu20.04虚拟机&#xff0c;并启用硬件加速 作者将狼才鲸创建日期2025-05-30 CSDN阅读地址&#xff1a;Windows10下使用QEMU安装Ubuntu20.04虚拟机&#xff0c;并启用硬件加速 本文档源码地址&#xff1a;Windows10下使用QEMU安装Ubuntu20.04虚拟机…

    顶刊SCS | 基于视觉语言大模型推理分割的建筑足迹尺度功能分类, 样本数据和代码已开源!

    论文介绍 题目&#xff1a;Visual-language reasoning segmentation (LARSE) of function-level building footprint across Yangtze River Economic Belt of China 期刊&#xff1a;Sustainable cities and society&#xff08;中科院一区TOP&#xff0c;IF10.5&#xff09;…

    【软件】navicat 官方免费版

    Navicat Premium Lite https://www.navicat.com.cn/download/navicat-premium-lite

    每个路由器接口,都必须分配所属网络内的 IP 地址,用于转发数据包

    在IP网络中&#xff0c;主机&#xff08;Host&#xff09;和路由器接口&#xff08;Router Interface&#xff09;都需要分配网络地址&#xff08;IP地址&#xff09;。 1. 主机&#xff08;Host&#xff09;的IP地址分配 (1) 作用 主机的IP地址用于唯一标识该设备&#xff0…

    鸿蒙OSUniApp页面切换动效实战:打造流畅精致的转场体验#三方框架 #Uniapp

    UniApp页面切换动效实战&#xff1a;打造流畅精致的转场体验 引言 在移动应用开发中&#xff0c;页面切换动效不仅能提升用户体验&#xff0c;还能传达应用的品质感。随着HarmonyOS的普及&#xff0c;用户对应用的动效体验要求越来越高。本文将深入探讨如何在UniApp中实现流畅…

    Tesseract OCR 安装与中文+英文识别实现

    一、下载 https://digi.bib.uni-mannheim.de/tesseract/ 下载&#xff0c;尽量选择时间靠前的&#xff08;识别更好些&#xff09;。符合你的运行机&#xff08;我的是windows64&#xff09; 持续点击下一步安装&#xff0c;安装你认可的路径即可&#xff0c;没必要配置环境变…

    Visual Studio 2022 发布独立的 exe 文件

    我们在用 Visual Studio 2022 写好一个 exe 程序之后&#xff0c;如果想把这个拿到其他地方运行&#xff0c;需要把 exe 所在的文件夹一起拿过去。 编译出来的 exe 文件需要其他几个文件一同放在同一目录才能运行&#xff0c;原因在于默认情况下&#xff0c;Visual Studio 是把…

    Kotlin-特殊类型

    文章目录 数据类型枚举类型匿名类和伴生对象单例类伴生对象 数据类型 声明一个数据类非常简单: //在class前面添加data关键字表示为一个数据类 data class Student(var name: String, var age: Int)数据类声明后,编译器会根据主构造函数中声明的所有属性自动为其生成以下函数…

    在线博客系统【测试报告】

    &#x1f552; 一. 项目背景 由于纸质笔记容易丢失&#xff0c;携带不变&#xff0c;为了方便自己学习的过程中记录笔记&#xff0c;特开发了这个博客系统。这个系统后端采用 SpringBoot MyBatis SpringMVC &#xff1b;前端使用Html CSS JS&#xff1b;数据库使用的是Mysq…