Flutter加載圖片的多樣玩法匯總
加載本地圖片
在項(xiàng)目目錄下創(chuàng)建assets文件夾,再在其文件夾下創(chuàng)建images文件夾,后面將需要的圖片復(fù)制到其中即可
在pubspec.yaml文件中添加引用
flutter: uses-material-design: true assets: - assets/images/
在Container中加載本地圖片
Container( width: 400.0,//容器寬度 height: 100.0,//容器高度 margin: const EdgeInsets.all(10.0), child: Image.asset('assets/images/bg.jpg'), ),
圓角本地圖片
效果圖
代碼
Container( decoration: ShapeDecoration( image: const DecorationImage( image: AssetImage('assets/images/bg.jpg'),//加載本地圖片 fit: BoxFit.cover ), shape:RoundedRectangleBorder(borderRadius: BorderRadiusDirectional.circular(10.0)) ), width: 400.0,//容器寬度 height: 100.0,//容器高度 margin: const EdgeInsets.all(10.0),//外邊距 )
加載網(wǎng)絡(luò)圖片-本地圖片占位圖
在網(wǎng)絡(luò)圖片未加載出來時(shí),顯示本地圖片,當(dāng)網(wǎng)絡(luò)圖片返回時(shí)替換本地圖片并展示一個(gè)深入淺出的動(dòng)畫,如果加載錯(cuò)誤,則顯示錯(cuò)誤圖
Container(//使用本地的圖片作為占位圖 width: 500.0,//容器寬度 height: 200.0,//容器高度 child: FadeInImage.assetNetwork( placeholder: "assets/images/bg.jpg", //默認(rèn)占位圖 imageErrorBuilder: (context, error, stackTrace) {//如果加載網(wǎng)絡(luò)錯(cuò)誤,顯示錯(cuò)誤背景 return Container( width: 500.0, height: 200.0, color: Colors.grey, ); }, image: "https://i0.hdslb.com/bfs/album/85dcfb9ae68ec58b447d823448b27f26e3d69b23.jpg"), ),
加載網(wǎng)絡(luò)圖片-loading
效果
代碼
當(dāng)網(wǎng)絡(luò)圖片未加載完成時(shí),顯示loading動(dòng)畫,直到加載完成
Container(//使用加載loading作為占位圖 width: 500.0, height: 150.0, margin: const EdgeInsets.only(top: 10.0), child: Image.network( "https://i0.hdslb.com/bfs/album/85dcfb9ae68ec58b447d823448b27f26e3d69b23.jpg", errorBuilder: (context,error,stackTrace){ return CircularProgressIndicator(); }, loadingBuilder: (context,child,loadingProgress){ if(loadingProgress == null)return child; return Container( alignment: Alignment.center, child: CircularProgressIndicator( value: loadingProgress.expectedTotalBytes != null ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes! : null, ), ); }, ), ),
圓角、邊框、漸變
BoxDecoration | 解釋 |
---|---|
color | 背景顏色 |
image | 背景圖片 |
border | 邊框 |
borderRadius | 圓角 |
gradient | 漸變 |
shape | 形狀 |
Container( //單組件布局容器,只允許有一個(gè)子組件 alignment: Alignment.center,//對(duì)齊方式:居中 width: 500.0,//容器寬度 height: 200.0,//容器高度 //color: Colors.blue,容器背景顏色 padding: const EdgeInsets.all(10.0),//外邊距 margin: const EdgeInsets.all(10.0),//內(nèi)邊距 decoration: BoxDecoration(//類似原生Android的XML創(chuàng)建樣式一樣 gradient: const LinearGradient(colors: [Colors.red,Colors.green,Colors.amber]),//線性漸變,需要先注釋原有存在的背景顏色語句 border: Border.all(color:Colors.black,width: 5.0),//繪制邊框,邊框顏色為黑色,邊框?qū)挾葹? borderRadius: const BorderRadius.all(Radius.circular(10.0)),//設(shè)置邊框圓角 shape: BoxShape.rectangle//盒子樣式形狀 ), child: const Text(容器內(nèi)子組建 'Hello World!', style: TextStyle(fontSize: 20.0,color: Colors.white), ), ),
內(nèi)邊距和外邊距使用如下:
padding: const EdgeInsets.all(10.0),//外邊距 margin: const EdgeInsets.all(10.0),//內(nèi)邊距
總結(jié)
到此這篇關(guān)于Flutter加載圖片的文章就介紹到這了,更多相關(guān)Flutter加載圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android仿餓了么加入購(gòu)物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫的按鈕效果(實(shí)例詳解)
這篇文章主要介紹了Android仿餓了么加入購(gòu)物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫的按鈕效果(實(shí)例詳解)的相關(guān)資料,需要的朋友可以參考下2017-01-01Android實(shí)現(xiàn)圓形ProgressBar停止轉(zhuǎn)動(dòng)的方法詳解
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓形ProgressBar停止轉(zhuǎn)動(dòng)方法的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法
這篇文章主要介紹了Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法,實(shí)例分析了Android廣播BroadCast控制activity關(guān)閉的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android中解決RecyclerView各種點(diǎn)擊事件的方法
這篇文章主要介紹了Android中解決RecyclerView各種點(diǎn)擊事件的方法,完美解決RecyclerView點(diǎn)擊事件、長(zhǎng)按事件、子項(xiàng)點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android 使用Path實(shí)現(xiàn)涂鴉功能
到月底了最近比較空閑,今天抽空給大家實(shí)現(xiàn)一個(gè)涂鴉效果,會(huì)分幾步實(shí)現(xiàn),這里有一個(gè)重要的知識(shí)點(diǎn)就是圖層,要理解這個(gè)。下面先從簡(jiǎn)單的說起,一起看看代碼吧2016-12-12Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼
EditText搜索結(jié)果下拉框、自動(dòng)or回調(diào)模式、可diy、使用超簡(jiǎn)便。這篇文章主要介紹了Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼,需要的朋友可以參考下2019-10-10Android優(yōu)雅地處理按鈕重復(fù)點(diǎn)擊的幾種方法
這篇文章主要介紹了Android優(yōu)雅地處理按鈕重復(fù)點(diǎn)擊的幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09實(shí)例詳解Android Selector和Shape的用法
shape和selector是Android UI設(shè)計(jì)中經(jīng)常用到的,比如我們要自定義一個(gè)圓角Button,點(diǎn)擊Button有些效果的變化,就要用到shape和selector,通過本文結(jié)合代碼實(shí)例給大家詳解Android Selector和Shape的用法,感興趣的朋友一起學(xué)習(xí)吧2016-01-01