溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用flutter動(dòng)態(tài)加載網(wǎng)絡(luò)圖片

發(fā)布時(shí)間:2021-05-21 16:16:31 來源:億速云 閱讀:425 作者:Leah 欄目:移動(dòng)開發(fā)

今天就跟大家聊聊有關(guān)如何使用flutter動(dòng)態(tài)加載網(wǎng)絡(luò)圖片,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

需要添加依賴包

flukit: ^1.0.0
引用 import 'package:flukit/flukit.dart';
//這一坨放在自己想要顯示輪播圖的地方
AspectRatio(
  aspectRatio:1.0,// 16.0 / 9.0,
  child: Swiper(
   indicatorAlignment: AlignmentDirectional.topEnd,
   circular: true,
   autoStart:false,
   indicator: NumberSwiperIndicator(),//使用的官方的 分?jǐn)?shù)下標(biāo)
   children:AspecRaticImgs(pro.image),//這里是一個(gè)List<String>類型的參數(shù),存放的圖片Url列表
  ),
  );
//輪播圖片
class NumberSwiperIndicator extends SwiperIndicator{
 @override
 Widget build(BuildContext context, int index, int itemCount) {
 if(itemCount>1){
 return Container(
  decoration: BoxDecoration(
   color: Colors.black45,
   borderRadius: BorderRadius.circular(20.0)
  ),
  margin: EdgeInsets.only(top: 10.0,right: 5.0),
  padding: EdgeInsets.symmetric(horizontal: 6.0,vertical: 2.0),
  child: Text("${++index}/$itemCount", style: TextStyle(color: SQColor.white, fontSize: 18.0)),
 );
 }else{
  return Container();
 }
 }
}

//這里我一開始用foreach循環(huán),發(fā)現(xiàn)不行 會(huì)報(bào)錯(cuò),說我add時(shí)用了空對(duì)象,頭疼,與C#真的是大相徑庭
List<Widget> AspecRaticImgs(List<String> imgUrl) {
 return imgUrl.map<Widget>((url){
 return Image.network(
  url,
  height: 400,
  fit: BoxFit.cover,
 );
 }).toList();
}

List<Widget> AspecRaticImgs(List<String> imgUrl) {
 return imgUrl.map<Widget>((url){
 return CachedNetworkImage(//這個(gè)加載更加舒服,當(dāng)在加載中的時(shí)候,有一個(gè)加載進(jìn)度
  imageUrl: url,
  height: 400,
  fit: BoxFit.cover,
  placeholder: CustomWidgets.loadingPlaceHolder,
  errorWidget: Image.asset('images/bg_gray.png',height: 400),
 );
 }).toList();
}

看完上述內(nèi)容,你們對(duì)如何使用flutter動(dòng)態(tài)加載網(wǎng)絡(luò)圖片有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI