支持如下圖像格式:JPEG,PNG,GIF,動畫GIF,WebP,動畫WebP,BMP和WBMPhtml
在項目的根目錄下建立圖片文件夾bash
主資源默認對應於1.0倍的分辨率圖片。看一個例子:網絡
…/my_icon.png //主資源
…/2.0x/my_icon.png
…/3.0x/my_icon.png
複製代碼
# To add assets to your application, add an assets section, like this:
assets:
- images/2.0x/live_end_yyicon.png
- images/3.0x/live_end_yyicon.png
- images/2.0x/treasure_default_card.png
- images/3.0x/treasure_default_card.png
複製代碼
var netImageUrl1 = "http://c.hiphotos.baidu.com/image/pic/item/a8773912b31bb0516a13ec1d387adab44aede0d4.jpg";
var netImageUrl2 = "https://flutter.io/images/homepage/header-illustration.png";
var localImageUrl = "images/2.0x/treasure_default_card.png";
var fileTest = "/storage/emulated/0/cache/111.png";
複製代碼
Image.asset(localImageUrl)
複製代碼
Image.network(netImageUrl2)
複製代碼
Image(image: NetworkImage(netImageUrl1),),
Image(
image: AssetImage(localImageUrl),
)
複製代碼
Image.file(File(fileTest))
複製代碼
alignment: Alignment.bottomLeft
複製代碼
centerSlice: Rect.fromLTWH(20, 20, 100, 100),
centerSlice: Rect.fromLTRB(100, 100, 100, 100),
複製代碼
color: Colors.black12,
colorBlendMode: BlendMode.colorBurn,
複製代碼
repeat:ImageRepeat.repeatX,
repeat:ImageRepeat.repeatY,
repeat:ImageRepeat.repeatX,
複製代碼
//設置寬高
width: 300,
height: 300,
複製代碼
fit 模式 | 描述 |
---|---|
BoxFit.fill | 全圖顯示,顯示可能拉伸,充滿 |
BoxFit.contain | 全圖顯示,顯示原比例,不需充滿 |
BoxFit.cover | 顯示可能拉伸,可能裁剪,充滿 |
BoxFit.fitWidth | 顯示可能拉伸,可能裁剪,寬度充滿 |
BoxFit.fitHeight | 顯示可能拉伸,可能裁剪,高度充滿 |
BoxFit.scaleDown | 效果和contain差很少,可是此屬性不容許顯示超過源圖片大小,可小不可大 |
有圖片的連接app
import 'package:flutter/material.dart';
import 'dart:io';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image Demo',
theme: ThemeData(
primarySwatch: Colors.green
),
home: MyHomePage(title: 'Image Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>{
var netImageUrl1 = "http://c.hiphotos.baidu.com/image/pic/item/a8773912b31bb0516a13ec1d387adab44aede0d4.jpg";
var netImageUrl2 = "https://flutter.io/images/homepage/header-illustration.png";
var localImageUrl = "images/2.0x/treasure_default_card.png";
var fileTest = "/storage/emulated/0/cache/111.png";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: ListView(
children: <Widget>[
//重新建的image文件中獲取
Image.asset(localImageUrl),
//加載網絡圖片
Image.network(netImageUrl2),
// 本地文件圖片
// Image.file(File(fileTest)),
//使用ImageProvider加載圖片
Image(image: NetworkImage(netImageUrl1),),
Image(
width: 100,
height: 100,
image: AssetImage(localImageUrl),
),
Image(
//設置imageProvider
image: AssetImage(localImageUrl),
//設置圖像在寬高範圍內的對齊方式
alignment: Alignment.bottomLeft,
//設置邊緣裁剪形式
// centerSlice: Rect.fromLTWH(20, 20, 100, 100),
// centerSlice: Rect.fromLTRB(100, 100, 100, 100),
//color 與 colorBlendMode 結合使用,用於顏色與每一個圖像像素混合
color: Colors.greenAccent,
colorBlendMode: BlendMode.exclusion,
// ? 圖像過濾器的質量級別
filterQuality: FilterQuality.high,
//繪製圖像未覆蓋的佈局邊界的任何部分
// repeat:ImageRepeat.repeat,
// repeat:ImageRepeat.repeatY,
// repeat:ImageRepeat.repeatX,
//設置寬高
width: 300,
height: 300,
fit: BoxFit.fill,
)
],
),
),
);
}
}
複製代碼