三、完整代码示例
完整代码示例 :
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. @override Widget 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; @override void initState() { // 获取 SD 卡路径 getSdPath(); } void getSdPath() async { String path = (await getExternalStorageDirectory()).path; setState(() { sdPath = path; }); } @override Widget build(BuildContext context) { print("sdPath : $sdPath"); return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: ListView( children: [ Stack( children: [ // 进度条 Center(child: CircularProgressIndicator(),), Center( // 网络加载时渐变出现 child: FadeInImage.memoryNetwork( // Placeholder placeholder: kTransparentImage, image: "https://ucc.alicdn.com/images/user-upload-01/2021032321394771.png", ), ) ], ), Center( // 图片加载完成之前显示的是 placeholder , 加载完成后显示网络图片 child: CachedNetworkImage( // 加载网络图片过程中显示的内容 , 这里显示进度条 placeholder: (context, url)=>CircularProgressIndicator(), // 网络图片地址 imageUrl: "https://ucc.alicdn.com/images/user-upload-01/20210324100419204.png", ), ), Stack( children: [ // 进度条 Center(child: CircularProgressIndicator(),), Center( // 网络加载时渐变出现 child: FadeInImage.assetNetwork( // Placeholder placeholder: "images/waiting.gif", image: "https://ucc.alicdn.com/images/user-upload-01/2021032321394771.png", ), ) ], ), // 图片组件 , 从网络中加载一张图片 /*Image.network( // 图片地址 "https://ucc.alicdn.com/images/user-upload-01/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. ); } }
运行效果 :
四、相关资源
参考资料 :
Flutter 官网 : https://flutter.dev/
Flutter 插件下载地址 : https://pub.dev/packages
Flutter 开发文档 : https://flutter.cn/docs ( 强烈推荐 )
官方 GitHub 地址 : https://github.com/flutter
Flutter 中文社区 : https://flutter.cn/
Flutter 实用教程 : https://flutter.cn/docs/cookbook
Flutter CodeLab : https://codelabs.flutter-io.cn/
Dart 中文文档 : https://dart.cn/
Dart 开发者官网 : https://api.dart.dev/
Flutter 中文网 ( 非官方 , 翻译的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )
GitHub 上的 Flutter 开源示例 : https://download.csdn.net/download/han1202012/15989510
博客源码下载 :
GitHub 地址 : https://github.com/han1202012/flutter_image_widget ( 随博客进度一直更新 , 有可能没有本博客的源码 )
博客源码快照 : https://download.csdn.net/download/han1202012/16059814 ( 本篇博客的源码快照 , 可以找到本博客的源码 )