日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Flutter】Image 组件 ( 内存加载 Placeholder | transparent_image 透明图像插件 )

發(fā)布時(shí)間:2025/6/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】Image 组件 ( 内存加载 Placeholder | transparent_image 透明图像插件 ) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 一、transparent_image 透明圖像插件
  • 二、內(nèi)存加載 Placeholder
  • 三、完整代碼示例
  • 四、相關(guān)資源





一、transparent_image 透明圖像插件



安裝 transparent_image 插件 :

  • 搜索插件 : 在 https://pub.dev/packages 中搜索 transparent_image 插件 ;
  • 插件地址 : https://pub.dev/packages/transparent_image
  • 配置插件 : 在 pubspec.yaml 中配置插件 ;
dependencies:transparent_image: ^2.0.0
  • 獲取插件 : 點(diǎn)擊 pubspec.yaml 中右上角的 Pub get 按鈕 , 獲取插件 ;
  • 導(dǎo)入頭文件 :
import 'package:transparent_image/transparent_image.dart';



二、內(nèi)存加載 Placeholder



Placeholder 是一個(gè)占位控件 , 在圖片還沒有就緒時(shí) , 如從網(wǎng)絡(luò)獲取圖片 , 先使用 Placeholder 占據(jù)圖片組件的位置 ;


代碼示例 :

Stack(children: [// 進(jìn)度條Center(child: CircularProgressIndicator(),),Center(// 網(wǎng)絡(luò)加載時(shí)漸變出現(xiàn)child: FadeInImage.memoryNetwork(// Placeholderplaceholder: kTransparentImage,image: "https://img-blog.csdnimg.cn/2021032321394771.png",),)], ),

運(yùn)行效果 : 進(jìn)度條一直都在 , 開始時(shí) FadeInImage 組件是透明狀態(tài) , 顯示進(jìn)度條 , 之后變?yōu)椴煌该?, 進(jìn)度條被覆蓋 , 但是一直在后面轉(zhuǎn) ;


顯示的網(wǎng)絡(luò)圖片 : ( 吸取上一篇博客的教訓(xùn) , 使用風(fēng)景圖片 )





三、完整代碼示例



完整代碼示例 :

import 'package:flutter/material.dart'; import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:transparent_image/transparent_image.dart'; import 'package:cached_network_image/cached_network_image.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);} }class MyHomePage extends StatefulWidget {MyHomePage({Key key, this.title}) : super(key: key);final String title;@override_MyHomePageState createState() => _MyHomePageState(); }class _MyHomePageState extends State<MyHomePage> {int _counter = 0;void _incrementCounter() {setState(() {_counter++;});}/// SD 卡路徑String sdPath;@overridevoid initState() {// 獲取 SD 卡路徑getSdPath();}void getSdPath() async {String path = (await getExternalStorageDirectory()).path;setState(() {sdPath = path;});}@overrideWidget build(BuildContext context) {print("sdPath : $sdPath");return Scaffold(appBar: AppBar(title: Text(widget.title),),body: Center(child: ListView(children: [Stack(children: [// 進(jìn)度條Center(child: CircularProgressIndicator(),),Center(// 網(wǎng)絡(luò)加載時(shí)漸變出現(xiàn)child: FadeInImage.memoryNetwork(// Placeholderplaceholder: kTransparentImage,image: "https://img-blog.csdnimg.cn/2021032321394771.png",),)],),// 圖片組件 , 從網(wǎng)絡(luò)中加載一張圖片/*Image.network(// 圖片地址"https://img-blog.csdnimg.cn/2021032313493741.png",),*//*Image(image: AssetImage("images/sidalin.png"),),*///Image.asset('images/sidalin2.png', ),/// 從 SD 卡加載圖片/*if(sdPath != null)Image.file(File('$sdPath/sidalin3.png'),width: 200,),*/],)),floatingActionButton: FloatingActionButton(onPressed: _incrementCounter,tooltip: 'Increment',child: Icon(Icons.add),), // This trailing comma makes auto-formatting nicer for build methods.);} }

pubspec.yaml 配置文件 :

name: flutter_image_widget description: A new Flutter application.version: 1.0.0+1environment:sdk: ">=2.1.0 <3.0.0"dependencies:flutter:sdk: fluttercupertino_icons: ^0.1.2path_provider: ^2.0.1transparent_image: ^2.0.0cached_network_image: ^2.5.1dev_dependencies:flutter_test:sdk: flutterflutter:uses-material-design: trueassets:- images/sidalin.png- images/sidalin2.png- images/waiting.gif

運(yùn)行效果 :





四、相關(guān)資源



參考資料 :

  • Flutter 官網(wǎng) : https://flutter.dev/
  • Flutter 插件下載地址 : https://pub.dev/packages
  • Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強(qiáng)烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區(qū) : https://flutter.cn/
  • Flutter 實(shí)用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文檔 : https://dart.cn/
  • Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
  • Flutter 中文網(wǎng) ( 非官方 , 翻譯的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相關(guān)問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )
  • GitHub 上的 Flutter 開源示例 : https://download.csdn.net/download/han1202012/15989510

博客源碼下載 :

  • GitHub 地址 : https://github.com/han1202012/flutter_image_widget ( 隨博客進(jìn)度一直更新 , 有可能沒有本博客的源碼 )

  • 博客源碼快照 : https://download.csdn.net/download/han1202012/16073006 ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )



總結(jié)

以上是生活随笔為你收集整理的【Flutter】Image 组件 ( 内存加载 Placeholder | transparent_image 透明图像插件 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。