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
    繁體地址,請注明出處:http://hk.pswp.cn/news/907525.shtml
    英文地址,請注明出處:http://en.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…