Image是Flutter中的图片组件,类似于iOS中的UIImageView,可加载资源图片、网络图片、本地图片等,可设置拉伸方式,重复模式等。
<!--more-->
Image的加载方法有以下几种:
使用方式各有不同,如下:
Image.asset(name)
是Image(image: AssetImage(name))
Image.file(path)
是Image(image: FileImage(File(path)))
Image.network(url)
是Image(image: NetworkImage(url))
Image.memory(list)
是Image(image: MemoryImage(list))
而且:
使用asset images时,图片打包在apk或ipa中,会导致包变大 加载时间对比:NetworkImage > FileImage > AssetImage > MemoryImage
Flutter加载本地图片,参考添加资源和图片,步骤如下:
pubspec.yaml
文件,找到这里# To add assets to your application, add an assets section, like this:
,打开下面的注释,这里需要注意yaml文件的格式对齐问题;pubspec.yaml
并列创建一个assets
文件夹,里面放入图片;添加成功后,使用时,也需要注意路径是从assets开始的,代码如下:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text Widget',
home: Scaffold(
body: Center(
child: Container(
child: new Image.asset('assets/images/jietu.png'),
// child: new Image(
// image: AssetImage('assets/images/jietu.png'),
// ),
width: 300.0,
height: 200.0,
color: Colors.lightBlue,
),
),
),
);
}
}
加载网络图片,代码如下:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text Widget',
home: Scaffold(
body: Center(
child: Container(
child: new Image.network(
'https://inews.gtimg.com/newsapp_ls/0/13792660143/0.jpeg',
),
width: 300.0,
height: 200.0,
color: Colors.lightBlue,
),
),
),
);
}
}
Image的常用属性如下:
代码如下:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text Widget',
home: Scaffold(
body: Center(
child: Container(
child: new Image.network(
'https://inews.gtimg.com/newsapp_ls/0/13792660143/0.jpeg',
alignment: Alignment.center,
filterQuality: FilterQuality.low,
// scale: 1.0,
// fit: BoxFit.scaleDown,
// repeat: ImageRepeat.repeatY,
// color: Colors.greenAccent,
// colorBlendMode: BlendMode.colorDodge,
),
width: 300.0,
height: 200.0,
color: Colors.lightBlue,
),
),
),
);
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。