Hellom's Studio.

flutter 之图片控件详解

字数统计: 731阅读时长: 3 min
2019/08/08 Share

Image 支持如下几种类型的构造函数:

new Image

  • 用于从 ImageProvider 获取图像

new Image.asset

  • 使用 keyAssetBundle 获取图像

new Image.network

  • 从网络 URL 中获取
1
2
3
e.g.

Image.network('网络图片地址');

new Image.file

  • 从本地文件中获取

new Image.memory

  • Uint8List 获取图像

如何加载静态图片及处理不同分辨率的图片?

加载项目中的静态图片步骤:

  • pubspec.yaml 文件中声明图片资源的路径

    assets:
       - images/my_icon.png
  • 使用 AssetImage 访问图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
e.g.

Image(
width: 26,
height: 26,
image: AssetImage(my_icon.png),
)

// or 两者等效

Image.asset(
my_icon.png,
width: 26,
height: 26,
)

为了让 Image 能够根据像素密度自适应不同分辨率 使用 AssetImage 指定图像 并且确保在 Widget 树中的 Image Widget 上方存在 MaterialApp 、 WidgetsApp 或 MediaQuery 窗口Widget

如何加载本地图片?

加载完整路径的本地图片

1
2
3
4
5
e.g.

import 'dart:io';

Image.file(File('/sdcard/Download/Stack.png'))

加载相对路径的本地图片

  • pubspec.yaml 文件中添加 path_provider 插件

  • 导入头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
e.g.

import 'dart:io';
import 'package:path_provider/path_provider.dart';

//Image.file(File('/sdcard/Download/Stack.png')),
FutureBuilder(future: _getLocalFile("Download/Stack.png"),
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
return snapshot.data != null ? Image.file(snapshot.data) : Container();
})
)

//获取SDCard的路径:
Future<File> _getLocalFile(String filename) async {
String dir = (await getExternalStorageDirectory()).path;
File f = new File('$dir/$filename');
return f;
}

如何设置 placeholder?

从内存中加载 placeholder

设置 placeholder 需要借助 FadeInImage 从内存 本地资源中加载 placeholder

  • pubspec.yaml 文件中添加 transparent_image 插件

https://pub.dev/packages/transparent_image#-installing-tab-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
e.g.

import 'package:flutter/material.dart';
import 'package:transparent_image/transparent_image.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'Fade in images';

return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Stack(
children: <Widget>[
Center(child: CircularProgressIndicator()),
Center(
child: FadeInImage.memoryNetwork( // 淡入效果
placeholder: kTransparentImage,
image: 'https://s2.ax1x.com/2019/07/18/ZXbWKU.jpg',
),
),
],
),
),
);
}
}

从本地资源中加载 placeholder

  • 配置本地资源图片

    flutter:
      assets:
       - assets/loading.gif
  • 具体实例代码:
1
2
3
4
5
6
e.g.

FadeInImage.assetNetwork(
placeholder: 'assets/loading.gif',
image: 'https://s2.ax1x.com/2019/07/18/ZXbWKU.jpg',
);

如何配置图片缓存?

可以使用 cached_network_image 插件 从网络上加载图片 并且缓存到本地 以供下次使用

  • pubspec.yaml 文件中添加 cached_network_image 插件

https://pub.dev/packages/cached_network_image#how-to-use

  • 具体实例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
e.g.

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'Cached Images';

return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
// 从缓存中 获取图片
child: CachedNetworkImage(
placeholder: (context, url) => new CircularProgressIndicator(),
imageUrl:
image: 'https://s2.ax1x.com/2019/07/18/ZXbWKU.jpg',
'https://picsum.photos/250?image=9',
),
),
),
);
}
}

如何加载Icon?

可以使用 Icon 来加载

Flutter 提供了 icon

可根据选择使用对应的 icon

https://design.google.com/icons/
https://material.io/tools/icons/?style=baseline

1
2
3
4
5
6
7
8
9
10
11
12

const Icon(this.icon//IconDate, {
Key key,
this.size,//大小
this.color,//颜色
this.semanticLabel,//标志位
this.textDirection,//绘制方向,一般使用不到
})

e.g.

Icon(Icons.android,size: 100)
CATALOG
  1. 1. Image 支持如下几种类型的构造函数:
    1. 1.1. new Image
    2. 1.2. new Image.asset
    3. 1.3. new Image.network
    4. 1.4. new Image.file
    5. 1.5. new Image.memory
  2. 2. 如何加载静态图片及处理不同分辨率的图片?
  3. 3. 如何加载本地图片?
    1. 3.1. 加载完整路径的本地图片
    2. 3.2. 加载相对路径的本地图片
  4. 4. 如何设置 placeholder?
    1. 4.1. 从内存中加载 placeholder
    2. 4.2. 从本地资源中加载 placeholder
  5. 5. 如何配置图片缓存?
  6. 6. 如何加载Icon?